Giter Site home page Giter Site logo

love-strawberries-opencv's Introduction

#Object Detection with OpenCV

I have used OpenCV + Python to detect strawberries in an image. Series of operations are performed which are well documented in the code to eventually highlight the biggest strawberry in an image and then draw a green circle around it. The code for this project is present in fun.py.

#Dependencies

1)openCV

2)matplotlib

3)numpy

#Usage

Run fun.py to create a new image with the detected strawberry. The last 3 lines at the bottom of demo.py let you define the input image name and the output image name. This detection takes a split second. Deep learning would be more accurate but requires more computation currently. Sometimes you just need to quickly detect an image and don't mind handcrafted which features to look for.

#Original Image

download1

#Explanation

This code contains the following functions:

1)find_strawberry(image): this function takes image as input and returns the newly generated image with detected strawberry

#Steps

Step-1: Convert the image Color scheme from BGR to RGB

image

Step-2: Make a consistent size (perform scaling accordingly)

Step-3: Remove noise from the image.Clean,smooth Colors without dots using Gaussian Filter

image_blur

Step-4: Convert the image Color scheme from RGB to HSV Unlike RGB,HSV seperates luma, or the image intensity, from chroma or the color information.This was done, because the main focus was on color. For HSV, Hue range is [0,179], Saturation range is [0,255] and Value range is [0,255].

image_blur_hsv

Step-5: Now using the property of the HSV: Filter by colour 0-10 hue => minimum red amount, max red amount => Color of strawberry must lie in this range

mask1

and Filter by brightness 170-180 hue

mask2

Step-6: Now look for what is in both ranges and hence combine masks that were created in Step-5 Combined Mask = Mask1 + Mask2

mask

For Best results of this Step we need to perform clean up.

Step-6(a): Morph the image. Closing operation is performed which is Dilation followed by Erosion. It is useful in closing small holes inside the foreground objects, or small black points on the object.

##Closing Operation

mask_closed

Step-6(b): Opening operation is performed which is the sister of Closing operation for removing noise.basically it is erosion followed by dilation. It is useful in removing noise.

##Opening Operation

mask_clean

**Read more about Closing and Opening operation on http://homepages.inf.ed.ac.uk/rbf/HIPR2/close.htm **

**Eroding and Dilating: http://docs.opencv.org/2.4/doc/tutorials/imgproc/erosion_dilatation/erosion_dilatation.html **

Step-7: Find biggest strawberry and get back list of segmented strawberries and an outline for the biggest one.

big_strawberry_contour

Step-8: Overlay cleaned mask on Image (I feel that this step is not necessary)

overlay

Step-9: Circle the biggest strawberry

Step-10: We're done, convert back to original color scheme

#Final Image

yo2

2)find_biggest_contour(image) : returns the biggest contour in the provided input. In this function,contours were detected and a hierarchy was created and highest contour was returned

3)Overlay_mask: take mask and image as input and returns new image in which mask is overlayed on the input image

4)circle_counter(image,contour) : returns new image with contour boundary drawn on the provided image with green color

5)show(image) : this function was to show image using matplotlib

#Credits

The credits for the original code go to gcrowder.

love-strawberries-opencv's People

Contributors

dipankersingh avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.