F
F
Fedor2014-08-20 14:44:21
Programming
Fedor, 2014-08-20 14:44:21

How to split the contents of a monochrome image into separate files?

Actually, there is some image (bmp/jpg/png) which contains a number of objects with broken borders. Objects are located in the image in any way (they can be very close to each other), but do not intersect.
ce709da9f87840ccbdcf16c8318521c7.png
The task is to programmatically split this image into many other images, each of which will contain separate monochrome elements.
What algorithms can you recommend?
Maybe there is something in OpenCV that will allow you to find out the coordinates of the extreme points (x1, y1, x2, y2) and already using, for example, QImage-> copy , copy the selected fragment and save it from QImage (pass it to another component).
PS> On reflection, I came up with an option to enter data into a two-dimensional array (black color - 0, white - 1) and somehow select groups of zeros from it and write them down somewhere else. Accordingly, from somewhere again turn into a file and save (work further with it). What could be better than this option?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
F
Fedor, 2014-10-28
@krox

Since the answers are coming, I will write the option on which I stopped and implemented.
1. In the OpenCV library, the contour search algorithm is taken.
2. If the contour satisfies the conditions in terms of size (not noise and not too small), copy this contour to a new image
3. Using the available coordinates of the extreme points of the contour, simply cut the image out of the image
4. Save the result to a file.
Everything turned out to be quite simple.

_
_ _, 2014-08-20
@AMar4enko

www.fmwconcepts.com/imagemagick/multicrop/index.php

P
plasticmirror, 2014-08-20
@plasticmirror

uh... enumeration of the array of points, we find black color - we find the whole figure by breadth-first search - we throw it into a file, we mark the points with white, we repeat.
no black dots - output
p.s. it is written in 20 minutes, it does not pretend to be optimal in terms of speed, but the conditions do not imply this either ...

D
Don Kaban, 2014-08-21
@donkaban

For self-closed shapes (if the scanline drawn through the shape is broken), breadth-first search will not work. Right bottom figure, for example

V
Vitaly, 2014-09-15
@vipuhoff

The wave algorithm is suitable, the simplest algorithm can be described as follows:
select a black point, any, in any figure, by any method,
create a bitmap\image with dimensions equal to the original image.
use the wave algorithm to search for all black points starting from the one that was chosen,
draw all black points into the created bitmap at the same coordinates as in the original one, paint over the drawn ones with a non-black color
so with all the points according to the wave algorithm until the black ones run out
save the resulting bitmap
repeat the whole procedure until the original will not run out of black dots.
in terms of speed, the algorithm will not be very fast, but not very slow either, in 5-10 seconds for an HD picture.
the wave algorithm can be simplified to the simplest recursion, on the input the coordinates of the first point p
void voln(p){
make p-white in the original
make p-black in the created bitmap
for each black point p2 around the point p
voln(p2)
}
at the exit of the recursion the second picture is filled with the fragment, the point of which was fed to the input to the recursion

O
Odissey Nemo, 2014-10-28
@odissey_nemo

Any algorithm for filling a one-color raster polygon with any color is taken, starting from any of its points of a given color. They were prepared under DOS in any raster library (in the same Borland).
1. From left to right, from top to bottom, the first point of the desired color is searched. If not found, exit. Otherwise, 2.
2. The point is remembered for the future. Starting from the found point, the polygon is filled, including it with some working color (filling algorithm).
3. The bounding box (extents) of this found polygon (top Y is already known - as at the first point) is determined in the raster coordinate system. A raster of this size is created (or slightly expanded for nice borders). The found polygon is distilled into it.
4. The found polygon is saved to a file.
5. The found polygon is removed from the original image. Let's say, by the method of the same fill, but with the absence color. Or a variation of the XOR operation on the fill color.
6. Starting from the memorized point, the next point of the desired color is searched.
7. If found, go to step 2.
8. If not found, exit.
The search for extents (Bounding box, BB) remains unclear. The simplest thing is to scan the entire source raster each time, which is not comme il faut. Or somehow modify the fill algorithm - with extents memorized. Which is simple enough. And flooded and received extents at a time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question