Page 1 of 1

Circle Crop with smooth gradient

Posted: 2013-09-27T15:30:37-07:00
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

Re: Circle Crop with smooth gradient

Posted: 2013-09-27T15:54:36-07:00
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.

Re: Circle Crop with smooth gradient

Posted: 2013-09-27T16:39:42-07:00
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

Re: Circle Crop with smooth gradient

Posted: 2013-09-27T17:00:57-07:00
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.

Re: Circle Crop with smooth gradient

Posted: 2013-09-27T21:09:42-07:00
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

Re: Circle Crop with smooth gradient

Posted: 2013-09-28T05:29:19-07:00
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

Re: Circle Crop with smooth gradient

Posted: 2013-09-28T10:23:15-07:00
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

Re: Circle Crop with smooth gradient

Posted: 2013-09-28T10:49:00-07:00
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.

Re: Circle Crop with smooth gradient

Posted: 2013-09-29T18:13:01-07:00
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.