Sparse Color as a Fill Operator Tutorial (shepard) on win32?

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
pash
Posts: 8
Joined: 2011-04-26T07:59:16-07:00
Authentication code: 8675308

Sparse Color as a Fill Operator Tutorial (shepard) on win32?

Post by pash »

Hi,
just want to try out the "Sparse Color as a Fill Operator"-Tutorial from http://www.imagemagick.org/Usage/canvas/

There is this example:

Code: Select all

  convert shape.gif -channel A -morphology EdgeIn Diamond shape_edge_pixels.gif
  convert shape_edge_pixels.gif txt:- |\
    sed '1d; / 0) /d; s/:.* /,/;' | \
     convert shape_edge_pixels.gif -sparse-color shepards '@-' \
             shape_edge_in_lights.png
  convert shape_edge_in_lights.png shape.gif -composite shape_in_lights.png
The "sed" command in the above takes the Enumerated Text File Format, deletes the first header line and any line containing transparency, before re-formating it into a list of pixel coordinates and colors. That list is then 'pipelined' into "-sparse-color" using the special "@-" argument.

Yes the above is very tricky, but works. At least for a very small number of points. Eventually I hope to be able to simply input an image like the above and have "-sparse-color", do the above step automatically.
Image -> Image -> Image -> Image

But how to use this on win32 without SED?


Thank you
User avatar
whugemann
Posts: 289
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: Sparse Color as a Fill Operator Tutorial (shepard) on wi

Post by whugemann »

There is a Windows version of SED, see http://www.imagemagick.org/Usage/windows/#conversion. Anyway, the original bash script would need some modification when transferred to the Windows/DOS environment, which is also explained in the same section of this webpage.

Wolfgang Hugemann
Wolfgang Hugemann
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Sparse Color as a Fill Operator Tutorial (shepard) on wi

Post by anthony »

I hope you can resolve the problem, but sed in the above hole fille technique is only being used to extract the coordinates and colors. Any text processing method can be used.

HOWEVER the 'sed' in the example pointed to is NOT on windows, but a sed script under linux to 'mostly' convert a "convert" shell command into a form suitable for DOS use.


Future...

I am working on a more direct image "hole fill" operator. One that uses Diffusion rather than Shepards for the color fill algorithm. Diffusion has the property that it will not 'leak' across boundaries. As such you will not get a mix of two different colors from different holes. This may be linked to the +sparse-colors option (currently not used) or as a separate option.

Note that eventually hole fill methods and techniques will move from 'Canvas' secotion of IM Examples, to the end of 'Masking'.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
pash
Posts: 8
Joined: 2011-04-26T07:59:16-07:00
Authentication code: 8675308

Re: Sparse Color as a Fill Operator Tutorial (shepard) on wi

Post by pash »

Thank you. :O)

Is there a possibility to get that blurred color matching Background with a different technique?

What I have already done:

I'm putting images (IMG1) in different sizes on a fixed Background (BKG) of 1280x780 by shrinking them to the max of 1280 and place them by the golden rule if there smaller:

Code: Select all

+------------------------+
|             +------+   |
|             |      |   |
|  BKG        | IMG1 |   |
|             |      |   |
|             +------+   |
+------------------------+
This is all no Problem with the great imagemagick.
By now I'm generating the Background transparent put the img1 on it, blur everything extreme and use that as new background to put the img1 on to match the color of img1.

But blurring often got a color which often is gray, so I thought that the Sheppard would deliver a nicer fitting color for img1.

Any suggestion?

I only want a color matching background. :O)
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Sparse Color as a Fill Operator Tutorial (shepard) on wi

Post by anthony »

pash wrote:Thank you. :O)

Is there a possibility to get that blurred color matching Background with a different technique?
An alternative shepards method, which directly uses a image rather than coordinates, is presented in the same area of IM examples just a little further down. Two methods actually one slow and awkward (blurs) and one fast (using resize to do large blurs)
http://www.imagemagick.org/Usage/canvas/#sparse_blur

The resize method is recommended, and allows you to over-do the resize (when you do not know the input image size) without any real loss of speed.

Actually the coordinate method being used is WRONG! The coordinates should have had 0.5 added to each pixel coordinates to convert pixel locations to mathematical image coordinates. See...
http://www.imagemagick.org/Usage/distor ... oordinates



ASIDE: Note that the shepards method will 'leak' across edges and boundaries. As such one area surrounded by white, and another area surrounded by red, will fill with pink rather than just the white and red that directly surrounds the area. The solution is to use a 'Color Diffusion' (google about it) rather than Shepards. Color Diffusions is yet to be implemented. Though I have managed to do it in vanilla IM using variable blurs, it is NOT a nice or easy to understand or use solution.

A real and fast colour diffusion method is being developed. But waiting some other core library modifications that are being made to make updating of lower level image data handling easier to change and improve.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Sparse Color as a Fill Operator Tutorial (shepard) on wi

Post by anthony »

pash wrote:I only want a color matching background. :O)
See Thumbnails self framing (external)
http://www.imagemagick.org/Usage/thumbnails/#self_frame

