8

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.

Image Link

Mustafa Ibrahim
  • 1,110
  • 9
  • 17

2 Answers2

6

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

enter image description here

Now just have an bitwise_and operation on both the images. You will get result as this:

enter image description here

**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) .

Community
  • 1
  • 1
Abid Rahman K
  • 48,289
  • 27
  • 140
  • 153
  • 1
    I also suggest this post since it shows [how to warp the image from a vector of `cv::Point`](http://stackoverflow.com/questions/7838487/executing-cvwarpperspective-for-a-fake-deskewing-on-a-set-of-cvpoint). – karlphillip Apr 30 '12 at 13:18
  • 1
    wow. that was the link i have been searching. Thank you!! (edited the answer with karl's link.) – Abid Rahman K Apr 30 '12 at 13:42
1

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.

guinny
  • 1,484
  • 10
  • 19