Page 1 of 1

Coloration of black/white picture after resize

Posted: 2011-04-23T06:48:13-07:00
by guillaumech
Hello all.

Sorry for my english (I french).
I use IM to color a picture. This picture has a pgm format. (the picture : http://88.190.18.22/img.pgm)
I execute this command line : convert img.pgm -filter Mitchell -resize 1026x864! img2.pgm
After, I build my coloration legend :

Code: Select all

 convert -size 9x2 xc: -draw '
      fill #ffffff rectangle    0,0 9,2
      fill #8bff00 rectangle    1,0 9,2
      fill #858348 rectangle    2,0 9,2
      fill #ff1c00 rectangle    3,0 9,2
      fill #ff6e00 rectangle    4,0 9,2
      fill #006599 rectangle    5,0 9,2
      fill #4c9900 rectangle    6,0 9,2
      fill #990097 rectangle    7,0 9,2
      fill #636363 rectangle    8,0 9,2 ' legend.png
And, I use the -clut option for coloration : convert img2.pgm legend.png -interpolate Integer -clut img3.png

I obtain this picture : http://88.190.18.22/img3.png, and with values : http://88.190.18.22/img3Val.png

I've a problem. If I've 3 values as that : 3 6 6, the coloration pass by 5 values : 3 (4) (5) 6 6. Clearly seen on the image with the orange color that does all the edges so there is not value 4. How resolve this problem ? It's possible ?

Thinks a lot for you help.
Guillaume C.

Re: Coloration of black/white picture after resize

Posted: 2011-04-23T09:54:33-07:00
by fmw42
Anthony is the expert on this, but try -interpolate nearest-neighbor rather than integer (which does a floor()).

see http://www.imagemagick.org/script/comma ... nterpolate

Re: Coloration of black/white picture after resize

Posted: 2011-04-23T13:18:29-07:00
by guillaumech
Think fmw42 for your post. I try the nearest-neighbor interpolation, and I obtain this picture : http://88.190.18.22/img3ValNearest.png
I remplace values (4,5,6,7) by the value 7. As we see very well the problem. The coloration pass by all color. I find a method to color with the real values. If I've values 3 and 6, I've the red and green color, and not the red, orange, blue and green color.
But if I've values (3, 4, 5, 6), I'd like to have those colors : red, orange, blue and green color.

I hope I was clear enough in my explanations ... It's difficult to explain.

Thinks.

Re: Coloration of black/white picture after resize

Posted: 2011-04-23T13:49:48-07:00
by fmw42
guillaumech wrote:Hello all.

Sorry for my english (I french).
I use IM to color a picture. This picture has a pgm format. (the picture : http://88.190.18.22/img.pgm)
I execute this command line : convert img.pgm -filter Mitchell -resize 1026x864! img2.pgm
After, I build my coloration legend :

Code: Select all

 convert -size 9x2 xc: -draw '
      fill #ffffff rectangle    0,0 9,2
      fill #8bff00 rectangle    1,0 9,2
      fill #858348 rectangle    2,0 9,2
      fill #ff1c00 rectangle    3,0 9,2
      fill #ff6e00 rectangle    4,0 9,2
      fill #006599 rectangle    5,0 9,2
      fill #4c9900 rectangle    6,0 9,2
      fill #990097 rectangle    7,0 9,2
      fill #636363 rectangle    8,0 9,2 ' legend.png
And, I use the -clut option for coloration : convert img2.pgm legend.png -interpolate Integer -clut img3.png

I obtain this picture : http://88.190.18.22/img3.png, and with values : http://88.190.18.22/img3Val.png

I've a problem. If I've 3 values as that : 3 6 6, the coloration pass by 5 values : 3 (4) (5) 6 6. Clearly seen on the image with the orange color that does all the edges so there is not value 4. How resolve this problem ? It's possible ?

Thinks a lot for you help.
Guillaume C.
What version of IM are you using and what Q level and what platform?

Your img2.pgm has 13 values in it from the verbose info due to the use of -filter ... -resize. But your colormap has only 9 colors to span the full range of the 13 values.

Code: Select all

Image: img2.pgm
  Format: PGM (Portable graymap format (gray scale))
  Class: DirectClass
  Geometry: 1026x864+0+0
  Resolution: 72x72
  Print size: 14.25x12
  Units: Undefined
  Type: Grayscale
  Base type: Grayscale
  Endianess: Undefined
  Colorspace: RGB
  Depth: 4-bit
  Channel depth:
    gray: 4-bit
  Channel statistics:
    Gray:
      min: 0 (0)
      max: 12 (0.8)
      mean: 7.16206 (0.477471)
      standard deviation: 2.3595 (0.1573)
      kurtosis: 1.11911
      skewness: -0.523557
  Histogram:
     24544: (  0,  0,  0) #000000 black
      9393: ( 17, 17, 17) #111111 rgb(17,17,17)
      7332: ( 34, 34, 34) #222222 rgb(34,34,34)
      6457: ( 51, 51, 51) #333333 grey20
      6635: ( 68, 68, 68) #444444 rgb(68,68,68)
     20411: ( 85, 85, 85) #555555 rgb(85,85,85)
    371408: (102,102,102) #666666 grey40
     95559: (119,119,119) #777777 rgb(119,119,119)
     86931: (136,136,136) #888888 rgb(136,136,136)
     82539: (153,153,153) #999999 grey60
     88139: (170,170,170) #AAAAAA rgb(170,170,170)
     85554: (187,187,187) #BBBBBB rgb(187,187,187)
      1562: (204,204,204) #CCCCCC grey80
I don't fully understand your problem. But Anthony is the one who knows -clut best.

Have you tried -map (-remap) in place of -clut?

see http://www.imagemagick.org/Usage/quantize/#map

convert img2.pgm +dither -map legend.png result.png

Re: Coloration of black/white picture after resize

Posted: 2011-04-23T14:58:58-07:00
by guillaumech
I know, it's very diffult for explain.
I rebuild the legend file, and map
The legend :

Code: Select all

 convert -size 8x2 xc: -draw '
      fill #ffffff rectangle    0,0 8,2
      fill #8bff00 rectangle    1,0 8,2
      fill #858348 rectangle    2,0 8,2
      fill #ff1c00 rectangle    3,0 8,2
      fill #ff6e00 rectangle    4,0 8,2
      fill #006599 rectangle    5,0 8,2
      fill #4c9900 rectangle    6,0 8,2
      fill #990097 rectangle    7,0 8,2 ' legend.png
My img.pgm : http://88.190.18.22/img10.pgm
I excute : convert img10.pgm -filter Mitchell -resize 1026x864! img12.pgm, and obtain : http://88.190.18.22/img12.pgm

In my img10.pgm, I've values between 0 and 7. It's possible to have two non-contiguous values ​​near, as 2 4. The color about value 2 is gray, and orange for the value 4. Yet, I've the red color beteween.

After this commandline : convert img12.pgm legend.png -interpolate nearest-neighbor -clut img13Val.png
I've this map : http://88.190.18.22/img13Val.png

Is also visible with the color green, so we have no value for the green (1)

I try convert img2.pgm +dither -map legend.png result.png
But I don't understand really how use it. I obtain a map with one color.

If I've those values :
[2][3][4] =>[gray][red][orange]
but if I've those values, I'd like to have :
[2][4] =>[gray][orange]

Re: Coloration of black/white picture after resize

Posted: 2011-04-23T15:15:49-07:00
by fmw42
try

convert img10.pgm -scale 1026x864! img12.pgm

That should make the image larger without introducing new gray values. But it will be blocky.

By using -filter Mitchell -resize 1026x864! you introduce new values that are not in your original image and thus the colormap will fill them in with colors that you don't expect.

Re: Coloration of black/white picture after resize

Posted: 2011-04-23T15:53:04-07:00
by guillaumech
I use convert img10.pgm -scale 1026x864! img12.pgm, but I obtain this map : http://88.190.18.22/img23Val.png
Why the red color ? My legend is :
convert -size 8x2 xc: -draw '
fill #ffffff rectangle 0,0 8,2 (blanc)
fill #8bff00 rectangle 1,0 8,2 (green)
fill #858348 rectangle 2,0 8,2 (gray)
fill #ff1c00 rectangle 3,0 8,2 (red)
fill #ff6e00 rectangle 4,0 8,2 (orange)
On the map, there aren't 3 values, but there is red color, why ? And this scale mode does not smooth the edges of colors, it's square.
I'm complicated ... : (

thank you so much already

Re: Coloration of black/white picture after resize

Posted: 2011-04-23T16:15:29-07:00
by anthony
Greetings, and welcome back. I remember our previous disscussions about generating tempreture maps.
Obviously these are not tempreture maps seeing as you are now wanting discontinuous color ranges.

Basically by 'discontinous' I mean... In the 'PGM' image you have two values side-by-size 3 5 But the resize and CLUT is adding a small gradient between these two values and thus producing a color range value of 4 color between squares 3 and 5. You do not want a gradient, or continuious range of value, but a discontinuation.

The problem here is NOT the CLUT operation. That is just substituting values as expected. The problem is when you resize, which generates that gradient between the two values.

Resize is not the answer. What you want is a resize that adds no new colors (values) between points.



Solution 1: Use -scale or -sample (either will do for enlargements).

This will generate 'pixelated' squares of color, without generating any intervening colours between them.

This is also what you would get if you had simply used a -sparse-color volonoi on single points in the middle of each square. Which if I remember you were trying to do at tthe start of our previous discussion (email)

Obviously this looks horrible, and was presumably already rejected as a solution.


Solution 2: Generate a separate mask of each color and merge the masks.

That is one mask of all '5' values only, enlarge and threshold and edge smooth, that mask (say at 50%), after that color that mask to be the '5' color and transparency. Now overlay (flatten) all masks together.

This was looked at extensively in a older discussion topic on generating tiled maps of landscapes (for a game)
viewtopic.php?f=1&t=14250

The problem is that the masks may not cover the whole image, but leave small 'holes' or gaps between the different colors. If you are using tiles you could generate a blur overlap between tiling images, but you specified you want colors not tiles, and blur overlap would just result in your previous problem.

Something that wasn't available during that discussion is to 'hole fill' using a Voronoi method. See Shepards Sparse Hole Fill but use Voronoi instead of
Shepards. It is not particularly nice as you need to extract all edge pixels and convert them into a color point list.

if you have the very latest Imagemagick a NEW and fast direct alternative is...
convert image_with_holes.png -morphology Voronoi Octagon holes_filled.png

WARNING: the above is not 'offical' yet, and is only an experimental addition which will likely be called from some other Image processing option instead of morphology (which only provides the distance function it uses internally). It is a step for a much more advanced hole-filly method (Diffusion color fills as aposed to problems with a Shepards Color Fills, both related but differs in boundary handling)


Solution 3: Generate a solution 1, then look for and round off corners.

This was also suggested in the discussion for the previous solution, but difficult to implement. It also still generates a fairly squareish look at 3 color joins. However in may ways it is similar to (a more limited) general solution (see next)


Solution 4: Use a technique developed for video games known as Pixel scaling.

See http://en.wikipedia.org/wiki/Pixel_art_ ... algorithms

This has not been implemented in ImageMagick as yet, and won't be until we get some programmer willing (and has time) to do the work. I would love to see it added, and even have it on my ToDo but I have too many other To Do's that I never seem to get to before it.

Some algorithms for this include...
Scale2x http://scale2x.sourceforge.net/
HQ3x http://www.hiend3d.com/hq3x.html
You may also be interested in a related topic, for rotating without adding extra colors
http://info.sonicretro.org/RotSprite

Re: Coloration of black/white picture after resize

Posted: 2011-04-24T12:03:36-07:00
by guillaumech
Think Anthony !
I will continue development but I think you find the solution with the Scale2x library :)

Re: Coloration of black/white picture after resize

Posted: 2011-04-27T14:39:17-07:00
by guillaumech
I come to ask some questions again.

I tried the library Scale2X, it generates good results, but I have a problem with edges that are not smooth ...
Here are the commands I use:

Code: Select all

convert img.pgm legende.png -interpolate integer -clut img.png;
scalex -k 4 img.png img.png;
scalex -k 4 img.png img.png;
scalex -k 4 img.png img.png;
convert img.png -resize 1026x864! img.png;
img.png : http://88.190.18.22/img.pgm
legende.png : it is in the post above

I obtain this map : http://88.190.18.22/imgFin.png
There are strange things ... So I'd like to try the masks method, but, but I did not really understand how to use it. And explications in English are difficult for a French :)
I make a mask for each color? But how?
To merge masks, we have to use the option +matte?

Thank you for your explanations.
Guillaume C.

PS : I was install the last version of IM.

Re: Coloration of black/white picture after resize

Posted: 2011-04-28T18:28:54-07:00
by anthony
Yes that looks like scale2X results.

Remember scale2X were designed for converting game icons and sprites (very small rasters) to a size that is more usable (visible) on more modern higher resolutions displays. The 'pixel' quality while being smoother, was still important to give the overall 'retro' look. As such you get all octagonal angles, and not smooth curves, especially at higher X scaling. A scale 3X or 4X will be better, but only to a point.

There does appear to be a bug in your version at 'hash' points where the you get two diagonally linked colors.


To make a mask of each color in a loop could be tricky. In a way it is a segmentation issue, so one method would be to segment the image. Try the http://www.imagemagick.org/Usage/scripts/segment_image.

However this will generally 'overdo it' as separate areas will be segmented into separate images.

The other way is take your original PGM image (noraw or ascii mode) and use text processing to
for each color, make a copy, zero (black) all other colors, save, and loop.
You can then read in all the images, make black (zero) transparent and process for the enlargement,
say using a fuzzy Gaussian filter, Alpha channel threshold. The images can then be merged, and any holes filled in.

Try it using different amounts of Gaussian filter blurring.
http://www.imagemagick.org/Usage/resize/#filter_blur

A final small blur will smooth the edges, or you can vectorise the results to smooth it
http://www.imagemagick.org/Usage/transform/#edge_vector

I would keep transparency enabled until after the hole fill. after that you should have no problem.


Hmm alternatively start with your scale2x, separate colors, blur, threshold, merge and hole fill. It may produce a even better result.