[Image-SIG] Unsharp Masking?

clee@spiralis.merseine.nu clee@spiralis.merseine.nu
Mon, 18 Mar 2002 10:26:25 -0600


>>>>> "kevin" == kevin  <Kevin@Cazabon.com> writes:

    kevin> I've cleaned it up a bit, and added documentation so you
    kevin> people other than me understand what's going on in the code
    kevin> a little more.  I've tried to move all the calculations
    kevin> upstream as much as possible, but it's still only about 7%
    kevin> faster than the original... there's a LONG way to go.  On a
    kevin> dual 1.2ghz box, it's taking 30 seconds to sharpen a
    kevin> 400x600 pixel image!

I haven't looked very closely at your code, but it sounds like you're
doing something close to what something I'm familiar with from imaging
processsing.  I've used the Numeric/scipy utils to do this sort of
thing before for fast convolutions.  (The new bbl_image package might
also provide the tools you need.)

Here's an example assuming grayscale images.  RGB is trickier (it's
often better to use an HSV representation of color for this sort of work.)
Assuming you have your image as a Numeric floating point array, called
"I", I believe this code will do approximately what you want:


----------------------------------------------------------------------------
I = <load your image as a float array>

import Numeric as num
import scipy.signal as signal

# create a filter kernel called K (taken from your comment)

K = array([[  66,      73,      81,      73,      66 ]
           [   73,      81,      90,      81,      73 ]
           [   81,      90,      100,     90,      81 ]
           [   73,      81,      90,      81,      73 ]
           [   66,      73,     81,      73,      66]]

# you probably want to normalize it in some way
K = K/num.sum(num.sum(K)) 

G1 = signal.convolve2d(I, K, mode='same')  # G stands for gausian blur
L1 = I - cnv_image # L stands for Laplacian (2D difference)
# note original image I = G1 + L1

# now you would like to do a thresholding operation of some sort
# I'm not sure exactly what you want but something like this this
# seesm to get the gist of what you want to do

# say theshold for change is 5.0, rich comparion operator ">" requires 
#  python version >=  2.1 (otherwise use older comparison function)

threshold = 5.0
changemask = L1 > threshold  


# compute how much to change the original image
multiplier = 2.0  #  2.0 <- percentage = 200 increase or whatever

L1change = L1 * multiplier

# set the values above threshold to the new L1change value
putmask(L1, changemask, L1change)  

# now reconstruct

sharpI = G1 + L1  # enhanced image

--------------------------------------------------------------------------

Hope this helps,

-chris