First time here? Check out the FAQ!

Ask Your Question
0

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

asked Sep 12 '18

buubuu gravatar image

updated Sep 24 '18

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.
C:\fakepath\img2.PNG

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.

Preview: (hide)

Comments

@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 (Sep 19 '18)edit

@ak1 I have a link to the image here https://ibb.co/mX6Qgp

I was actually able to get it working by setting the threshold value to 100. But this causes some of my other images to crop out weirdly. Like this one https://ibb.co/ckM0xU

buubuu gravatar imagebuubuu (Sep 24 '18)edit

1 answer

Sort by » oldest newest most voted
0

answered Sep 12 '18

ak1 gravatar image

updated Sep 29 '18

@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

Mat drawing = Mat::zeros( src_gray.size(), CV_8UC3 ); //create black image
drawContours(drawing, contours, contours.size() - 1,Scalar(255,255,255), CV_FILLED); //create form mask
src.copyTo(drawing,drawing); //copying form data from orignal image using masking

Rect box = boundingRect(contours[contours.size() - 1]);  
imwrite( "form.jpg", drawing(box) );
}

input1: C:\fakepath\form.jpg result1: image description

input2: C:\fakepath\form1.jpg result2: image description

Preview: (hide)

Comments

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 (Sep 12 '18)edit

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

There are a good list of resources mentioned here.

http://answers.opencv.org/question/69...

ak1 gravatar imageak1 (Sep 13 '18)edit

@ak1 I have a link to the image here https://ibb.co/mX6Qgp

I was actually able to get it working by setting the threshold value to 100. But this causes some of my other images to crop out weirdly. Like this one https://ibb.co/ckM0xU

buubuu gravatar imagebuubuu (Sep 24 '18)edit

@buubuu sorry for late reply. I thought form in your image wont be tilted as per given in first image. It is easy to extract the form with above code .You have to take care of rotation. I will edit the code.

ak1 gravatar imageak1 (Sep 29 '18)edit

@buubuu I have edited the code.

ak1 gravatar imageak1 (Sep 29 '18)edit

Question Tools

1 follower

Stats

Asked: Sep 12 '18

Seen: 709 times

Last updated: Sep 29 '18