Splitting up image by color

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
Thomisback

Splitting up image by color

Post by Thomisback »

Hi,

I'm not very good at IM and I really need some help at this stage.

(For your information:
- Background of picture is always black
- Foreground text/shapes are always white
)

I'm trying to split up an image by color and then save all the images in separate files, e.g if I have:
Image

The command should then convert it to two separate files like this:
Image
And
Image

I can crop the image but then it will not find the actual shapes and cut it dynamically...

Any help is appreciated!
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Splitting up image by color

Post by anthony »

We had a similar problem given in the IM forum
viewtopic.php?f=1&t=11705

Their were a couple of solutions including the (slow) script
http://www.imagemagick.org/Usage/scripts/segment_image

This would divide your image into three masks, the background black part, and two foreground white objects.

An API of C version can be made much faster, as the shell script version needs
to process the image from scratch multiple times until no more parts are found. That can take some time.

Another similar script is
http://www.imagemagick.org/Usage/scripts/divide_vert

This looks for lines of constant horizontal color, and divided the image either
into segments of two or more colors, or alternating horizontal segments of constant and non-constant colors.

I developed it to allow text images to be sub-divided into separate 'lines'
of text so that the space between those lines can be adjusted, or removed.
It was also good for the automatic deletion of text label that was optionally present UNDER a photo.

It could easily be modified to...
  • horizontally divide up an image,
  • equally divide the solid color regions into two passing them to the previous and next segments.
  • use of a 'fuzz' factor or other statistical variance test to determine if a line is a single or multi-colored.
  • repeatally sub-divide vertically, then horizontally, then vertically again, and so on, until no more sub-divisions are found. Such a script
    would allow for automatic sub-division of GIF animation into sections, or into image blocks for a HTML image-map.
I'd love to see both these scripts converted into MagickCore Library functions
as image segmentation methods.


If you have any modifications, ideas or other solitions to this image segmentation problem, please include them here.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Thomisback

Re: Splitting up image by color

Post by Thomisback »

Hi, thanks for all the information I'm going to give it a try!
I'll keep you up to date!
Thanks a lot!
Thomisback

Re: Splitting up image by color

Post by Thomisback »

Hi,
Here is how I did it:
I made a script in PHP which would search for a vertical line of the same color and then use this to crop the original image to these new dimensions!
If anyone's needs help with a similar problem just PM me.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Splitting up image by color

Post by anthony »

That is simular to the divide_vert script though that splits along horizontal lines. You may like to look at that code for ideas on fast ways to figure out the number of colors exist in a line.

As I mentioned above a better solution would be to get a statistic
of the maximum color separation along each row/column.


These segmentation scripts really should be converted to core library C functions.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply