[HELP] Split closed black frames from an image

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Zeebadi

[HELP] Split closed black frames from an image

Post by Zeebadi »

Hello,

I need your help in my problem, I do not know if its too difficult or not ... which is ...

I have a black and white image, that have some open text and some closed black (rectangular) frames containing some text inside, I need to to extract all closed black frames into separated files.


Here you can see an example image: http://www.blue.ps/page22r.jpg

so can you please help me in this issue ?

Many worm regards
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: [HELP] Split closed black frames from an image

Post by snibgo »

Quite simple in principle, but tedious.

1. Create nearly square masks, eg 5x4 pixels, with black square and one white edge. Compare these with page22r.jpg to find the container lines. (Using one exactly square mask with all pixels black will create false positives.) The result will have white for the lines, and black elsewhere. Call this "containers.png".

2. Find the first black pixel in containers.png. Floodfill with transparent.

3. Composite this with page22r.jpg to isolate one article.

4. Floodfill the pixel found in (2) with red.

5. Repeat from 2 until no more black pixels.

One of the results will be the page that is outside all the black containers.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [HELP] Split closed black frames from an image

Post by fmw42 »

try this.

First floodfill any white between your boxes with some color other than white or black and try to floodfill all white outside these boxes. Then apply my just fixed multicrop as follows:

convert page22r.jpg -fuzz 5% -fill antiquewhite -draw "color 10,100 floodfill" page22r_aw.png

Note coordinates 10,100 are under the title area and are a location of white that should floodfill as needed.

multicrop page22r_aw.png page22r_aw_crop.png

this will get more areas that you want -- some false positives, but should get those you need. If not increase the grid some. The non-white background color that was floodfilled may show around your boxes, but you can batch process them to replace the background color with white again.
Zeebadi

Re: [HELP] Split closed black frames from an image

Post by Zeebadi »

Hello,

snibgo thank you so much, but I am not very familiar with the small things of imagemagick, I've started using it 48 hours ago !

----------------------------------

Dear fmw42, it seems that you are the one :)

I've tried your solution, and its working very great, it produces some false positives, but no problem, I can remove them by comparing the size to 50 KB for example.

As you mentioned about the blue overlay color, like this

Image

I tried just to replace "blue" with "white" (convert page12c.png -fuzz 5% -fill white -draw "color 0,0 floodfill" page12c_blue.png) and it worked very smoothly like this

Image

Thank you very much, and I appreciate your time very much :)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [HELP] Split closed black frames from an image

Post by fmw42 »

Download the latest version of multicrop. I fixed the issue with the blue. See my instructions above.
Zeebadi

Re: [HELP] Split closed black frames from an image

Post by Zeebadi »

yes yes, I did, its working very fine :)

thanks
parrones

Re: [HELP] Split closed black frames from an image

Post by parrones »

Hello Fred,

I have a similar problem and tried the solution you presented.
The image I have is this: http://www.tecno2.com.br/pagina001a.png

I need to identify the coordinates of all rectangles I see in the image... I think that the problem is caused because some corner don't "close"...

Do you have some tip that I can try to solve this problem, or even try a different approach ??

Thanks,

Marcello
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [HELP] Split closed black frames from an image

Post by fmw42 »

parrones wrote:Hello Fred,

I have a similar problem and tried the solution you presented.
The image I have is this: http://www.tecno2.com.br/pagina001a.png

I need to identify the coordinates of all rectangles I see in the image... I think that the problem is caused because some corner don't "close"...

Do you have some tip that I can try to solve this problem, or even try a different approach ??

Thanks,

Marcello
The following will close all rectangles. However, my script will not help you with your problem. I don't think the black lines are wide enough to separate the white rectanges and anyway you want the intersections of the black lines. At this point I don't know what to suggests. IM does not have any tools to find intersections of lines.

convert pagina001a.png -morphology open:10 square pagina001a_open10.png

You might be able to write a script to floodfill each closed rectangle with different colors. Then you can isolate each color by making all the others black or transparent and crop out each colored rectangle to get is dimensions. Best I can suggest. My script, separate, will do this.

#Threshold the image so all lines are completely black
convert pagina001a_open10.png -negate -threshold 0 -negate pagina001a_open10_thresh.png

# run script to generate colored rectangles as output with -0, -1 ... -12 appended before the suffix
separate -m 6 pagina001a_open10_thresh.png pagina001a_open10_thresh_separate.png

The output images will be the size of the input with black everywhere except for a colored rectangle for one of your areas.

After you get these, then use -trim (without +repage) to extract each rectangle without the black. Then do identify on the resulting image and the virtual canvas will tell you the offset coords for the upper left corner of the colored area relative to the larger black image. The size will then allow you to compute the bottom right corner or all the corners, if you want.

Fred
parrones

Re: [HELP] Split closed black frames from an image

Post by parrones »

Fred,

Thank you....
Great tips...

I followed all steps.. but I'm having a problem to trim the image...

I'm using the following commad:

convert pagina001a_open10_thresh_separate-1.png -bordercolor black -border 1x1 -trim pagina001a_open10_thresh_separate-1_trimmed.png

The original image has black and red colors....
The destination has same colors....

Can you help me with that ??

Thanks again...
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [HELP] Split closed black frames from an image

Post by fmw42 »

Apparently black is not pure black (and I will look into this), but simply use -black-threshold to make it black and add a positive border of black around the whole image before trimming.

convert pagina001a_open10_thresh_separate-1.png -black-threshold 5% -bordercolor black -border 5x5 -trim pagina001a_open10_thresh_separate-1_trim.png


Or you can use -fuzz XX%

convert pagina001a_open10_thresh_separate-1.png -fuzz 5% -bordercolor black -border 5x5 -trim pagina001a_open10_thresh_separate-1_trim.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [HELP] Split closed black frames from an image

Post by fmw42 »

I have modified my script, separate, to allow for optional trimming to the bounding box of each object and also optionally report the image size and offset from the original.
parrones

Re: [HELP] Split closed black frames from an image

Post by parrones »

Fred,

Thanks... worked very well...

Just a problem... the separate script is a bit slow to perform this operation.
It's taking 1 minute to perform all commands...

Is there a way to optimize that ??
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [HELP] Split closed black frames from an image

Post by fmw42 »

parrones wrote:Fred,

It's taking 1 minute to perform all commands...

Is there a way to optimize that ??

Possibly. It has to search the whole image for a white pixel each time it looks for a rectangle. The other way that I used in my multicrop script was to search a sparse array of locations rather than use a compare search of the whole image to find any white spaces. But I don't have the time right now to rewrite the script that way. However, I am not sure this is the limiting factor. It may just be all the temp files I have to create and your image is rather large. I would have to do some research to figure out where the time is being spent.

You could get a faster processor with mutliple processors. Or someone could write the script into a true IM function.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [HELP] Split closed black frames from an image

Post by fmw42 »

I have looked at the separate script and the time looks to be spent finding the rectangles using the compare function (which has to check every pixel in the image for each rectangle it needs to find). I can modify the script to allow an optional faster method of testing only at points on a sparse grid. But if the grid is not dense enough it will miss some of your rectangles.

How important is it for you to have this run faster? Do you have a lot of these to process? Or is this just a one time image that has to be processed?

Anyone else need this to be faster and willing to use the grid method.

Fred
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [HELP] Split closed black frames from an image

Post by fmw42 »

I have updated the separate script to allow the optional use of a grid search rather than a precise search. This increases speed by up to about 2x. But if a good choice of grid spacing is not chosen, then either it will be slower than optimum (possibly as slow or slower than the precise method) or it may miss some features.

Fred
Post Reply