How to split picture according to another picture?

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
Alcareru

How to split picture according to another picture?

Post by Alcareru »

Hello once again!

I would like to know if it's possible to split a picture according to another picture of same size. Let's call these pictures, the actual picture and the mask picture. The mask picture has solid colored areas on a transparent background. Let's call these areas submasks and their colors as submask colors. The colored areas represent areas in the actual picture that contain only interesting things in that color. So what I would like to do is split the actual picture into subpictures by using the submasks as selection tools.

In addition I want to transform these subpictures into bitonal ones. That should be done by converting everything else than the corresponding submask color in the subpicture to white and the subpicture color itself to black. It's probably reasonable constraint if we assume the submasks to represent only rectangular shapes.

Here's a few pictures to illustrate what I mean [EDIT: Link now works]:
http://www.cs.helsinki.fi/u/tglehto/tmp/IM/

I did already find something from the forums concerning this thing, especially close seems to be these:
viewtopic.php?f=1&t=10377&p=32751&hilit ... age#p32751
http://www.imagemagick.org/Usage/scripts/segment_image

I'm starting to get a hang of this script, but I wonder is this really the only way of doing this? It takes forever to go through all the pixels in the picture. What if I'd do something like:
Loop untill image is empty:
1. check: only corners of the image and collect pixel and color data if they are not transparent
2. trim
3. if image not changed 4 and 5 else go repeat 1
4. check: only edeges of the image and collect pixel and color data if they are not transparent, make such edgeconnected areas transparent
5. try to trim again
(maybe we need to also store information as to what corner pixels we got from each continuous area: NW, NE, SE, SW)

Btw is it even possible to get the pixel location data in relation to the original picture?

Myabe through the percieved virtual canvases? How to use them? What does this mean?
The page or virtual canvas information of the image is preserved allowing you to extract the result of the -trim operation from the image. Use a +repage to remove the virtual canvas page information if it is unwanted.
Ok. Now if we know the colors and their locaisons (at least 1 pixel per color), then we can go through each of those coordinates and color in the original picuter, removing everything else (read=making it white) from the picture excpet the continuous area of that color connected to that pixel. Also change the color of that area to black. Store all these intermadiate pictures for later use as masks (also don't forget the colordata related to that mask). For each pixel to process of coures always use the original picture.

One at a time apply the masks on the actual picture and make the color transformation as I previously dicribed. Maybe also tirm the results as well.

Does that sound insane or brilliang :)?

In the script I mentioned there we're some parts that someone could enlighten a bit:
I don't quite understand the -write miff:- thingy, could someone explain it a bit? In my pathetic attemps to understan them I tried to run this:
convert example_img_mask.tif -write miff:- -fuzz 0% -fill none -draw matte 5x5+20+20 floodfill example_img_test.tif
and made the windows cmd print cool smileyfaces, arrows, symbols and such and the all so beloved pc beeper to play mozart.. :D
If I remove the -write miff:- part and quote draw-triggers values. It removed the background color from the image. How did the coordinates have anything to do with this? It seems to do the same thing no matter what they are?

This line is a total mystery for me.
convert miff:- -reverse -layers OptimizeTransparency -reverse "$2"
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to split picture according to another picture?

Post by anthony »

This has two steps. split the mask into multiple pieces, (segmentation)
then cut out each piece from the original image (masking).

The first step see
viewtopic.php?f=1&t=11705

The script http://www.imagemagick.org/Usage/scripts/segment_image
I developed in response to the above can be modified to extract each colored
area as a separate masking image.

for the second see adding a transparency mask...
http://www.imagemagick.org/Usage/compose/#copyopacity

Rubbleweb also does thing with images and color areas masks for web page image selection.
That is the user clicks on the image in some location. A CGi takes that location and looks up the color in the mask image to determine what page to jump the user to. Very niffy.

NOTE other than the segmentation (which IM is still very poor on though is on my TODO list), a sequence of masks can be extracted from a single image, all in the one command!

convert actual_image.png null: mask1.png mask2.png mask3.png \
+matt -compose CopyOpacity -layers composite -trim +repage \
images-%d.png

If the masks are not white on black, but a shape on transparency, replace
+matt -compose CopyOpacity
with -compose DstIn

See http://www.imagemagick.org/Usage/compose/#dstin

Using -layers composite will also let you use a mask which is a layer image. That is a smaller image with virtual canvas (-page) offset, though I do not think this would work well for CopyOpacity, It will work find for DstIn
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Alcareru

Re: How to split picture according to another picture?

Post by Alcareru »

Thanks anthony once again. This forum is the best ever! :)
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to split picture according to another picture?

Post by anthony »

So what did you end up doing? At least for generating the separate masking images.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Alcareru

Re: How to split picture according to another picture?

Post by Alcareru »

For now.. nothing :)
I managed to get things working well enough without the the whole process (meaning my OCR app fails to read one letter correctly because of 2 letters are too close, but it can deduct what was actually written so it doesn't matter). Anyways I will be digging back to this because it interests me even if it won't be a part of my almost ready pile of python code (I'd put it in there, but the features I had planned would probably be too difficult to use for the end users). I'll be getting back to this and on my spare time I will forge this script into more general form python script that eases the process of extracting text data from pictures that remain reasonably static... I'm hoping that means next week :).
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to split picture according to another picture?

Post by anthony »

Well let us know how it goes. It would be nice to here from users how they actually solve problems, such as this.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply