Ask Your Question
0

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

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

buubuu gravatar image

updated 2018-09-24 14:05:04 -0600

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.

edit retag flag offensive close merge delete

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 ( 2018-09-19 06:57:19 -0600 )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 ( 2018-09-24 15:27:23 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

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

ak1 gravatar image

updated 2018-09-29 03:35:15 -0600

@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

edit flag offensive delete link more

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 ( 2018-09-12 13:29:06 -0600 )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 ( 2018-09-13 10:06:25 -0600 )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 ( 2018-09-24 14:21:48 -0600 )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 ( 2018-09-29 03:03:29 -0600 )edit

@buubuu I have edited the code.

ak1 gravatar imageak1 ( 2018-09-29 03:38:16 -0600 )edit

Question Tools

1 follower

Stats

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

Seen: 631 times

Last updated: Sep 29 '18