Few questions

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?".
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Few questions

Post by VanGog » 2012-05-19T12:40:06-07:00

I look for some functions.

1) How can I expand and contract "selection" (PS menu Selection/Modify/Expand or Contract) ... I mean I would like to select routes and streets from image (light white or light gray) and fill them with white color; the rest is black. So I would like to extend the white +3 and contract it back -3.

2) Does IM has some algorithm to detect amount of pixels in a group of pixels? PS has function magic wend, so when you click pixel of some color, all nearby pixels are selected. And I need to detect amount of these pixels because I would like to remove the pixels (fill them black) if the amount is less than eg. 150. In my case the image is B/W.

3) Then I want to make relief (is it right word?) to get only the edges of the selection. Therefor I look for filter function like in PS - its called "Glowing edges" under Filter/Style menu.

4) Next thing, I would like to generate grid, where I would define color, count of lines and width if lines.

5) can you help me to simulate a light of streetlight shining in the night? For example if I would darken daylight image of aerial photo, I would like to place a streetlight there. I know I can use blending, but would need some idea how to create a cycle, with a gradient mask (also cycle) that will go to outwards (white inside, black outside).
I have idea. When I have the selection of the streets and routes (white mask on black background) I could spread the white pixels out. Something like blur function but only for white color ant lighten effect would be used. So this way I would generate light mask. Then I need dark background (almost black) for the places where is not any light. Then I need new layer with dark image of the town and I need to convert it to orange tones. Like the light of night city:
http://upload.wikimedia.org/wikipedia/c ... 007%29.jpg
So now I would just connected the mask to get similar effect like in the link.

So my questions here:
A) how to spread the white pixels
B) how to generate the orange light over the dark image. I think I should darken the image with -level and then create new layer with the original photo and change the channel settings. And overlay.

User avatar
fmw42
Posts: 23429
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Few questions

Post by fmw42 » 2012-05-19T16:13:30-07:00

VanGog wrote:I look for some functions.

1) How can I expand and contract "selection" (PS menu Selection/Modify/Expand or Contract) ... I mean I would like to select routes and streets from image (light white or light gray) and fill them with white color; the rest is black. So I would like to extend the white +3 and contract it back -3.
Not directly. What you can do is use -fuzz ... -floodfill at some pixel coordinates. Then convert the result to binary as a mask. Then blur the mask and threshold to make it bigger or smaller.

see
http://www.imagemagick.org/Usage/color_ ... #floodfill
http://www.imagemagick.org/Usage/draw/#color
http://www.imagemagick.org/script/comma ... 370r1#blur
http://www.imagemagick.org/script/comma ... #threshold
VanGog wrote:2) Does IM has some algorithm to detect amount of pixels in a group of pixels? PS has function magic wend, so when you click pixel of some color, all nearby pixels are selected. And I need to detect amount of these pixels because I would like to remove the pixels (fill them black) if the amount is less than eg. 150. In my case the image is B/W.
The closest is to use -fuzz ... -floodfill or -fuzz -draw "color coords floodfill" Then make the result binary as a mask.

see same links above

If you are on Linux, Mac or Windows with Cygwin, you can use my script, magicwand, at the link below.

With regard to the number of pixels, once you get the binary mask, the mean value will tell you how many pixels it contains.

convert maskimage -format "%[fx:mean*w*h]" info:
VanGog wrote:3) Then I want to make relief (is it right word?) to get only the edges of the selection. Therefor I look for filter function like in PS - its called "Glowing edges" under Filter/Style menu.
see -edge
http://www.imagemagick.org/script/comma ... s.php#edge

or -morphology convolve laplacian
http://www.imagemagick.org/Usage/convol ... et_kernels


VanGog wrote:4) Next thing, I would like to generate grid, where I would define color, count of lines and width if lines.
IM does not have a grid function. You would have to write a script to draw multiply lines where you want them. see -draw
http://www.imagemagick.org/Usage/draw/#primitives

Again if you are on Linux, Mac or Windows with Cygwin, you can try my script, grid, linked below.
VanGog wrote:5) can you help me to simulate a light of streetlight shining in the night? For example if I would darken daylight image of aerial photo, I would like to place a streetlight there. I know I can use blending, but would need some idea how to create a cycle, with a gradient mask (also cycle) that will go to outwards (white inside, black outside).
I have idea. When I have the selection of the streets and routes (white mask on black background) I could spread the white pixels out. Something like blur function but only for white color ant lighten effect would be used. So this way I would generate light mask. Then I need dark background (almost black) for the places where is not any light. Then I need new layer with dark image of the town and I need to convert it to orange tones. Like the light of night city:
http://upload.wikimedia.org/wikipedia/c ... 07%29.jpg
So now I would just connected the mask to get similar effect like in the link.
Here I am not really sure I understand what you want. Can you provide examples?

