Ask Your Question

How to crop only the form on this image? opencv 3.2

asked 2018-09-11 19:22:22 -0500

buubuu gravatar image

updated 2018-09-12 13:54:08 -0500

Hi there. I'm trying to crop just the form from this image without the content outside of the box. C:\fakepath\img.jpg

So far I've been able to get just the just content.

Can someone please show me how to do this in code? OpenCv has been a challenge for me. I really need some hand holding. Thanks in advanced.

@ak1 Thanks for the answer. It worked the image i provided. But I tried the with a similar image, it only gives me a brown box.

edit retag flag offensive close merge delete


@buubuu upload your image here. You need to adjust threshold value once correctly. To make it robust you can use adaptive thresholding.

ak1 gravatar imageak1 ( 2018-09-19 06:57:19 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-09-12 02:13:06 -0500

ak1 gravatar image

@buubuu you can do like this,

#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>

using namespace cv;
using namespace std;

int main()
Mat src = imread("/home/aniket/Downloads/form.jpg"); //Load source image
Mat src_gray;
cvtColor(src, src_gray, CV_BGR2GRAY);
threshold(src_gray, src_gray, 150 , 255 , CV_THRESH_BINARY_INV); // inverse thresholding (prepare input for findcontours)

vector<vector<Point> > contours; // Vector for storing contour
vector<Vec4i> hierarchy;
findContours( src_gray, contours, hierarchy,CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE ); // Find the contours in the image

Rect box = boundingRect(contours[contours.size() - 1]);   // get roi of largest contour
imwrite( "form.jpg", src(box) );

image description

edit flag offensive delete link more


Thanks so much! Can you point me to a good resource on learning opencv? I find a lot of the terms in the officals docs difficult to understand. Like where did you even learn that contours can be stored in a vector? Whats a vector? And whats Vec4i and how did you know to use that? Is there easily digestable docs for all this?

buubuu gravatar imagebuubuu ( 2018-09-12 13:29:06 -0500 )edit

There are some good blogs. 1. Harris blog 2. OpenCV SRF 3. Aishack.

There are a good list of resources mentioned here.

ak1 gravatar imageak1 ( 2018-09-13 10:06:25 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-09-11 19:22:22 -0500

Seen: 43 times

Last updated: Sep 12