epc
epc

Reputation: 192

Splitting image with OpenCV

I have an image from lottery tickets that I'd like to split into chunks. Basically the image can be described as a a group of "lanes" having on header lane, several number lanes and one footer lane. Each lane separated from the other by a line like this:

lotto logo
some info
----------------
01 02 03 04 05
----------------
01 02 03 04 05
06 07 08
----------------
footer message

A sample image can be found here. Would it be possible to use opencv to detect those lines and save each lane into a separate image?

Thanks in advance.

Upvotes: 1

Views: 2034

Answers (2)

David Nilosek
David Nilosek

Reputation: 1412

Since you know that you are looking for repeating long horizontalish lines, you could use some kind of texture analysis such as a Gabor Filter to isolate those lines, and then use a line detection algorithm such as the Hough Transform. OpenCV has functions to do all of that.

Given the boring nature of my evening I decided to test those ideas a little bit. After tweaking some of the gabor filter parameters I was able to isolate horizontal-ish lines in the image as such:

IsolatedLines

Code:

    //Tweaked parameters
 cv::Size ks(101,101);
 int sig = 20;
     int lm = 50;
     int th = 0;

    cv::Mat kernel = cv::getGaborKernel(ks,sig,th,lm,0,0,CV_32F);

    //using kernel transpose because theta = 90 (or pi/2) 
    //did not give horizontal lines.
    cv::filter2D(im, im, CV_32F, kernel.t());
    //Threshold to get binary image for Hough
    cv::threshold( im, im, 254, 255, cv::THRESH_BINARY_INV); 

From there I just ran the usual HoughLinesP algorithm (filtering for very long lines) to get this:

Detected Lines

Code:

im.convertTo(im,CV_8U);
std::vector<cv::Vec4i> lines;
cv::HoughLinesP(im,lines,1,CV_PI/180, 200, 800, 0);

From there all you would have to do is identify each line and crop out the correct image areas.

If all of your images look pretty much identical to your sample, this approach might work for you. It also might not be robust enough. Hopefully it gives you something more to think about.

Upvotes: 2

karlphillip
karlphillip

Reputation: 93468

Sure, it's possible.

The first step requires you to detect the lines of dots, and for that you can use algorithms like:

This post provides a brief description on these methods, and you can find several examples around the web on how to use them.

Once you found the location of the lines of dots, you can perform 3 crop operations to extract the 3 Regions Of Interest (ROI) into new images.

Good luck.

Upvotes: 1

Related Questions