Also look at my script, glow or halo.
VanGog wrote:So my questions here:
A) how to spread the white pixels
B) how to generate the orange light over the dark image. I think I should darken the image with -level and then create new layer with the original photo and change the channel settings. And overlay.

Again, it would be best if you could provide input and output examples. Otherwise, it is hard to know what you really want to do.

VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Few questions

Post by VanGog » 2012-05-20T01:57:05-07:00

Ad 2) If I would want to write script to detect amount of pixels and decide if to fill it with black, I would probably need to do these steps:

After the B/W mask was expanded and contracted and thresholded I need to print all white pixels. I expect that the pixels are printed from upper left to right and to bottom. Then I would start to loop through all white pixels. In every cycle of the loop I would test if the pixel is not red and is not blue. If it is not red, then I would fill it with red color. Then I would make new print - output amount of red color in image. I would decide if I want to fill the area black or blue. (Black if the amount is small; blue if the amount is good). Now I break the loop and repeat whole process.

On the end of the process I will change blue to white.

Do I need to use -fill to detect amount of pixels of some color?
Do I must create new mask to generate the count of pixels of specific color?
It looks much complicated to get acceptable performance.
Does the command convert maskimage -format "%[fx:mean*w*h]" info: work only for BW mask?
Maybe I should just do it like this: After I fill the group of pixels red, then I write it to disk. Then I fill create new BW mask by -fill and get the information as you do in this command.
Or like this: Instead of using BW mask, to use RGB so the red should be in red channel and I could extract red information from the red channel? Instead of Blue and white to use Green and blue...

I don't know, I need help.

User avatar
glennrp
Posts: 1163
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Few questions

Post by glennrp » 2012-05-20T10:38:24-07:00

>IM does not have a grid function

I've wanted a grid function several times in the past. It would be a nice
"draw" option to add. One thing I've done is to crop the image into
tiles (convert image -crop 100x100 +adjoin tile-%02d.ppm) and then use "mogrify
-frame 1x1 tile*.ppm" to add a frame around each tile, then append the tiles
from each row and then append the rows. In effect this stretches the
image slightly to accommodate the 2-pixel-wide grid lines. You could
automate keeping track of which tiles go in which rows, but I just
did it manually in this demonstration:
  • convert logo: -crop 200x200 +adjoin tile-%02d.ppm
    mogrify -frame 1x1 tile*.ppm
    convert +append tile-0[0123].ppm row0.ppm
    convert +append tile-0[4567].ppm row1.ppm
    convert +append tile-0[89].ppm tile-1?.ppm row2.ppm
    convert -append row?.ppm grid.ppm
Otherwise, just using convert with a bunch of "draw line" options
will work as well without stretching the image. I'd use a semitransparent
fill color for the lines in case something important would be
overwritten.

User avatar
fmw42
Posts: 23429
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Few questions

Post by fmw42 » 2012-05-20T11:10:03-07:00

Does the command convert maskimage -format "%[fx:mean*w*h]" info: work only for BW mask?
No it can be used on any image, but it will only give the number of pixels correctly if the image is b/w.

VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Few questions

Post by VanGog » 2012-05-20T11:50:16-07:00

fmw42 wrote: No it can be used on any image, but it will only give the number of pixels correctly if the image is b/w.
And the command does not count black pixels in BW mask? It behaves like the black pixels there are not?

User avatar
fmw42
Posts: 23429
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Few questions

Post by fmw42 » 2012-05-20T12:24:09-07:00

VanGog wrote:
fmw42 wrote: No it can be used on any image, but it will only give the number of pixels correctly if the image is b/w.
And the command does not count black pixels in BW mask? It behaves like the black pixels there are not?
Yes, that is the case, because the mean value is in the range 0-1. So it acts as a fraction of the total number of pixels that are white. But note, that unless your region is isolated and no other white in the mask, then it will count all white pixels. So if you want to remove small outliers, your can use -morphology to do that on the mask before computing the number of white pixels.

You can also take the mask and get coordinates of all white pixels.

convert maskimage txt: | grep "white"

VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Few questions

Post by VanGog » 2012-05-20T12:33:49-07:00

fmw42 wrote:
VanGog wrote:But note, that unless your region is isolated and no other white in the mask, then it will count all white pixels. So if you want to remove small outliers, your can use -morphology to do that on the mask before computing the number of white pixels.

You can also take the mask and get coordinates of all white pixels.

convert maskimage txt: | grep "white"
Language problem here. What does mean the word "outliers"? And isolated? Do you speak about hues of gray? I do not understand the 1st sentence of citation. So does it count the gray and hues of white or not? You spoke about threshold and now about -morphology.

