Circle Crop with smooth gradient

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
Bernd Hohmann
Posts: 4
Joined: 2013-09-27T14:42:15-07:00
Authentication code: 6789

Circle Crop with smooth gradient

Post by Bernd Hohmann »

I need to do a circle crop on an image with a gradient.

Reason: I'm doing HDR panoramashots with a Sigma 8mm fisheye. And sometimes I have internal reflections of the lense in the image - namely outside the 180° field of view (see image).
Image

Unfortunately the controlpoint detectors will gladly jump on those artifacts which makes automatic processing difficult.

So I decided to crop those images with ($1 = input image, $2 = output image)

Code: Select all

convert -quality 100% -size "$width x $height" xc:none -fill $1 -draw "translate $center_x $center_y circle 0,0 $center_y,0" $2
This creates a not very smooth circle where the jagged boundaries irritate the controlpoint detectors too.

How to create a nice, 5-20 pixel wide gradient?

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

Re: Circle Crop with smooth gradient

Post by fmw42 »

Please clarify where the gradient comes in? Do you mean a radial gradient that ramps from the edge of the circle outward?

Can you provide the values that you are using for your command corresponding to the image you have?

You can try using -morphology distance as below and then use as an alpha mask


convert -size 320x213 xc:black -fill white \
-draw "translate 160,106.5 circle 0,0 0,106.5" -alpha off -negate \
-morphology distance Euclidean:4 -auto-level -negate -level 50x100% result.png


Let us know if this is kind of what you need or what changes you require. Please clarify further as needed.
Bernd Hohmann
Posts: 4
Joined: 2013-09-27T14:42:15-07:00
Authentication code: 6789

Re: Circle Crop with smooth gradient

Post by Bernd Hohmann »

The legendary Fred himself - I feel honored, thanks for helping me.

Testcase in full resolution:

http://201.ag/IMG_8520.JPG is the image in full resolution
http://201.ag/crop is the script to crop the image to http://201.ag/crop_IMG_8520.JPG
http://201.ag/gradient_IMG_8520.JPG was done with GIMP. Zoom into the border to see the difference.

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

Re: Circle Crop with smooth gradient

Post by fmw42 »

I do not see your harsh stair stepping (in the zoomed image) in my result when I use IM 6.8.7.0 Q16 Mac OSX with

convert -size 320x213 xc:black -fill white \
-draw "translate 160,106.5 circle 0,0 0,106.5" -alpha off result


I think you have two problems:

1) your calculations of center are only to integer precision when you use expr. Use bc instead or IM fx calculations to get floating point precision.

2) I suspect your IM version may have a bug and is not anti-aliasing the -draw command. You never answered what version and platform you are using (or I missed it).

If you want to antialias further

convert -size 320x213 xc:black -fill white \
-draw "translate 160,106.5 circle 0,0 0,106.5" -alpha off -virtual-pixel black -blur 1x65000 -level 50x100% result.png

You can change the radius of the blur from 1 to some larger value if needed.
Last edited by fmw42 on 2013-09-27T21:16:16-07:00, edited 2 times in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Circle Crop with smooth gradient

Post by fmw42 »

Looking at your script and taking values from it, I tried to reproduce your full resolution results in IM 6.8.7.0 Q16 Mac OSX. My results do not show the extreme stair-stepping. So again I think your anti-aliasing is not working correctly.

The first is your script condensed (and with floating point calculations). The second is creating a separate mask and putting the mask in the alpha channel. The third is the same as the second, but I further anti-alias by using -blur ... -level ... And again you can increase the radius of the blur from 1 if you want more anti-aliasing.

Code: Select all

infile="crop_IMG_8520.JPG"
offx=-50
offy=-20
ww=`convert $infile -format "%w" info:`
hh=`convert $infile -format "%h" info:`
centx=`convert xc: -format "%[fx:$ww/2 + $offx]" info:`
centy=`convert xc: -format "%[fx:$hh/2 + $offy]" info:`
convert -size ${ww}x${hh} xc:black -fill $infile -draw "translate $centx,$centy circle 0,0 $centy,0" -alpha off 1tmp1.png


