Inverse blur/sharpen kernel

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
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Inverse blur/sharpen kernel

Post by VanGog »

Is it possible to create kernel which will blur the parts of image which are sharp and also will sharpen the areas which are blur?

For instance in this image I have marked regions with text which should be sharpen more (red frame) and areas which should be sharpen less (orange regions). The bottom right should be blurred.

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

Re: Inverse blur/sharpen kernel

Post by snibgo »

Use "-compose Blur" with a mask that is white where you want to blur, and black where you don't want to blur (and gray in the middle).

For selective sharpening, do a selective blur but then extrapolate from the blur, past the input image.
snibgo's IM pages: im.snibgo.com
Vilius
Posts: 3
Joined: 2016-08-02T04:13:55-07:00
Authentication code: 1151

Re: Inverse blur/sharpen kernel

Post by Vilius »

Hi,
I was looking for the solution of almost the same problem. I need to selectively sharpen some pictures (in batch modus) -- like here http://digiretus.com/selective-sharpening-in-photoshop/ in Photoshop. The top of the photograph should be sharpened and towards the bottom the factor should gradually decrease. I am able to prepare N-S gradient mask however I don't understand what snibgo means with extrapolate from the blur, past the input image.
Could someone provide a small example?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Inverse blur/sharpen kernel

Post by fmw42 »

See https://imagemagick.org/Usage/mapping/#blur for the basic variable blur. I am not sure what snibgo meant either, but I do not think it applies to your case. The basic variable blur should do what you want. For sharpening, I think you can create the blurred version and then subtract some fraction of it from the original (sort of unsharp masking)

Alternately and more efficiently, make a sharpened copy of your image and use the gradient as a mask to blend to the two images. See https://imagemagick.org/Usage/compose/#compose for the masked composite operation.

See also https://imagemagick.org/Usage/photos/#tilt_shift
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Inverse blur/sharpen kernel

Post by snibgo »

snibgo wrote:... extrapolate from the blur, past the input image.
This is unsharp masking, but in this case with a selective (ie variable) blur, so the sharpening is also selective.

When we have an image and a blurred version of that image, the result could be an interpolation between those two versions, anywhere between the blur and the original. Or it could be an extrapolation, past the original, which increases sharpness.

See Selective blur "Toytown effect".
snibgo's IM pages: im.snibgo.com
Vilius
Posts: 3
Joined: 2016-08-02T04:13:55-07:00
Authentication code: 1151

Re: Inverse blur/sharpen kernel

Post by Vilius »

Many thanks for your answers.

Do you mean something like:

w=$(convert image -format "%w" info:); h=$(convert image -format "%h" info:); convert -composite image \( +clone image -unsharp 0x3+1+0 \) \( +clone -size 1x$h gradient: -fx "atan(u*Pi)/atan(Pi)-.1" -negate -scale "$wx$h !" \) result.jpg

?
I will start to experiment with the parameters.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Inverse blur/sharpen kernel

Post by snibgo »

Something like that, yes.

In the bad old days, IM commands could list the operations in any order you want, and IM would process the operations one after the other in whatever order it wanted.

These days, IM will process the operations in the same order they are given. So "-composite" before any images have been read won't do anything, because there are no images to composite.
snibgo's IM pages: im.snibgo.com
Post Reply