Color Fill Bounded Polygon Sectors

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
Jbwxman
Posts: 2
Joined: 2019-05-21T09:47:28-07:00
Authentication code: 1152

Color Fill Bounded Polygon Sectors

Post by Jbwxman »

I recently created an image using matplotlib. The image contains 3 color radius sweeps with a curve (to create a cone). I would like to color fill each cone sector that is bounded by a vertical line and horizontal line. Any ideas how I would go about doing this with Imagemagick?

I wondered if there was a command line function that looks for 2 neighboring colors that meet and change them both to a single color (i.e. red color touches while color). The cone sectors will change position regularly, so I don't think I can use a static pixel position.

This is what my image looks like now https://imgur.com/vCYIXGH
This is what it may look like if the sectors were color filled. https://imgur.com/rwJTDP3 -- > I used gimp to do a quick color fill.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Color Fill Bounded Polygon Sectors

Post by fmw42 »

You can do that using -draw with flood fill. You have to manually select the 3 colors from the arcs. Then pick 3 points, one inside each white region. For example, this seems to work for me. This is unix syntax. In Windows replace the ending \ with ^

Code: Select all

convert img.png -alpha off -fuzz 30% \
-fill "rgb(0,228,0)" -draw "color 360,220 floodfill" \
-fill "rgb(192,192,5)" -draw "color 340,240 floodfill" \
-fill "rgb(255,0,0)" -draw "color 330,250 floodfill" \
-alpha off result.png
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Color Fill Bounded Polygon Sectors

Post by snibgo »

Jbwxman wrote:The cone sectors will change position regularly, so I don't think I can use a static pixel position.
You can calculate the seed point for each fill as the average of the coordinates of the end-points of the curved lines.
snibgo's IM pages: im.snibgo.com
Jbwxman
Posts: 2
Joined: 2019-05-21T09:47:28-07:00
Authentication code: 1152

Re: Color Fill Bounded Polygon Sectors

Post by Jbwxman »

Thanks @fmw42. Now I just need a method to work when the polygon moves... to find the necessary point within.

@snibgo, would I need to use the morphology feature to accomplish this? Could you provide an example for this type of scenario.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Color Fill Bounded Polygon Sectors

Post by snibgo »

Yes, you could do it with morphology LineEnds. See http://www.imagemagick.org/Usage/morphology/#lineends . First, we make the selected colour white and everything else black.
Windows BAT syntax:

Code: Select all

magick ^
  vCYIXGH.png ^
  -fill Black -fuzz 5%% +opaque sRGB(0,50%%,0) ^
  -fill White -fuzz 0 +opaque Black ^
  -colorspace Gray ^
  -alpha off ^
  -morphology HMT LineEnds ^
  txt: | grep white

329,188: (65535,65535,65535)  #FFFFFF  white
392,253: (65535,65535,65535)  #FFFFFF  white
Another method is to find the first and last white pixel. (Strictly, these aren't necessarily the ends.) Windows BAT syntax:

Code: Select all

%IMG7%magick ^
  vCYIXGH.png ^
  -fill Black -fuzz 5%% +opaque sRGB(0,50%%,0) ^
  -fill White -fuzz 0 +opaque Black ^
  -colorspace Gray ^
  -alpha off ^
  ( +clone ^
    -define identify:locate=maximum ^
    -define identify:limit=1 ^
    +write info: ^
    +delete ^
  ) ^
  -flip ^
  info:

Channel maximum locations:
  Gray: 65535 (1) 327,188
Channel maximum locations:
  Gray: 65535 (1) 392,226
The first white pixel is at (327,188). The last is at (392,226) in the flipped image, so (392, 480-1-226) = (392,253) in the input image.

The average of those coordinates is (359.5,207).

Note that these two methods give slightly different results. They are both "good enough".
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Color Fill Bounded Polygon Sectors

Post by fmw42 »

Here is another way. Threshold your image so that you have one arc in white on a black background. Then generate the convex hull. Then use connected components to find the centroid of the convex hull. Here is an example in Unix

# create arc image and turn alpha off and threshold to binary.

Code: Select all

convert -size 200x200 xc:black -fill none -stroke white -strokewidth 2 -draw "arc 50,50 150,150 -150,50" -alpha off -threshold 50% PNG24:x.png
Image

# compute the convex hull image

Code: Select all

convert x.png -morphology thicken:-1 convexhull x2.png
Image

# get the centroid

Code: Select all

coords=`convert x2.png -type bilevel \
-define connected-components:verbose=true \
-connected-components 4 null: | grep "gray(255)" | awk '{print $3}'`
echo "$coords"
110.5,89.0

# the verbose listing will have two entries, one for the black background and one for the white, i.e. gray(255) convex hull shape. The centroid is the 3rd item in the line. See https://imagemagick.org/script/connected-components.php

Code: Select all

convert x2.png -type bilevel \
-define connected-components:verbose=true \
-connected-components 4 null:
0: 200x200+0+0 97.6,101.3 34256 gray(0)
1: 96x91+56+49 110.7,88.8 5744 gray(255)

Sorry, I do not know how to get the centroid from the verbose listing in Windows syntax.
Post Reply