infile="crop_IMG_8520.JPG"
offx=-50
offy=-20
ww=`convert $infile -format "%w" info:`
hh=`convert $infile -format "%h" info:`
centx=`convert xc: -format "%[fx:$ww/2 + $offx]" info:`
centy=`convert xc: -format "%[fx:$hh/2 + $offy]" info:`
convert $infile \
\( -size ${ww}x${hh} xc:black -fill white -draw "translate $centx,$centy circle 0,0 $centy,0" \) \
-alpha off -compose copy_opacity -composite 1tmp2.png



infile="crop_IMG_8520.JPG"
offx=-50
offy=-20
ww=`convert $infile -format "%w" info:`
hh=`convert $infile -format "%h" info:`
centx=`convert xc: -format "%[fx:$ww/2 + $offx]" info:`
centy=`convert xc: -format "%[fx:$hh/2 + $offy]" info:`
convert $infile \
\( -size ${ww}x${hh} xc:black -fill white -draw "translate $centx,$centy circle 0,0 $centy,0" -virtual-pixel black -blur 1x65000 -level 50x100% \) \
-alpha off -compose copy_opacity -composite 1tmp3.png
Bernd Hohmann
Posts: 4
Joined: 2013-09-27T14:42:15-07:00
Authentication code: 6789

Re: Circle Crop with smooth gradient

Post by Bernd Hohmann »

fmw42 wrote: 2) I suspect your IM version may have a bug and is not anti-aliasing the -draw command. You never answered what version and platform you are using (or I missed it).

If you want to antialias further

convert -size 320x213 xc:black -fill white \
-draw "translate 160,106.5 circle 0,0 0,106.5" -alpha off -virtual-pixel black -blur 1x65000 -level 50x100% result.png
Version: ImageMagick 6.7.7-10 2013-09-01 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC
Features: OpenMP
Debian 7


I guess my version is foobar, because antialiasing doesn't work. Checking further this evening.

Bernd
Bernd Hohmann
Posts: 4
Joined: 2013-09-27T14:42:15-07:00
Authentication code: 6789

Re: Circle Crop with smooth gradient

Post by Bernd Hohmann »

fmw42 wrote:Looking at your script and taking values from it, I tried to reproduce your full resolution results in IM 6.8.7.0 Q16 Mac OSX. My results do not show the extreme stair-stepping. So again I think your anti-aliasing is not working correctly.
Gna.. The problem is, that you're working with some transparency-tricks and PNG as output format. This doesn't work with JPG, which is a requirement for me.

Any other solution?

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

Re: Circle Crop with smooth gradient

Post by fmw42 »

Two things:

1) Try recompiling IM without OpenMP. So unix systems have trouble with it.

2) Add -flatten to my command to save as jpg

infile="crop_IMG_8520.JPG"
offx=-50
offy=-20
ww=`convert $infile -format "%w" info:`
hh=`convert $infile -format "%h" info:`
centx=`convert xc: -format "%[fx:$ww/2 + $offx]" info:`
centy=`convert xc: -format "%[fx:$hh/2 + $offy]" info:`
convert $infile \
\( -size ${ww}x${hh} xc:black -fill white -draw "translate $centx,$centy circle 0,0 $centy,0" -virtual-pixel black -blur 1x65000 -level 50x100% \) \
-alpha off -compose copy_opacity -composite -background black -flatten 1tmp3.png

You can change -background black to any color you want, for example, if you want it white.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Circle Crop with smooth gradient

Post by anthony »

Rather than -flatten you should use -alpha remove
http://www.imagemagick.org/Usage/masking/#alpha_remove
See details in, Removing Transparency
http://www.imagemagick.org/Usage/masking/#remove

While they both have the same result using 'over composition' onto the the background color, the -alpha remove does not create a second 'background image' to compose against, but does the pixel composition pixel by pixel more directly. It also does not 'merge' multiple images as -layers flatten (what -flatten is an short name, historical alias for) does.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply