Opencv blob detection and vectorization - c++

I have a piece of flat material which has seen pieces taken off (convex pieces) and I would like to have what's left of it vectorized.
For example this picture http://www.laser-cutting.com/images/Coreplast_med.jpg
In this image's case I would like the blob of the circle and of the star identified as not part of the material anymore, their contours and the contour of the image vectorized as to be left only with what's left of the material (forget the handwriting).
Getting the contours is no problem with the cvCanny but how do I then vectoriz the contours? Is there a way to identify the blobs?
Any idea on how to proceed? I've read many blob-related questions but none helped me.
Thanks

In case you know what your shapes will look like, you could try this:
How to detect simple geometric shapes using OpenCV
Also cvFindContours as Samuel Audet said should be good for the vectorization :
http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=findcontours#findcontours
contours – Detected contours. Each contour is stored as a vector of points.
From there you can play around with your points and make whatever 2D representation/processing you want.

Related

opencv - Contour Alignment and comparison

I have tried to use edge detection to find the contour of images and try to compare the similarity of the contours by matchshape function. However, results are not as good as expected. I think it may be because of the images are not aligned before calculating the similarity. Therefore, I am asking for a way of aligning two contours in opencv. I am thinking of aligning by first finding the smallest bounding box or circle and then find out translation, rotation or resize needed to align those boxes. Then apply those transformation on the contour and test the similarity of them. Does this method work? Is there any method to align images? Thanks for your help. For your reference, attached are two contours going to be tested. They should be very similar but the distance found is quite large. The first two images have larger distance than that between the first and the last one, which seems contradicts with what it looks like (the last one should be the worst). Thanks.
These kinds of problems are known as registration problems. CPD, BCPD, and ICP would be your best shot.
[https://github.com/neka-nat/probreg][1]

OpenCV haarcascade_frontalface detection region

For face detection I have used the haarcascade_frontalface_alt.xml.
The problem is that the this this algorithm gives me a roi a little bit larger so the rectangle catches some hair and some of the background. Is there a solution to change the dimension of this rectangle?
This what the haarcascade_frontalface_alt.xml detects:
And this what I want to detect:
You cannot reply on OpenCV to do this because its model is trained based on face images just like the first one. That is to say, it is supposed to give face detections like the first one.
Instead, consider to crop the detected rectangles a little bit, whatever size you want it be.
To be more accurate, you can crop the faces based on the facial features, as discussed in this thread.

OpenCV C++ set ROI from a rectangular area

Anyone know how to set ROI based on image bellow?
I used Hough Transform to detect the white line and draw the red line into the image.
What I need to do is to set the ROI in the rectangle.
Since Hough Transform unable to get location of each rectangle and the main problem is I cannot defined the location (x,y) manually.
Any solution that able to auto detect the rectangle and set the ROI?
Anyone can give some idea for me or the code can be use?
Please forgive my poor english and thank you.
this blog post is very good in explaining how to find a rectangle with the hough transform and it has also some c++ code with opencv 2 API.
The approach is to find lines, intersect them, and find the rectangle. In your case you will have more rectangles and so it's a little bit more complicated..
But if you manage to obtain such image.. why don't use just some threshold and find connected regions (aka blob)?

Detection of parking lot lines and ROI openCV

I am working on a openCV project, trying to detect parking spaces and extract the ROI(Region of Interest) from an image for further vehicle detection. The image provided will consist of all empty parking spaces. I have read several posts and tutorials about this. So far, the approach I have tried are:
1.Convert image to grayscale using `cvtColor()`
2.Blur the image using `blur()`
3.Threshold the image to get edges `threshold()`
4.Find image contours using findContours()
5.Finding all convex contours using `convexHull()`
6.Approx polygonal regions using `approxPolyDP()`
7.Get the points for the result from 5, if total number of points =4.
Check for area and angle.
I guess the problem with this approach is when I do findContours(), it finds irregular and longer contours which causes approxPolyDP to assume quadrilaterals larger than the parking space itself. Some parking lines have holes/irregularity.
I have also tried goodFeaturesToTrack() and it gives corners quite efficiently, but the points stored in the output are in arbitrary order and I think it is going to be quite rigorous to extract quadrilaterals/rectangles from it.
I have spent quite good hours on this. Is there any better approach to this?
This is the image I am playing with.
Try using dilate on the thresholded image to make the holes disappear.
Here is a good tutorial on it: opencv erode and dilate.

OpenCV - find or access shape contour not surrounded by bg, only separated by an outline

I've been trying to find the contour of a single shape in a very plain background using OpenCV's findContour (I'd like to use the C++ syntax). However, it keeps on making its outline a contour and not the shape itself. I'm thinking it's because of the white edge resulted from Canny which doesn't make the shape closed.
Case A: Shape is by the image's edge
(This is not the actual input image but a simpler input image to illustrate this problem.)
Case B: Background surrounds the shape
There are the main functions I used:
findContours( grayImage, contours, hierarchy, RETR_LIST,CHAIN_APPROX_SIMPLE);
approxPolyDP(Mat(contours.at(largestContourIndex)),poly,3,true);
drawContours(output, contours, largestContourIndex, RGB(250,0,100), -1, 8, hierarchy, 0, Point() );
EDIT: Skipping edge detection gives the contour I need but I need to have the best contour approximate I can get.
Thanks in advance.
Did you try playing around with morphology operations?
If your basic problem is that the contour you're getting is on the outside of the object instead of the inside, and especially if your object are made out of so clear-cut and mostly regular shapes, than morphology might help.
I know OpenCV has implementations of dilation and erosion, as well as opening and closing operations. A very simple approach that might work in your situation is just eroding the shape a little bit (maybe 1-2-3 iterations) and then doing exactly what you are doing already. Hopefully, then, you'll get the outer contours of the eroded shape, that should actually be the inner contours of the original shape.
I think OpenCV actually implements even some more complex morphology, but as always, try the simple stuff first :D
It seems to me that the contour you are looking for is probably detected, but you are not using it. Instead you are using the largest contour. Try plotting all found contours one by one and see if it's in there.
If it is not, try inverting the canny image and repeating the process.
I still haven't found the reason why I can't get the shape contour but I found a workaround. After doing erosion and dilation, I basically have to draw a border or a rectangle on the outermost pixels of the input image for the background to surround the shape, ...
rectangle(input,Point(0,0),Point(input.cols-1,input.rows-1),Scalar(0,0,0),1,8,0);
... hence, letting Canny draw a closed shape outline and giving me the shape contour I want. I am still trying to successfully invert Canny's output like what #dvhamme has suggested but it's still giving me errors. It would be better if somebody points out how to properly get or access the shape contour but thanks everyone for the help.