Polygonize an image

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
User avatar
hellocatfood
Posts: 44
Joined: 2010-01-01T13:29:41-07:00
Authentication code: 8675309

Polygonize an image

Post by hellocatfood » 2018-11-25T07:05:10-07:00

I'm trying to use Imagemagick to "polygonize" an image. To illustrate what I mean here's an example made with G'MIC.

Original photo: https://www.catster.com/wp-content/uplo ... y-shot.jpg

End result:

Image

An explanation of how the G'MIC script works can be seen here: http://gimpchat.com/viewtopic.php?f=28&t=9174

Can this be made in Imagemagick? I've tried using blur to average the colours but then I can't get the flat colours or straight lines.

snibgo
Posts: 11793
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Polygonize an image

Post by snibgo » 2018-11-25T08:34:36-07:00

IM doesn't have a command that is the equivalent of GMIC's "polygonize" script. I expect it could be done in IM with a shell script, as IM and GMIC have similar primitive operations. To do it accurately, you could examine the GMIC script and find equivalent operations in IM.
snibgo's IM pages: im.snibgo.com

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

Re: Polygonize an image

Post by fmw42 » 2018-11-25T11:59:15-07:00

snibgo, I know it is not the same, but you might post your method for converting the image to circles. I believe that I saw that once before.

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

Re: Polygonize an image

Post by fmw42 » 2018-11-25T12:22:34-07:00

You can get constant color sections as follows:

Image

Code: Select all

convert cat.jpg -blur 0x3 -colorspace YIQ -monitor -mean-shift 51x51+10% +monitor -set colorspace YIQ -colorspace sRGB cat3.png
Image


To polygonalize, you might be able to do that with a raster to vector tool such as potrace. But I have not used it before.

User avatar
hellocatfood
Posts: 44
Joined: 2010-01-01T13:29:41-07:00
Authentication code: 8675309

Re: Polygonize an image

Post by hellocatfood » 2018-11-26T03:13:38-07:00

snibgo wrote:
2018-11-25T08:34:36-07:00
To do it accurately, you could examine the GMIC script and find equivalent operations in IM.
I had a look at GMIC and it seems like the polygonize effect is a whole effect in itself (fx_polygonize_preview) so, unless I learnt C++ I don't think I could convert it to ImageMagick
fmw42 wrote:
2018-11-25T12:22:34-07:00
To polygonalize, you might be able to do that with a raster to vector tool such as potrace. But I have not used it before.
Potrace does not have anything equivalent to Inkscape's Simplify Path option. The closest would be the -alphamax option:
set the corner threshold parameter. The default value is 1. The smaller this value, the more sharp corners will be produced. If this parameter is 0, then no smoothing will be performed and the output is a polygon. If this parameter is greater than 4/3, then all corners are suppressed and the output is completely smooth.
Still, this doesn't polygonize or simplify the resultant shape.

In the end I had to rely on using G'MIC from the command line. As described in their documentation I first ran the function in GIMP G'MIC and then copied the corresponding command line operation. In this case it was:

Code: Select all

gmic input_cat.jpg fx_polygonize_preview 300,10,10,10,10,0,0,0,255,0,50,50 -o output_cat.jpg

snibgo
Posts: 11793
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Polygonize an image

Post by snibgo » 2018-11-26T05:30:28-07:00

hellocatfood wrote:... unless I learnt C++ I don't think I could convert it to ImageMagick.
It is written in GMIC's own scripting language. Here is (a version of) that script:

Code: Select all

#@cli polygonize : _warp_amplitude>=0,_smoothness[%]>=0,_min_area[%]>=0,_resolution_x[%]>0,_resolution_y[%]>0
#@cli : Apply polygon effect on selected images.
#@cli : Default values: 'warp_amplitude=300', 'smoothness=2%', 'min_area=0.1%', 'resolution_x=resolution_y=10%'.
#@cli : $ image.jpg +polygonize ,
polygonize : check "${1=300}>=0 && ${2=2%}>=0 && ${3=0.1%}>=0 && ${4=10%}>0 && ${5=$4}>0"
e[^-1] "Polygonize image$? with warp amplitude $1, smoothness $2, minimal area $3 and resolutions ($4,$5)."
v -  repeat $! l[$>]
+b $2 gradient_norm. g. a[-2,-1] c channels. 0,2 *. {1/0.1+max(abs(im),abs(iM))}
resx={max(1,round(if(${is_percent\ $4},w*$4,w/$4)-1))}
resy={max(1,round(if(${is_percent\ $5},h*$5,h/$5)-1))}
plane3d 1,1,$resx,$resy *3d. {0,w-1},{0,h-1},1
s3d. rm.. i.. (0;{h-1}) r.. 3,{h},1,1,3 round.. y..
[-4] a[-7--2] y r. 3,{h/3},1,1,-1 z. 0,1 permute. yzcx
repeat $1 +warp[1] .,0,0 +[-2,-1] done
permute. cxyz z. 0,2 y. j[2] .,0,8 rm[-3,-1]
[0],[0] j3d. [1],0,0,0,1,2 rm[1]
if {$3>0}
min_area={0,if(${is_percent\ $3},$3*w*h,$3)}
+area. 0,1 >=. $min_area +.. 1 *.. . distance. 1 *. -1 watershed.. . rm.
fi
blend shapeaverage
endl done v +
snibgo's IM pages: im.snibgo.com

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

Re: Polygonize an image

Post by fmw42 » 2018-11-26T11:06:21-07:00

You can use OpenCV to polygonalize from contours. See http://www.swarthmore.edu/NatSci/mzucke ... proxpolydp

Post Reply