User avatar
fmw42
Posts: 23429
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Few questions

Post by fmw42 » 2012-05-20T12:56:01-07:00

VanGog wrote:
fmw42 wrote:
VanGog wrote:But note, that unless your region is isolated and no other white in the mask, then it will count all white pixels. So if you want to remove small outliers, your can use -morphology to do that on the mask before computing the number of white pixels.

You can also take the mask and get coordinates of all white pixels.

convert maskimage txt: | grep "white"
Language problem here. What does mean the word "outliers"? And isolated? Do you speak about hues of gray? I do not understand the 1st sentence of citation. So does it count the gray and hues of white or not? You spoke about threshold and now about -morphology.

Isolate means all together in one group. Outliers means small regions or single pixels that far away from the main group separated by large black regions from the main group of white.

It has nothing to do with grays or hues. The mask must be all black and white, no gray.

VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Few questions

Post by VanGog » 2012-05-20T13:49:19-07:00

Now it is more clear. I will read about the morphology tomorrow
http://www.imagemagick.org/Usage/morphology/
so I will continue later.

User avatar
fmw42
Posts: 23429
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Few questions

Post by fmw42 » 2012-05-20T14:28:21-07:00

VanGog wrote:Now it is more clear. I will read about the morphology tomorrow
http://www.imagemagick.org/Usage/morphology/
so I will continue later.
-morphology open/close will remove small spots depending upon whether black or white

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

Re: Few questions

Post by anthony » 2012-05-21T22:29:43-07:00

glennrp wrote:>IM does not have a grid function

I've wanted a grid function several times in the past. It would be a nice
"draw" option to add.
Actually so have I, and just recently too.

I have been thinking that a 'grid' function (or expand pixels to bordered tiles) type function (a bit like 'scale' but adding a border around each square) would be a nice addition.


I did this about two years ago in a "kernal2image" script, See
http://www.imagemagick.org/Usage/morpho ... rnel2image
But the way I did it their was to crop into pixels, and splice in some edges (top and left sides), rejoin, and splice along bottom right edge. It is slow, tricky, and awkward, but it worked.
(in script look for the comment "Add inter-pixel gaps and colors")

Recentally I had another project whcih needed to do the same thing. But was a little smarter about it.
What I did this time is generate a 'block' image, tile it, and use it as a mask for a scaled image.

Test image...

Code: Select all

   convert rose: -crop 10x10+12+20 grid_input.png
Image

Now generate an enlarged 'pixelized grid' (white borders using 'screen' compose) of that image

Code: Select all

convert -size 10x10 xc: -draw 'rectangle 1,1 9,9' -write mpr:block +delete \
            grid_input.png -scale 1000% -size 101x101 tile:mpr:block \
           +swap  -compose screen -composite grid_blocks.png
Image

Note the first 'size is the scale amount (10x), while grid tile size is scale*image+1
The swap is so I used the 'tiled' image for the size, rather than the scaled image (whcih is one pixel smaller in size)

It is not exactly 'pretty' but it works.

Same technique can make circular 'spots'

Code: Select all

  convert -size 10x10 xc: -draw 'circle 5,5 1,3' -negate -write mpr:spot +delete \
              grid_input.png -scale 1000% -size 101x101 tile:mpr:spot \
              -compose multiply -composite grid_spots.png
Image

I have been thinking that with the right interpolation method I could make use of the weirdness of EWA box resampling with too small a support...
http://www.imagemagick.org/Usage/resize ... terpolated

Update: I have added examples of 'gridding'. (give it an hour or two to appear online - when images appear above)
http://www.imagemagick.org/Usage/transform/#gridding
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
http://www.imagemagick.org/Usage/

VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Few questions

Post by VanGog » 2012-05-22T01:34:10-07:00

Yet I have some idea with splice command, in the developers section...

EDIT by moderator: viewtopic.php?f=2&t=21060

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

Re: Few questions

Post by anthony » 2012-05-27T16:10:08-07:00

Question on night lights, split to different topic.
viewtopic.php?f=1&t=21104&p=85605#p85602
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
http://www.imagemagick.org/Usage/

VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Few questions

Post by VanGog » 2012-05-29T13:11:33-07:00

fmw42 wrote:
VanGog wrote:3) Then I want to make relief (is it right word?) to get only the edges of the selection. Therefor I look for filter function like in PS - its called "Glowing edges" under Filter/Style menu.
see -edge
http://www.imagemagick.org/script/comma ... s.php#edge

or -morphology convolve laplacian
http://www.imagemagick.org/Usage/convol ... et_kernels
I'm trying now apply edge on binary mask

Code: Select all

convert rose_thresholed.png -negate  -edge 1 rose_t90_.png
But I see the edges are outwards orientated. Is any chance to get inward orientated edge?

Post Reply