What is contours in computer vision? [closed] - opencv

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am currently studying a computer vision module in college.
I would like to get a theoretical understanding of what contours are in computer vision and what they are used for.

A contour is simply the boundary of an object in an image. Various representations of contours (e.g. chain code, Fourier descriptors, shape context) are used to recognize or categorize objects.
This assumes that you have a way to segment out an object and find its boundary, which itself is not a trivial problem. One particular class of algorithms for finding boundaries is called active contours or snakes. Is this what you are asking about?

Here you can go through the official documentation of opencv, where they say that contour is a simple curve which joins continuous points with same color or intensity.
I used the concept of contours in hand gesture recognition where i have used the area bounded by contours as a basis to remove the noise and detect only the hand part in the image.

Contour is a boundary around something that has well defined edges, which means that the machine is able to calculate difference in gradient (significant difference in magnitude of pixel value), try to see if the same difference continues and forms a recognisable shape and draw a boundary around it. Opencv can do it for a lot of shapes and they are shown in the link below.
Just imagine how you do it with your eyes. You're in a room and you create a boundary in your mind when you see a frame or a monitor or a ball. Exactly the same way contours work in opencv. As #Dima said, various algorithms are used for this purpose.
If you need examples and how contours are represented in opencv, here's a link.
Hope this helps.

Open CV python provides us with contours and several edge detection features to identify several attributes of objects. Contours can be explained simply as a curve joining all the continuous points(along the boundary), having same colour or intensity.
Use of binary image in contour detection:
Contours are useful tool for shape analysis and object detection and recognition. We take in binary image (in other words, images whose pixels have only 2 possible values).So before finding contours, apply threshold or canny edge detection.
Steps for finding the contours:
1)Convert to grayscale
2)Convert to binary image
3)Find contours
Draw contours :
To draw the contours, cv2.drawContours function is used. It can also be used to draw any shape provided you have its boundary points.
Properties of contours:
1)To find the area.
2)To find the perimeter

Related

Can I use approxPolyDP to improve the people detection?

Can I use approxPolyDP to improve the people detection?
I am trying to detect people using BackgroundSubtractorMOG2. So after I receive the foreground of the image I obtain all the contours of the image using this function:
Imgproc.findContours(contourImg, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE);
I iterate each element of contours variable and if the contour has a specified contour area => it's a person
My question is if I can detect the people shapes better ussing approxPolyDP.
Is it possible? If so can you tell me how?
I already used CLOSE morphological operation before finding counturs
My question is if I can detect the people shapes better ussing approxPolyDP.
Although "better" is somewhat ambiguous, you could improve your classification by using that method. From the docs we can see:
The functions approxPolyDP approximate a curve or a polygon with another curve/polygon with less vertices so that the distance between them is less or equal to the specified precision.
That "precision" refers to the epsilon parameter, which stands for the "maximum distance between the original curve and its approximation" (also from the docs). It is basically an accuracy parameter obtained from the arc length (the lower the more precise the contour will be).
We can see from this tutorial that a way to achieve this is:
epsilon = 0.1*cv2.arcLength(contour,True)
approx = cv2.approxPolyDP(contour,epsilon,True)
resulting in a better approximation for the contour. In the example in that tutorial they achieve an optimal contour using 1% of arc length, although you should carefully select this percentage for your specific situation.
By using these procedures you will surely obtain higher precision on your contour areas, which will better enable you to correctly classify people from other objects that have similar areas. You will also have to modify your classification criterion (the >= some_area) accordingly to correctly discriminate between people and non-people objects now that you have a more precise area.

Finding Homography v/s Contour detection

The problem is to detect a known rectangular object in an image.
Which of the following is computationally less expensive:
Finding homography - For finding homography, we use the template of the known object to do feature matching.
Contour detection - We try to detect the biggest contour in the image. In this particular case we assume that the biggest contour will correspond to the known rectangular object we are trying to find.
In both the cases we do perspective transform after detecting the object to set the perspective.
NOTE: We are using Open-CV functions to find the homography and detecting contour.
You should try finding the biggest contour. It's the simplest and will be far faster. You needs to detect Canny edges then find contours and find the one with the biggest area. However, it can fail if contours are unclear or if there is a bigger object as it doesn't consider shape. You can also apply both of your ideas to get better results.
EDIT:
To reply your comment, you have Canny edge + find contours + find biggest against find features + match features
I think that the first combination is less computationally expensive. Moreover, there is a good implementation of squares/rectangle detection here.
However, if the contours of the rectangle are not clear, and if moreover the rectangle is highly textured, you should get better results with features matching.

Confusion regarding Object recognition and features using SURF

