Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract Rectangle From Contour OpenCV

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

like image 896
Mustafa Ibrahim Avatar asked Apr 30 '12 11:04

Mustafa Ibrahim


People also ask

How do you find the rectangle in OpenCV?

Use the findContours() and contourArea() Function of OpenCV to Detect Rectangles in Images in Python. We can detect a rectangle present in an image using the findContours() function of OpenCV, and we can use the contourArea() function to sort different rectangles according to their area.


1 Answers

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

like image 119
Abid Rahman K Avatar answered Oct 08 '22 03:10

Abid Rahman K