Page 1 of 1
Polygonize an image
Posted: 2018-11-25T07:05:10-07:00
by hellocatfood
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:
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.
Re: Polygonize an image
Posted: 2018-11-25T08:34:36-07:00
by snibgo
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.
Re: Polygonize an image
Posted: 2018-11-25T11:59:15-07:00
by fmw42
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.
Re: Polygonize an image
Posted: 2018-11-25T12:22:34-07:00
by fmw42
You can get constant color sections as follows:
Code: Select all
convert cat.jpg -blur 0x3 -colorspace YIQ -monitor -mean-shift 51x51+10% +monitor -set colorspace YIQ -colorspace sRGB cat3.png
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.
Re: Polygonize an image
Posted: 2018-11-26T03:13:38-07:00
by hellocatfood
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:00To 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
Re: Polygonize an image
Posted: 2018-11-26T05:30:28-07:00
by snibgo
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 +
Re: Polygonize an image
Posted: 2018-11-26T11:06:21-07:00
by fmw42
You can use OpenCV to polygonalize from contours. See
http://www.swarthmore.edu/NatSci/mzucke ... proxpolydp