I have some conceptual issues in understanding SURF and SIFT algorithm All about SURF. As far as my understanding goes SURF finds Laplacian of Gaussians and SIFT operates on difference of Gaussians. It then constructs a 64-variable vector around it to extract the features. I have applied this CODE.
(Q1 ) So, what forms the features?
(Q2) We initialize the algorithm using SurfFeatureDetector detector(500). So, does this means that the size of the feature space is 500?
(Q3) The output of SURF Good_Matches gives matches between Keypoint1 and Keypoint2 and by tuning the number of matches we can conclude that if the object has been found/detected or not. What is meant by KeyPoints ? Do these store the features ?
(Q4) I need to do object recognition application. In the code, it appears that the algorithm can recognize the book. So, it can be applied for object recognition. I was under the impression that SURF can be used to differentiate objects based on color and shape. But, SURF and SIFT find the corner edge detection, so there is no point in using color images as training samples since they will be converted to gray scale. There is no option of using colors or HSV in these algorithms, unless I compute the keypoints for each channel separately, which is a different area of research (Evaluating Color Descriptors for Object and Scene Recognition).
So, how can I detect and recognize objects based on their color, shape? I think I can use SURF for differentiating objects based on their shape. Say, for instance I have a 2 books and a bottle. I need to only recognize a single book out of the entire objects. But, as soon as there are other similar shaped objects in the scene, SURF gives lots of false positives. I shall appreciate suggestions on what methods to apply for my application.
The local maxima (response of the DoG which is greater (smaller) than responses of the neighbour pixels about the point, upper and lover image in pyramid -- 3x3x3 neighbourhood) forms the coordinates of the feature (circle) center. The radius of the circle is level of the pyramid.
It is Hessian threshold. It means that you would take only maximas (see 1) with values bigger than threshold. Bigger threshold lead to the less number of features, but stability of features is better and visa versa.
Keypoint == feature. In OpenCV Keypoint is the structure to store features.
No, SURF is good for comparison of the textured objects but not for shape and color. For the shape I recommend to use MSER (but not OpenCV one), Canny edge detector, not local features. This presentation might be useful

OpenCV How can I calculate area of binary object? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am using OpenCV and c++. I have black and white image after binarization. How can I calculate area of object when I have only one coordinate of point (x,y) belongs to this object??
since it's a binary image, you can easily get the blobs. Once you get the blobs, you can easily calculate the blob area.
You can use cvBlobsLib to get the blobs. OpenCV hasn't integrated cvBlobsLib. You can do it by yourself. But OpenCV has functionality for contours. You can use findContours to get blobs/contours and then get the area. OpenCV - findContours
OpenCV - Structural Analysis and Shape Description
You'll find information about moments, huMoments, contours, etc. Using moments, you can create your own blobs and it will also help you in getting the blob area.
Here's an Open Source framework for machine vision which uses Python bindings of OpenCV - SimpleCV
They have implemented a complete blob functionality using contours and moments. You can have a look.
Blob
BlobMaker
It's written in Python, but you can port it to C++ and use blobs.
The algorithm is very easy and should be the following:
Find all contours on your image.
Cycle through all contours and check if point is inside a contour.
If contour is found than calculate it's area.
You have to write some kind of region growing algorithm, starting from your startpoint, incrementally adding adjacent pixels that are of the same color.
A lazy (and much slower) way to do it would be to use cv::floodFill function, setting up the pixels in your shape to a specific color, then counting the pixels in you image that have this specific color.

License plate detection [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
My task is to pin-point where is the plate number in an image. The image does not only contain the plate number. It may contain the whole car or anything. I used gaussian blur then grayscale then contrast then laplacian of gaussian to detect the edges.
Now, I am at loss on how to detect where is the plate number in the image. I am not going to read the license number, just make the system know where is the license number.
Can you direct me to a study regarding this? Or perhaps the algorithm that can be used to do this.
Thank you!
I think a more robust way to tackle this is a train a detector if you have enough training images of the license plate in different scenarios. Few things you can try is Haar cascade classifier in Opencv library. It does a multiscale detection of learned patterns.
You could try edge detection or some form of Hough transforms.
For example, do edge detection and then look for rectangles (or if the images aren't straight on, parallelograms) in the image. If you know that the plates will all be the same shape and size ratios, you can use that to speed up your search.
EDIT:
Found this for you.
Using some feature recognition algorithm e.g. SIFT would be a good starting point. Do you need real-time recognition or not? I recommend trying to tighten search space first, for example by filtering out regions from the image (is your environment controlled or not?). There is an article about recognising license plates using SIFT here (I just skimmed it but it looks reasonable).
License-plates or number plates of vehcles come with 2 striking properties.
They have specified color pattern (Black letters on white, yellow or gray background)
Aspect ratio
These properties can be used to extract only the license plate. First threshold the image using adaptive thresholding. Then find contours in the image with aspect ratio in a close range to standard value. This method should work for most of the cases. You can also try erosion followed by dilation of thresholded image to remove noise.

Resources