Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

@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

@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]);   // get roi of largest contour
 imwrite( "form.jpg", src(box) drawing(box) );
}

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

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