Extract similar parts of 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
defied

Extract similar parts of an image

Post by defied »

Hello,

I'm looking for a way to extract similar parts from an image and save them so I can later restore the original image again.
Suppose I have an image with several people on them, I'd like to extract the faces.

Image

If you look at the example above, you see that they somehow extract the face and put an animation below the face.

Is this possible with imagemagick?
defied

Re: Extract similar parts of an image

Post by defied »

Let's say I have this image:
Image

I want to be able to divide it in parts which have the same color, later I want to put all the different parts together to form the original image again.


Image Image
Image

What I did was:

Code: Select all

convert pic.jpg -blur 0x3 +dither -colors 3 blend.jpg
Leaving me with:
Image

I then extract a common color in blend.jpg as an example

Code: Select all

convert blend.jpg -format %c histogram:info:-

Code: Select all

convert blend.jpg -transparent "#1E1915" -fuzz 5% mask.gif
Image

Code: Select all

convert mask.gif -channel alpha -negate -separate maska.gif
Image

Code: Select all

composite -compose CopyOpacity maska.gif pic.jpg done.gif   
Image

As you can see there is a lot of noise, how could I remove this?

Is there a better/more accurate way to divide an image into pieces?
The reason I want this is because I want users to be able to fill their clothes or a background with glitters.

Thank you
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Extract similar parts of an image

Post by Bonzo »

You could try modifying this code to get the colours:

Code: Select all

// create a new image with everything transparent apart from the selected colour. This image must be saved as a png due to the transparency.
exec("/usr/local/bin/convert 2_1124623409.jpg -matte \( +clone -fuzz 33% -transparent rgb\(38,134,71\) \) -compose DstOut -composite output_hat.png");
http://www.rubblewebs.co.uk/imagemagick ... =green.jpg
defied

Re: Extract similar parts of an image

Post by defied »

Bonzo thanks for the help, your method did help a lot.

Image

But now the problem is, if I put this on top of the original image and add javascript to detect what "image" was clicked, it will always be the top one because it's the entire image (including transparency).

So I need to find a way to crop the image above to only show what matters (remove all transparency/dirt) and be able to still position it correctly over the original image.

I remember

Code: Select all

 convert noisy.jpg  -virtual-pixel edge -blur 0x10 -fuzz 15% -trim  info:
from the documentation, maybe that will do it, I'll give it a try.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Extract similar parts of an image

Post by anthony »

What you are doing here is known in image research as Segmentation...
That is dividing an image up basied on simularities in color, texture, and pre-defined shapes.

This is a very very hard subject and one that IM does not provide a good interface too at this time. However I will give you some info,

FIRST.. Color quantization (using -colors) is designed to find the best colors for color reduction and dithering. It is not designed to find areas of simular colors.

SECOND... the first stage of separation basied on colors is in IM as a -segment
operator. Unformtuatally it does not appear to work well, and then only on very large images, not the small ones you typically use in testing operators.

This option only does segmentation of the image in terms of colors, with some color counting attributes.

If you are interested in segmentation You can look at..
Leptonica graphics library Documentation --- Color Segmentation
http://www.leptonica.com/color-segmentation.html

Whcih seems to do a better job that the IM -segment operator.

Also google for other pages, and contribute your findings to this forum wit ha subject of image segmentation.


There is another aspect of segmentation. Cutting images up into vertical and horizontal slices, separating it on lines or columns of constant color.
A usful subset of segmentation, which I places into script and hopefully will get built into IM at some point in the future..
http://www.imagemagick.org/Usage/scripts/divide_vert

I was hoping to eventually use this to 'auto-divide' GIF animations into separate animated parts. See IM Examples... Splitting up an Animation
http://www.imagemagick.org/Usage/anim_mods/#split
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply