Ask Your Question
1

Detect If area above line is empty

asked 2016-01-29 17:18:51 -0600

rasterdetect gravatar image

I am starting with the following image

image description

and I am identifying the horizontal lines in order to find the form fields. I am using the following code

Mat src=imread(filename);

if(!src.data)
    cerr<<"problem loading image"<<endl;

Mat rsz;

Size size(800,900);

resize(src, rsz, size);

Mat gray;

if(rsz.channels()==3){
    cvtColor(rsz,gray,CV_BGR2GRAY);
}else{
    gray=rsz;
}

Mat bw;
adaptiveThreshold(~gray, bw, 255,CV_ADAPTIVE_THRESH_MEAN_C,THRESH_BINARY, 15,-2);

Mat horizontal = bw.clone();
Mat vertical =bw.clone();

int scale = 25;

int horizontalsize = horizontal.cols/scale;

Mat horizontalStructure = getStructuringElement(MORPH_RECT,Size(horizontalsize,1));

erode(horizontal, horizontal, horizontalStructure, Point(-1, -1));
dilate(horizontal, horizontal, horizontalStructure, Point(-1, -1));

vector<Vec4i> hierarchy;
vector<vector<Point> > contours;

findContours(horizontal, contours, hierarchy, CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

vector<vector<Point> > contours_poly( contours.size() );
vector<Rect> boundRect( contours.size() );
vector<Mat> rois;

for (size_t i = 0; i < contours.size(); i++)
{

    approxPolyDP( Mat(contours[i]), contours_poly[i], 3,true );
    boundRect[i] = boundingRect( Mat(contours_poly[i]) );

    rois.push_back(rsz(boundRect[i]).clone());
    rectangle( rsz, boundRect[i].tl(), boundRect[i].br(),Scalar(0, 255, 0), 1, 8, 0 );
}

which gives me

image description

which seems to work almost perfectly except it finds the line in the www.labor.ny.gov link in the top right. I don't want this line to be found. So somehow I have to filter the bounding rectangles based on whether they have empty space above them (the link obviously would not pass this filter). Does anyone know how I can implement this filter? From the code you can see that I have the bounding rectangle objects which contain their position, so I think I just need to check if some small bounding rectangle above it is empty.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-01-29 18:21:49 -0600

updated 2016-01-29 18:32:54 -0600

cv::countNonZero is your medicine, you can change your code like below

    approxPolyDP( Mat(contours[i]), contours_poly[i], 3,true );
    boundRect[i] = boundingRect( Mat(contours_poly[i]) );

    Rect upperRect = boundRect[i];
    upperRect.y -= 5;  // you can change this value if needed on other images
    int nonzeropixels = countNonZero( bw( upperRect ) );
    if( nonzeropixels < 10) // // you can change this value if needed on other images
    rectangle( rsz, boundRect[i].tl(), boundRect[i].br(),Scalar(0, 255, 0), 1, 8, 0 );

    rois.push_back(rsz(boundRect[i]).clone());
edit flag offensive delete link more

Comments

This worked thanks!

rasterdetect gravatar imagerasterdetect ( 2016-01-29 18:54:22 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2016-01-29 17:18:51 -0600

Seen: 1,659 times

Last updated: Jan 29 '16