Crop all areas with a certain color (get text out of colored areas)

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?".
ederhj
Posts: 11
Joined: 2015-08-30T02:57:13-07:00
Authentication code: 1151

Re: Crop all areas with a certain color (get text out of colored areas)

Post by ederhj »

fmw42 wrote:You can use the following to extract the largest the crop areas corresponding to the largest white areas from the s.png image. You will have too many due to the large white region at the bottom right. But you can then throw out the ones not needed or examine the bounding box coordinates for shapes that are longer in x than y. Skip the first one, which has color gray(0) and is the black background. Then get the next ones that are gray(255) until you read one that is gray(0) again. See http://magick.imagemagick.org/script/co ... onents.php

Code: Select all

convert s.png -define connected-components:verbose=true -connected-components 4 null:
Objects (id: bounding-box centroid area mean-color):
0: 885x1090+0+0 437.8,544.0 927407 gray(0)
286: 101x161+784+929 847.8,1024.5 9443 gray(255) <-- not likely what you want, not the right w/h aspect ratio
215: 176x32+278+359 365.2,373.9 4914 gray(255) <-- probably a good one
217: 193x29+524+384 618.3,398.1 4525 gray(255) <-- probably a good one
223: 130x35+361+544 425.3,561.1 3839 gray(255) <-- probably a good one
72: 207x28+469+170 575.2,182.4 3504 gray(255) <-- probably a good one
52: 157x25+239+115 316.9,126.7 3238 gray(255) <-- probably a good one
228: 83x26+347+636 387.0,648.5 1821 gray(255) <-- probably a good one
219: 84x25+136+421 177.2,432.4 1804 gray(255) <-- probably a good one
211: 80x26+139+228 177.3,240.4 1796 gray(255) <-- probably a good one
327: 17x21+836+939 843.5,950.1 168 gray(255) <-- probably too small and not the right w/h aspect ratio
505: 12x11+801+1045 807.2,1050.5 85 gray(255) <-- probably too small and not the right w/h aspect ratio
573: 26x8+774+1082 786.3,1086.6 75 gray(255) <-- probably too small and not the right w/h aspect ratio

96: 11x12+633+178 638.6,183.5 63 gray(0)
442: 12x13+793+1027 798.8,1033.4 63 gray(255)
377: 14x8+771+1000 777.9,1004.4 62 gray(255)
379: 9x7+804+1001 807.5,1004.3 55 gray(0)
...

And: How do I get the output above (with the areas) into a file.
It doesn't work if I use the ">" pipe.
There I get each pixel not the area.

Thank U
ederhj
Posts: 11
Joined: 2015-08-30T02:57:13-07:00
Authentication code: 1151

Re: Crop all areas with a certain color (get text out of colored areas)

Post by ederhj »

And: For my Example it doesn't work because of "too many objects".

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

Re: Crop all areas with a certain color (get text out of colored areas)

Post by fmw42 »

Are you on Q8 or Q16 compile of IM? Set the area threshold to avoid too many objects.

Was this not your image? If not, then post it to dropbox.com and put the URL here along with your processing. You cannot process connected components on an image without reducing the number of colors or binarizing it or you will get too many objects.

Why do you need to put it into a file. You can just put it into a string as per my code example earlier. Nevertheless, this works for me using the example from the link.

Code: Select all

convert objects.gif -define connected-components:verbose=true -connected-components 4 result.gif > tmp.txt
It probably won't redirect (not pipe) to the file due to "too many objects". So add the area-threshold to the command using

-define connected-components:area-threshold=XXX

Where XXX is some minimum area (pixel count in the region) to keep and it will throw out all regions smaller than XXX
ederhj
Posts: 11
Joined: 2015-08-30T02:57:13-07:00
Authentication code: 1151

Re: Crop all areas with a certain color (get text out of colored areas)

Post by ederhj »

Hello again,

I'm no able to get it work.
To recapitulate what I want:
First of all I have a File (https://www.dropbox.com/s/99y4on2ber1ln ... t.jpg?dl=0), where certain text-areas are marked with a colored marker.
This marked areas I want to user for renaming the file.
So what do I have in my mind.

1. I have to crop out these areas and save the crops in new files
2. these files will be OCRed, that means, I search for texts in the JPG an get the text out of it
3. I rename the files

The points 2 and 3 are no problem. But the number 1 I can't manage.

So please help me to get the CROP out of the INPUT.JPG so I get OUTPUT_GREEN1.JPG, OUTPUT_GREEN2.JPG an so on.

Please help me with the command "CONVERT" without scripting, because I use for scripting on windows AutoIt3.
Please give me only logical sequences of commands you I get the output.jpgs from the input.jpgs.

Sorry. Buit I don't understand die syntax and the special commands of CONVERT.

Thank U again.

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

Re: Crop all areas with a certain color (get text out of colored areas)

Post by fmw42 »

Did you convert the file to binary as per user snibgo's commands earlier? You must do that first. Use the binary file for the connected-components to get the crop areas, then use those crop areas on your original image. See his code earlier (viewtopic.php?f=1&t=28280#p125389) and my code (viewtopic.php?f=1&t=28280#p125526) that used his resulting binary image.
Post Reply