Ask Your Question

OpenCV find sub images

asked 2012-07-31 13:45:20 -0500

I have a scanner which is big enough to scan multiple pictures at once. Unfortunatelly, all the pictures are stored in one jpg file, separated only by white borders. Is there any way to automatically find the sub images and store them in separate files? I was thinking about using OpenCV to get the job done, but I can't find the right functions. Does anybody know which OpenCV function would work?

Thanks, Konstantin

edit retag flag offensive close merge delete


Am I right that images are placed not regularly, i.e. not in the same grid every time? Otherwise you can simply search for the white vertical and horizontal lines (i.e. cv::countNonZero after thresholding), and use them for splitting. In other words I'm saying that you should provide more information about your task, an image will be enough.

Kirill Kornyakov gravatar imageKirill Kornyakov ( 2012-08-03 16:27:42 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2012-07-31 19:26:55 -0500

Hi Konstantin!

That sounds like fun!! Well... I am not aware of any OpenCV function that does specifically that, but here is what I would do:

  1. Get a binary mask of the whole image by thresholding on the white color. You should get a mask where appear in black all the pixels with "white color"(the color that corresponds with the borders) and pixels with any other color(that correspond to each of the sub-images) would appear in white.

  2. Improve the quality of the mask (the sub-images will likely also contain white pixels that would generate noise on the mask) by applying a combination of Opening and Closing operations (combinations of cv::erode and cv::dilate)

  3. Extract the contours of the white areas in the mask (cv::findContours). Hopefully you will get one contour per each sub-image.

  4. Calculate the bounding box of each detected contour (cv::boundingRect).

  5. For each bounding box, extract the original content and save it as a separate image.

Take a look at chapters 5 and 7 of the book:

"OpenCV 2 Computer Vision Application Programming Cookbook". Robert Laganiere.

Good luck!

edit flag offensive delete link more

Question Tools


Asked: 2012-07-31 13:45:20 -0500

Seen: 1,646 times

Last updated: Jul 31 '12