[Image-SIG] Unsharp Masking?

K Schutte schutte@fel.tno.nl
Mon, 18 Mar 2002 17:15:34 +0100


Three suggestions here:

1. The C code is implemented for convolutions in
Imaging-1.1.2/libImaging/Filter.c
   Here you can specify filter coefficients yourself.

2. Gaussian smoothing can be implemented as a separable filter. This means that
   instead of convoluting with a 5x5 kernel you can convolve first with a
   5x1 kernel, followed by a convolutiuon with a 1x5 filter. This means that
   you have to perform not 25 but only 10 operations per pixel.

3. A nice thing about Gaussian distributions is that they follow the
   Central Limit Theorem. Which tells you that enough times applying
   about any (bla bla bla on fairly standard assumptions) convolution 
   to an image will result in a more-or-less Gaussian blurring.
   Starting with something like a Gaussian blurring (with a smaller
   sigma) will help. So will the filter ImageFilter.SMOOTH might be
   a nice choice. Experimenting helps you to decide how much times
   you should apply this filter to reach a sigma (radius in your code)
   preferred to your application. It is my guess this is a limited
   amount and gives you much faster execution.

Klamer

> "Kevin@Cazabon.com" wrote:
> 
> I've cleaned it up a bit, and added documentation so you people other than
> me understand what's going on in the code a little more.
> 
> I've tried to move all the calculations upstream as much as possible, but it's
> still only about 7% faster than the original... there's a LONG way to go.  On
> a dual 1.2ghz box, it's taking 30 seconds to sharpen a 400x600 pixel image!
> 
> Iterating over each channel of each pixel in Python is just too slow... would
> trying to implement this type of thing in a C extension (like the core
> _imaging.pyd file) help a lot?  Unfortunately, I'm not much of a C
> programmer.  FRED!!! HELP!!!  q:]
> 
> I could do the calculations on a gray-scale version of the file and only
> sharpen based on gray-contrast, but I'd rather not limit it that way... (max
> 3x speed improvement, minus time to create grayscale version of image)
> 
> The updated version, if anyone can give me a hand is available at:
> http://www.cazabon.com/python/unsharpMask/UnsharpMaskingModule.py
> 
> (and it now can be used for gaussian blurring too, as an added bonus).
> 
> Thanks,
> Kevin.
> 
>      ----- Original Message -----
>      From: Moodie, Craig CA
>      To: 'Kevin@Cazabon.com'
>      Sent: Sunday, March 17, 2002 7:05 PM
>      Subject: RE: [Image-SIG] Unsharp Masking?
> 
>      Kevin,
>          On first inspection I would suggest that you remove as much as
>      is physically possible from the inner loop. Put as much as you can
>      into a lookup table-----avoid calculations.You should be able to at
>      least get a speed increase of 2.
>      For example
>                              pixNumber = ((column + (row *
>      image.size[0])) * channels) + c + (xr * channels) + (yr *
>      image.size[0] * channels)
> 
>                              if pixNumber < 0:
>                                  pixNumber = column * channels
>                              elif pixNumber > len(imArray) - 1:
>                                  pixNumber = (column + (row *
>      image.size[0])) * channels
> 
>                              pixValue = imArray[pixNumber]
> 
>                              pixWeight = pow(weight, (abs(xr) +
>      abs(yr)))----------------------->LUT
>                              totalValue = totalValue + ((origPixel -
>      pixValue) * pixWeight)
>      the underlined could all be calculated outside the xr,yr loops.
>          Cheers
>              Craig
> 
>           -----Original Message-----
>           From: Kevin@Cazabon.com [mailto:kevin@cazabon.com]
>           Sent: Monday, March 18, 2002 10:18 AM
>           To: image-sig@python.org
>           Subject: [Image-SIG] Unsharp Masking?
> 
>           Does anyone have a FAST unsharp-masking method for use
>           with PIL?
> 
>           I wrote a brute-force module for this in Python
>           today (attached, if it gets through the newsgroup
>           filters) that works just fine, but it's mighty slow...
>           anyone have something faster?  The built in
>           ImageEnhance.Sharpness filter is OK for very basic stuff,
>           but the best way to sharpen is through an unsharp-mask
>           type algorithm... it's much more powerful
>           and controllable.
> 
>           For those that don't know how unsharp masking works,
>           here's the basics:
> 
>           1)  a copy of the image is blurred using a gaussian-type
>           blurring algorighm (this is the 'unsharp' part), with a
>           user-defined radius for the blur.
> 
>           2)  the blurred image is then compared (pixel by pixel if
>           necessary) to the original image
> 
>           3)  the amount of difference between the blurred pixel and
>           original pixel determines if it is an edge or not.  If the
>           difference is greater than the user-specified "threshold",
>           sharpening is performed in step 4)
> 
>           4)  the pixel is changed by the OPPOSITE amount of the
>           difference between the original and blurred pixels,
>           multiplied by the user-defined "percentage"
> 
> 
> 
>           Any help is appreciated in speeding this up!
> 
>           Kevin Cazabon.
> 
>      EOM
> 
>      NOTICE - This message and any attached files may contain information
>      that is confidential and/or subject of legal privilege intended only
>      for use by the intended recipient. If you are not the intended
>      recipient or the person responsible for delivering the message to
>      the intended recipient, be advised that you have received this
>      message in error and that any dissemination, copying or use of this
>      message or attachment is strictly forbidden, as is the disclosure of
>      the information therein. If you have received this message in error
>      please notify the sender immediately and delete the message.

-- 
Klamer Schutte, E-mail: Schutte@fel.tno.nl
Electro-Optical Systems, TNO Physics and Electronics Laboratory
Tel: +31-70-3740469 -- Fax: +31-70-3740654 -- Mobile: +31-6-51316671