Ask Your Question
1

How to find biggest white zone in an scanned image

asked 2015-10-12 10:49:03 -0600

zoosy gravatar image

updated 2015-10-13 08:37:28 -0600

Hi all,

I have a project and I'm stuck... can you please help me to move on?

I'm scanning some documents for a client and I have to apply a software barcode on the first page of the documents, but without covering information ... so I have to find the biggest white zone on the image.

If it's important, I'm scanning in B&W (binary).

I'll upload an example to understand what I mean. The red zone seems to be the biggest white zone in this image and I want to put the barcode in the middle. What I need is to find the coordinates of the white zone. The original image is down below.

example

Original image:

original

edit retag flag offensive close merge delete

Comments

but you need to find all this area or only you want to extract this area in order to find the barcode?

theodore gravatar imagetheodore ( 2015-10-12 11:44:05 -0600 )edit

My problem is to find an area suitable to put the barcode... so, in other words, the barcode is applied after I find the suitable area.

zoosy gravatar imagezoosy ( 2015-10-12 11:50:56 -0600 )edit
2

Can you upload the Original Image without any marks?

Balaji R gravatar imageBalaji R ( 2015-10-12 12:04:49 -0600 )edit

moreover, is the form of the paper sheets more or less the same? meaning with the boxes which contain the text and the handwritten text?

theodore gravatar imagetheodore ( 2015-10-12 12:14:06 -0600 )edit

Hi Balaji, I've uploaded the original image in the problem body

Hi theodore, the pattern is not the same... the invoices are from multiple vendors of my client. That's why I need to find a suitable zone to put my barcode.

Thank you all for your involvement!

zoosy gravatar imagezoosy ( 2015-10-13 00:52:32 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
2

answered 2015-10-12 17:23:28 -0600

updated 2015-10-14 11:49:59 -0600

using Morphological Transformations you can find the biggest white area as shown with the code below.

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    Mat src,src_gray,eroded;

    char* filename = argc >= 2 ? argv[1] : (char*)"Scan_Image0022.jpg";
    src = imread( filename );
    if (src.empty())
    {
        return -1;
    }

    int scale = 4;
    cvtColor( src, src_gray, COLOR_BGR2GRAY );
    resize(src_gray,src_gray,Size(src_gray.cols/scale,src_gray.rows/scale )); // optionally resize image to speed up the process

    rectangle(src_gray,Rect(5,5,src_gray.cols-10,src_gray.rows-10),Scalar(0),4); // correction 1
    src_gray = src_gray >127;

    vector<vector<Point> > contours;

    for( int i = 2 ; i < src_gray.cols/2   ; i++ )
    {
        cv::Mat kernel = cv::Mat::ones(i, i, CV_8U);
        erode(src_gray,eroded, kernel);

        findContours( eroded, contours, RETR_LIST, CHAIN_APPROX_SIMPLE, Point(0, 0) );
        if( contours.size()== 1 & contours[0].size() < 5 )
        {
            resize(kernel,kernel,Size(),0.9,0.9); // correction 2
            dilate(eroded,eroded, kernel);
            findContours( eroded, contours, RETR_LIST, CHAIN_APPROX_SIMPLE, Point(0, 0) );
            polylines( src, Mat( contours[0] ) * scale, true, Scalar(0,0,255)); // resize up the contour and draw
            break;
        }
    }
    imshow( "result", src );

    waitKey(0);
    return(0);
}

resized result image:

image description

edit flag offensive delete link more

Comments

1

Hi sturkmen,

Thank you for your reply... I'll give it a try today and I'll get back to let you all know the result. I'll make some tests on multiple layouts.

I'll be back :)

Zoosy

zoosy gravatar imagezoosy ( 2015-10-13 00:55:01 -0600 )edit
0

answered 2015-10-14 11:15:57 -0600

zoosy gravatar image

OK, I've been playing with sturkmen's script with around 10 images (invoices) and here are 2 examples of something that stops the script from getting the right area. If the contour has some long characters (parentheses or even commas) the script doesn't go forward. In other words, the findContours function is very strict.

Now, the question is: Can I ignore 5-10 pixels around the text to get the right contours? I've looked in the opencv manual, but didn't find anything like that at findContours function.

Thank you all again for your time!

First original image:

original1

Script result:

result1

Second original image:

original2

Script result:

result2

edit flag offensive delete link more

Comments

i updated my code please try it. ( look at the lines having comment // correction) try to understand the logic of code . you can change some values as your need

sturkmen gravatar imagesturkmen ( 2015-10-14 11:51:34 -0600 )edit

I'll play with it right now and will try to understand the logic.

Thank you again and I'll let you know the results.

Best regards, Zoosy

zoosy gravatar imagezoosy ( 2015-10-14 12:12:10 -0600 )edit

i want to point out two feature.( to speed up testing )

  • if you are using windows you can use drag&drop to test different images (just drag the image file to exe file)
  • you can save result image from high-gui window by pressing CTRL + S

    int scale = 4; if you don't need performance set it int scale = 1; or int scale = 2

sturkmen gravatar imagesturkmen ( 2015-10-14 12:17:37 -0600 )edit

Hi Sturkmen,

I've installed openCV in CentOS... I've managed to modify the script in order to give more arguments from command line and also to save the image using imwrite. The scale down to speed the process it's perfect, because with scale=1 it takes a very very long time.

I have to play to this line of code:

if( contours.size()== 1 & contours[0].size() < 5 )

because it's happening what I wrote above: the contour stops if few pixels (from pharanteses or commas) are in their way. If I use more, let's say 8, then the contour continues giving me a bigger white zone. Then, I can take the coordinates and compute the final 4 coordinates. The 2 corrections doesn't help in this case.

I'll be back with the final script that gives me what I need...

Your help is much appreciated!

Zoosy

zoosy gravatar imagezoosy ( 2015-10-15 00:53:04 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-10-12 10:49:03 -0600

Seen: 5,449 times

Last updated: Oct 14 '15