You are right that blur will tend to go to grey, and will likely include any inside pixel color. You could however make all the internal pixels transparent, then do the blur and remove transparency before overlaying the image. That is similar to what sparse color is doing to grab just the edge pixels, but only needs a single blur.

The trick is making the insides transparent! and sparse-color examples also shows you how to do that!

When you get your solution, share it here.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
pash
Posts: 8
Joined: 2011-04-26T07:59:16-07:00
Authentication code: 8675308

Re: Sparse Color as a Fill Operator Tutorial (shepard) on wi

Post by pash »

Ok and thank you anthony, complex but just some quick try from my side:

I've got an Image on a transparent background:

http://image-upload.de/image/SSNBKx/f2fcb51c1b.png

but when I use:

Code: Select all

convert f2fcb51c1b.png -channel RGBA -blur 0x15 -alpha off  sparse.png
it won't blead over it's boundarys

result:

http://image-upload.de/image/H7dS9r/61c69d5195.png

PS: Later the day I will have a look at the thumb method and try that.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Sparse Color as a Fill Operator Tutorial (shepard) on wi

Post by anthony »

With such a large image however a single blur will not do the job as it will not extend its effects far enough. That is why it suddenly cuts off afetr extending the colors only so far.

I can see now why you were trying to use a sparse color fill..

Assuming you just want to replace the transparency with a average color. here is one way.
This generates a average color using -scale. It then uses distort to generate a tiled canvas from that
color (using the viewport setting that was saved previously)
http://www.imagemagick.org/Usage/distor ... t_viewport

Code: Select all

convert f2fcb51c1b.png -set option:distort:viewport %G \
         \( +clone -scale 1x1\! -alpha off \
            -filter point -distort SRT 0 +filter \
         \)   +swap -flatten  average_background.png
however as you said you get a dark gray background. -- too dark the image is not that dark!

Hmmm I have found a bug -scale is not handling transparency in images correctly!
Reporting it...

Using -resize will get a correct average color.

Code: Select all

 convert f2fcb51c1b.png -set option:distort:viewport %G \
         \( +clone -filter box -resize 1x1\! +filter -alpha off \
            -filter point -distort SRT 0 +filter \
         \)   +swap -flatten  average_background.png
Still very grey but not so dark...

So lets get the average of just the 3 pixels around the edge of the image

Code: Select all

convert f2fcb51c1b.png -set option:distort:viewport %G \
         \( +clone -channel A -morphology EdgeIn Square:3 +channel \
            -filter box -resize 1x1\! +filter -alpha off \
            -filter point -distort SRT 0 +filter \
         \) +swap -flatten  edge_background.png
A nice drak brick color. Much of the darkness here is from the black edge pixels on the right side of the image
Lets trim the 'bad' outside edge, then average the next three pixels into the image...

Code: Select all

convert f2fcb51c1b.png -set option:distort:viewport %G \
         \( +clone -channel A -morphology Erode Square:1\
            -morphology EdgeIn  Square:3 +channel \
            -filter box -resize 1x1\! +filter -alpha off \
            -filter point -distort SRT 0 +filter \
         \) +swap -flatten  edge_background_2.png
A much improved 'brick color'.

It seems your image edge is not very good!

Now for Shepards.... using resize bluring (but with the whole image)
Image largest dimension is 1280 which means we need at least 11 resizes but more will work too..

Code: Select all

  convert f2fcb51c1b.png -filter Box \
          \( +clone -resize 50% \)  \( +clone -resize 50% \)  \( +clone -resize 50% \) \
          \( +clone -resize 50% \)  \( +clone -resize 50% \)  \( +clone -resize 50% \) \
          \( +clone -resize 50% \)  \( +clone -resize 50% \)  \( +clone -resize 50% \) \
          \( +clone -resize 50% \)  \( +clone -resize 50% \)  \( +clone -resize 50% \) \
          -layers RemoveDups -filter Gaussian -resize 1280x853\! -reverse \
          -background None -flatten -alpha off    shepards_background.png
Note that the command is long, but it is quite FAST!
Note as I did not extract the edge; the average color is the image average.
But you get a shepards halo effect around the image.

This does not appear to be very strong due to the effect of color leaking from the inside pixels.
You can see that leakage of color near the 'black' window pane causing a dark halo effect just outside the image edge.

Your probably better off using average.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
pash
Posts: 8
Joined: 2011-04-26T07:59:16-07:00
Authentication code: 8675308

Re: Sparse Color as a Fill Operator Tutorial (shepard) on wi

Post by pash »

Wow, you're my personal hero! :O)


I love that color leakage and hope that this is not noted as a problem in imagemagick and wont be corrected in future releases!!

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

Re: Sparse Color as a Fill Operator Tutorial (shepard) on wi

Post by anthony »

The color leakage here is a consequence of using shepards with the full image.
I will look at including shepards as one type of hole fill method, though it will be up to the caller if it is restricted to just edges or to whole image. A diffuse hole-fill would not have color leakage.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply