A Site for Photographers by Photographers

Community > Forums > photo.net > Digital Imaging > Need mathematical definition...

Featured Equipment Deals

State of the ART: The Purpose of Fine Art Photography Read More

State of the ART: The Purpose of Fine Art Photography

Fine Art photographer, Pete Myers, discusses fine art photography in his first of four installments.

Latest Equipment Articles

Sun Position Tracking Apps Read More

Sun Position Tracking Apps

These 5 apps, ranging in price from free to $8.99, are our top picks for tracking sun (and moon) light. Also ranging in complexity, some help you keep tabs on the ideal lighting of the day while...

Latest Learning Articles

State of the ART: Rag Mama Rag! Read More

State of the ART: Rag Mama Rag!

In his latest exploration, fine art photographer Pete Myers reviews and compares some of the highest quality rag-based photographic papers on the market today.


Need mathematical definition of unsharp mask

Ron Gemeinhardt , Apr 12, 1999; 01:41 p.m.

OK, here's one for the hardcore bitcrunchers among us. Does anyone happen to know the spatial filter matrix for an "unsharp mask" filter? My image-processing tool of choice supports custom filters, and includes four flavors of high-pass (sharpening) filters out of the box, but not the unsharp mask. Another product I sometimes use has a "true" unsharp mask, but it won't let me open the built-in filter definitions as a starting point for a custom one, so I can't see what it's doing.

Naturally, the filter whose definition I can't see is the one that produces the most consistently pleasing effect. I bumped into this problem while updating my photo gallery yesterday, and am hoping to cut down the steps involved in prepping scans for the web.

If I'm not being clear, what I'm looking for is the coefficient matrix used when calculating filtered pixels from pixels in the neighborhood. For example, one type of sharpening filter might look like so (forgive me if it's off a bit; I'm going from memory):

	 0  -1   0
	-1   5  -1
	 0  -1   0
Examples in my formal image processing texts confine themselves to fairly simple filter matrices to avoid obscuring the principles, so I found no help there.

I believe I have up to a 7x7 or 9x9 matrix to define a filter. Anybody? Thanks.

Responses

Andrei Frolov , Apr 12, 1999; 02:32 p.m.

What unsharp mask does is it takes value channel of the image, blurs that to specified degree to produce unsharp mask. That mask is then substracted from main image, and contrast is stretched to compensate. It works something like this: V' = (V - x*U)/(1 - x), where V is the value of the image pixel , U is the unsharp mask pixel, and x is degree of sharpening applied (0 .. 1.0). It should not be too dificult to implement using generic matrix transform. The only problem is that nice results are produced by the bigger blur radius, and you need big matrix to emulate that. It would be probably easier and faster to implement blur algorithm separately (say, like some sort of red-black sweep or something), and then mask as above.

If you want to try this, here is 3x3 toy model (with Gaussian blur):

	( [ 0 0 0 ]       [ 0.071 0.107 0.071 ] )
	( [ 0 1 0 ] - x * [ 0.107 0.290 0.107 ] ) / (1 - x)
	( [ 0 0 0 ]       [ 0.071 0.107 0.071 ] )
I haven't tried this particular example, you tell me if it works :)...

Alan Gibson , Apr 12, 1999; 03:05 p.m.

Andrei beat me to it. Never mind, I can supply some more gory detail. The general technique for an unsharp mask is: make a blurred copy of the image, and extrapolate from that, past the original, resulting in a sharper image. A simple technique for the extrapolation is to take twice the original, and subtract the blurred copy. A simple convolution matrix to create the blurred image is:

  0  1 0
  1 -3 1
  0  1 0
Subtracting this blurred image from the twice original is equivalent to aplying the following matrix to the original:
   0 -1  0
  -1  5 -1
   0 -1  0
For my work, I prefer the flexibility of leaving it as two operations: create the blur, and then extrapolate. For the blur, I like a Gaussian distribution. For these, the values in the cells of the array are given by:
  v = e^(-(x*x + y*y) / (2 * sd * sd))
where sd is the required standard deviation. Examples of the blurring matrix are:
SD 0.6:
  0, 1, 0
  1, 4, 1
  0, 1, 0
Divide by 8

SD 0.7:
  0, 0, 1, 0, 0
  0, 8, 21, 8, 0
  1, 21, 59, 21, 1
  0, 8, 21, 8, 0
  0, 0, 1, 0, 0
Divide by 179

SD 0.8:
  0, 0, 1, 0, 0
  0, 5, 10, 5, 0
  1, 10, 23, 10, 1
  0, 5, 10, 5, 0
  0, 0, 1, 0, 0
Divide by 87

SD 0.9:
  0, 1, 1, 1, 0
  1, 3, 6, 3, 1
  1, 6, 12, 6, 1
  1, 3, 6, 3, 1
  0, 1, 1, 1, 0
Divide by 60

SD 1.0:
  0, 0, 1, 1, 1, 0, 0
  0, 2, 7, 12, 7, 2, 0
  1, 7, 33, 55, 33, 7, 1
  1, 12, 55, 90, 55, 12, 1
  1, 7, 33, 55, 33, 7, 1
  0, 2, 7, 12, 7, 2, 0
  0, 0, 1, 1, 1, 0, 0
Divide by 566

SD 1.5:
  0, 0, 0, 1, 1, 1, 0, 0, 0
  0, 1, 2, 4, 5, 4, 2, 1, 0
  0, 2, 6, 12, 14, 12, 6, 2, 0
  1, 4, 12, 22, 28, 22, 12, 4, 1
  1, 5, 14, 28, 35, 28, 14, 5, 1
  1, 4, 12, 22, 28, 22, 12, 4, 1
  0, 2, 6, 12, 14, 12, 6, 2, 0
  0, 1, 2, 4, 5, 4, 2, 1, 0
  0, 0, 0, 1, 1, 1, 0, 0, 0
Divide by 495
Which of these blurs, and what degree of extrapolation, is best depends on the nature of the image, and your personal preferences. For much of my web work, I like SD=0.7, and final=2*original minus blur. That could be expressed as a single matrix:
  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
Divide by 179

Chrissy Ragulsky , Apr 12, 1999; 09:19 p.m.

I knew there was someday going to be a good reason why I should have paid more attention in math class!

Ron Gemeinhardt , Apr 12, 1999; 10:09 p.m.

Andrei, Alan:

Thanks very much. Blame my preconceptions, but I never thought of a sharpening operation as the subraction of unsharp information, but rather as the removal of low spatial frequencies. Of course that amounts to the same thing, but it's all in how you look at it.

Armed with that insight, I now see why my canned filters all perform so poorly--they are all defined on a 3x3 matrix. I have support for up to 7x7, which seems large enough to predefine several new filters with useful variations in strength. I gave the SD=0.7 example a quick trial, and it clipped the highlights, so I suspect I may have a software limit to work around (an 8- or 16-bit limit somewhere, I'm sure--shameful in a 32-bit app!), but that shouldn't be difficult. Thanks again!

And Chrissy: don't feel bad; if I weren't so rusty myself on the math, I'd have remembered the term "convolution matrix" for my original post!

Alan Gibson , Apr 13, 1999; 04:23 a.m.

My pleasure. Indcidentally, my 'twice the original minus the blur' is a particular example of V' = a*V + (1-a)*U, where a = 2. The value of a should be between 1 and infinity. This formula is equivalent to Andrei's V' = (V - x*U)/(1 - x), where x = 1-1/a, so when a=2, x=0.5.

The fun comes when you want to sharpen a picture, without sharpening the grain. This means you want to boost the middle frequencies. But that's another story.

Struan Gray , Apr 13, 1999; 06:23 a.m.

If you'd like some more fun, take a look at a method called 'statistical differencing', which extends the idea of an unsharp mask by weighting the subtraction of the blurred version of the image using the local standard deviation. In layman's terms, it applies a more agressive unsharp mask in areas with a lot of fine detail, but treats smoother areas more gently. I find it to be a more subtle way of bringing out detail in an image than a raw unsharp mask. Details are in 'Digital Image Processing' by W.K.Pratt, pub. Wiley, New York 1978, pp 305-ff.

Back to top

Notify me of Responses