A Site for Photographers by Photographers

Community > Forums > photo.net > Digital Imaging Software > unsharp mask using convolve...

unsharp mask using convolve matrix function

david holley , Apr 25, 2001; 02:25 p.m.

An unsharp mask function can be defined as a matrix convolution:

0 0 -1 0 0

0 -8 -21 -8 0

-1 -21 299 -21 -1

0 -8 -21 -8 0

0 0 -1 0 0

(credit for these values Alan Gibson, previous answer)

Can anyone give some guidelines of how to use these values as starting values, and vary the values to simulate Photoshop's Amount, Radius, and Threshhold values. Varying the center value (ie. 100 to 500) seems to simulate the Amount value.



    1   |   2     Next    Last

Jack Kennealy , Apr 25, 2001; 05:01 p.m.

David, I'm not sure where that comes from, but, without referring to my reference books, I'd call that filter simply one variation of a linear high-pass filter - which, to be complete, should also be divided by 179, I believe.

I believe that, strictly speaking, the unsharp mask process is not quite a simple linear process, and, therefore, can not be described by a single convolution kernel.

If I'm wrong, I'm sure someone will correct me! It's been a while since I buried myself in the mathematics of this! Embarrassing...

Scott -- , Apr 25, 2001; 05:30 p.m.

It's starting to look like photo.net needs an Advanced Digital Darkroom forum...

Norman Koren , Apr 25, 2001; 05:31 p.m.

David-- I present a simplified explanation of sharpening in part 2 of my article on image sharpness and MTF, http://www.normankoren.com/Tutorials/MTF2.html. My explanation is one dimensional (simplified); the matrix you present is two dimensional (realistic).

When a threshold is applied in Unsharp Mask, a decision is made as to whether or not to sharpen. This decision is based on the gradient between adjacent pixels-- if it's below the threshold there is no sharpening. That keeps smooth areas like skies from becomming more grainy, but there is no linear matrix in this case since the decision introduces a nonlinearity. If no threshold is applied, a linear matrix exists. The one you gave is for a very specific Amount and Radius. The radius corresponds roughly to the distance between the central pixel (299 in the example) and the pixel with maximum (negative) boost (-21): Radius would be approximately 1 in this case. For a larger radius the matrix would be larger-- more elements. The value of the maximum negative boost (-21 in the example) relative to the center value (299) is proportional to Amount. But I'm not sure of the exact equation. Increasing the center value will effectively decrease Amount.

Division by 179, mentioned in the previous response, is correct and necessary for the matrix to be properly normalized: the sum of the elements equals 1. That should give you a rough idea of what to expect. I'd have to sit down and spend a bunch of time hashing out equations to come up with anything better-- I work in electrical engineering where we process signals in quite a similar manner (pulse slimming in magnetic recording is exactly the same as sharpening, but in one dimension instead of two). I'm not familiar with technical literature in optics.

Gordon Richardson , Apr 26, 2001; 12:22 a.m.

The previous thread in the Archives answers this question - "Need mathematical definition of unsharp mask": http://www.photo.net/bboard/q-and-a-fetch-msg.tcl?msg_id=000Qi5

Pete Andrews , Apr 26, 2001; 06:10 a.m.

Surely the whole matrix has to be expanded in order to increase the radius? A fixed 25 element matrix can only handle a two pixel radius. Less, if adjacent pixel thresholding is to be taken into account. These simple integer matrices always seem like a crude way to do things to me.

Winston Chang , Apr 26, 2001; 01:02 p.m.

I wrote the Unsharp Mask plug-in for the GIMP, so I can tell you how my version works. It seems to behave like photoshop's unsharp mask, but I haven't done side-by-side comparisons.

The first step is to perform a gaussian blur on the image. This is the hard part. I did it by creating a two-pass convolution matrix (since the gaussian function is linearly separable). The plug-in performs a numerical integration, with a radius equal to 2*s, where s is the standard deviation for the function. The gaussian function is f(x) = e^-(x^2/2s^2). To do the numerical integration, the function is sampled 50 times per pixel. The matrix is then scaled down so that the total area under the curve is 1.

After generating the matrix, the filter performs a horizontal and vertical pass on the image, creating the blurred image. If a given pixel in the blurred image is called b and one in the original is called a, a pixel in the final image should be a-amount*(a-b), where amount is greater than zero (and usually less than one).

One more thing -- the threshold is the minimum value of |a-b| that would be processed. If |a-b| is less than the threshold, it would be treated as if it were zero and ignored.

Someone pointed out to me that the last stage, combining the blurred and original image, behaves differently from a traditional sandwiched-negative unsharp mask. Whereas the traditional method effectively does a multiplicative combine on the two images, the digital version does an additive combine. I don't know if the multiplicative way looks significantly different -- I suppose you could play around with the GIMP (or photoshop) combining layers if you wanted to find out.

Other notes:

  • I believe you can use a binomial expansion to approximate the gaussian (bell-curve) distribution, but it's not very good with small radii.
  • Optimization is a pain in the ass.
May I ask what you are doing this for?

Winston Chang , Apr 26, 2001; 02:23 p.m.

I just realized that my previous post didn't answer your question at all...

I don't believe that there is any way to implement a threshold with a convolution matrix, but you should be able to change the radius and amount.

The basic idea for an unsharp mask convolution matrix is to start with a blur matrix. Sum the values of the matrix (I'll call this c), then take the additive inverse of the blur matrix and add c/(2*amount) to the center pixel. Then normalize the matrix so the sum of all the cells is 1. I think most convolution programs do this part for you.
Note: my amount parameter description might not be exactly right.

Example: if your blur matrix is:

0 1 0
1 3 1
0 1 0

then the sum is 7. So take the additive inverse of the blur matrix and add 7/(2*amount) (I'll use amount=0.5) to the center:
0 -1 0
-1 4 -1
0 -1 0

Normalize to 1, and voila, you have an unsharp mask.

To increase the radius of the blur, you'll have to use a larger convolution matrix -- I used a gaussian function to produce the blur matrix, though other functions are possible. It's generated according to the formula f(x) = e^(-(x^2+y^2)/2s^2), where s is the standard deviation. If this part is confusing, I'm sure there are better resources out there that describe gaussian blurs.

Mark Van Tilburg , Apr 26, 2001; 03:07 p.m.

Hey Winston, great to get a real expert on the GIMP handy. I wonder if you could tell me how to do a "fuzzy select" with the GIMP, like photoshop has. What I mean by a fuzzy select is by drawing an enclosed space around a section in the photo, and then the program more loosely selects around the object, and you can change the effect with fades and so forth. Am I making myself clear, I hope?

Thanks for any help... Mark

david holley , Apr 27, 2001; 09:10 a.m.

Thanks to all for you answers.

Winston, I just needed a "quick and dirty" method of approximating unsharp mask and convolve matrix seemed like a likely candidate.

    1   |   2     Next    Last

Back to top

Notify me of Responses