Extract Rectangle From Contour OpenCV - c++

after making some edge and corner detection and then find contours i have this output.
how i can crop this image and return only this rectangle using openCV
EDIT:
i tried cvBoundingRect and then setimageROI but the output image still having some background but i want the rectangle only
Thank You.

i hope you need the rectangle area you selected.
For this you need to make another grayscale image, let us call it 'mask'. Then draw the rectangle contour obtained on it and fill it with white (255,255,255). You will obtain an image like this ( all images hand-edited in paint program):
Now just have an bitwise_and operation on both the images. You will get result as this:
**NB:**Now if it is not the one you wanted, instead you wanted this kind of selection, You can find information about it in this SOF question (thanks to karl philip for the link) .

I guess Mustafa wants to get the box automatically? If not, please accept Abid's answer and ignore this one.
Otherwise:
As I don't know how far it should generalize, for this specific image, do hough transform, which gives you straight lines. However the line at the bottom can become false positive. But with some post processing, e.g. blur, dilate, you will be able to get rid of it. Or you could use the knowledge that the lines build a rectangle.

Related

Shape Detection Using OpenCv

I am working on Image Processing and for that i am using OpenCV Library in c++.
I have one image in which i want to detect particular shape and mainly want its point.
I have below image , where four black corners are there.I want to detect four corner points as i have drawn with red color.
And please note that image can be at any angle or position. Not straight always.
I have tried cv::threshold, canny, findContours, minAreaRect but I am not getting expected output.
Please anybody can help me.Thanks in Advance.
OpenCV has a function to detect corners using the Harris-Stephens method, here is a tutorial with C++ code example.

Finding individual center points of circles in an image

I am using open CV and C++. I have a completely dark image which has 3 colored points on it. I need their center coordinates. If I have only one colored point in the dark image, it will automatically display its center coordinate. However,if I take as input the dark image with the 3 colored points,my program will make an average if those 3 coordinates and return the center of the 3 colored points together,which is my exact problem. I need their individual center coordinates.
Can anyone suggest a method to do that please. Thanks
Here is the code http://pastebin.com/RM7chqBE
Found a solution!
load original image to grayscale
convert original image to gray
set range of intensity value depending on color that needs to be detected
vector of contours and hierarchy
findContours
vector of moments and point
iterate through each contour to find coordinates
One of the ways to do this easily is to use the findContours and drawContours function.
In the documentation you have a bit of code that explains how to retrieve the connected components of an image. Which is what you are actually trying to do.
For example you could draw every connected component you will find (that means every dot) on it's own image and use the code you already have on every image.
This may not be the most efficient way to do this however but it's really simple.
Here is how I would do it
http://pastebin.com/y1Ae3e2V
I'm not sure this works however as I don't have time to test it but you can try it.

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)?

ideas for removing background fringes using opencv

basically i wrote a code that had two images. a reference img and a background img. So far i have successfully found the matching image by using feature recognition. Then i rotated it and resized it to look identical as the reference image. The only problem left is the fact that the image as some of the background image on the fringes of the object. This image has been appropriately cropped so i just need to work with the image below. The most obvious answer that first came to me was perhaps use a edge detection algorithm (canny) and use that to give me a clue on where the background may lie. However since the images itself could technically be anything i feel like there would be lots of noise and various unusual errors so if possible i would rather not want to take that path. I also saw the backgroundsubtraction MOG but it seemed like that works for videos and not for single stilled image. In case i was wrong i tried the following code but had 0 effect:
BackgroundSubtractorMOG bs_mog(3, 4, 0.8);
Mat foreground_mog;
bs_mog (cropped_img, foreground_mog, -1.0);
Perhaps i am doing it wrong. So my thought is other than edge detection and if backgroundsubtractorMOG is only for moving images are there any other ideas or options i can look into to remove the fringe background image (i want to turn it all into just white)
thank you in advance for your ideas and comments
EDIT:
well i unerstand the logic already posted by others but i am unsure what the best way to make a mask for this bottom image would be. It is important to note that the image can technically be anything. Not necessary round in shape. Also due to changes in the algorithm the shape must be resized after the object is separated from the background. This means i can't use my reference image to just make a mask and use that mask on this image due to the difference in size.
Segmentation could work. Try cvgrabCut() with a customized mask.
Set as background all pixels very close to borders. (red in image below)
Set as foreground the center area of your image. (green in image below)
Any intermediate pixels set them to probably foreground. (gray in image below)

How can I find ROI and detect markers inside?

I'm a beginner in computer vision. I have a question about detection and tracking. I want to detect the white rectangle in the image below, to determine the interesting area and to detect the contour of the red markers.
But I don't want to make use of color information to detect the markers.
Can anyone give me suggestions on how to do this?
If you want to just detect the circles, an adapted Hough Tranfrom should work.
You can find the contours with CvFindContours and use CvApproxPoly() to find the rectangle. You can find a good example of how to use this function to find rectangles here and adapt it to your situation. To find the circles I would advise to do something with the ratio between the arcLength and the area of the contours you find as for circles this ratio is very specific. To find the arcLength use cvArcLength(CvSeq* c) to find the area use cvContourArea(CvSeq* c) while going through the contours in a for loop.