0

Possible Duplicate:
Is there a way to detect if an image is blurry?

How to calculate blurness and sharpness of a given image usig opencv? Is there any functions there in opencv to do it? If there is no functions in opencv how can I implement it? nay ideas would be great..

The input will be an image and the output should be the blurness and sharpness of the image.

Community
  • 1
  • 1
2vision2
  • 4,933
  • 16
  • 83
  • 164
  • Do you mean blurred image and sharpened image? – sgarizvi Dec 20 '12 at 06:52
  • See: http://docs.opencv.org/modules/imgproc/doc/filtering.html And for sure you can implement your own, you need a little background on Image Processing, you can use convolution for example: http://beej.us/blog/data/convolution-image-processing/ – Maroun Dec 20 '12 at 06:52
  • 2
    @MarounMaroun he wants to detect and measure the blur in some way, he is not looking for a method for apply the blur. By the way I see no point in this request, i can't imagine why this should work or be useful. – user1849534 Dec 20 '12 at 06:54
  • I misunderstood the question. Sorry, my fault. – Maroun Dec 20 '12 at 06:54
  • Do you have definitions for these two concepts? I can't imagine "blurness" would make much sense without a "perfect" reference picture. – juanchopanza Dec 20 '12 at 06:59
  • @MarounMaroun user1849534 is right. Is there a way. Can you please guide me. – 2vision2 Dec 20 '12 at 07:04
  • No idea about OpenCV, but I'd look at doing an FFT - high frequencies would indicate sharpness. – JasonD Dec 20 '12 at 08:14
  • There was some research on this topic, but it's good to know that there is no perfect measurement algorithm for blurness/sharpness because you cannot accurately define what blurness is. A blurred picture will have more entropy than that of a clear, crisp flower standing in front of a white, featureless wall. Tony-D's answer offers a good direction, but you definitely are not going to have a product out of it - it's a dead end good for a school project. – Sam Dec 20 '12 at 08:37
  • @sammy Thanks for your reply. There is a code snippet in this link stackoverflow.com/questions/7765810/… a function named GetSharpness. Does it calculates the sharpness of the image? your comments on it? – 2vision2 Dec 20 '12 at 09:42
  • @JasonD Thanks for your reply. There is a code snippet in this link stackoverflow.com/questions/7765810/… a function named GetSharpness. Does it calculates the sharpness of the image? your comments on it? – 2vision2 Dec 20 '12 at 09:47

2 Answers2

3

I don't know about opencv.

If I were trying to get an approximate measurement of where an imagine is on the sharp-to-blurry spectrum, I'd start from the observation that the sharpness of parts of an image is evident from the contrast between adjacent pixels - something like max(c1 * abs(r1 - r2), c2 * abs(g1 - g2), c3 * abs(b1 - b2)) where c1-3 weigh perceptual importance of each of the red, green and blue channels, and the two pixels are (r1,g1,b1) and (r2,g2,b2)).

Many tweaks possible, such as raising each colour's contribution to a power to emphasise changes at the dark (power <1)or bright (power >1) end of the brightness scale. Note that the max() approach considers sharpness for each colour channel separately: a change from say (255,255,255) to (0,255,255) is very dramatic despite only one channel changing.

You may find it better to convert from RBG to another colour representation, such as Hue/Saturation/Value (there'll be lots of sites online explaining the HSV space, and formulas for conversions).

Photographically, we're usually interested in knowing that the in-focus part of the image is sharp (foreground/background blur/bokeh due to shallow depth of field is a normal and frequently desirable quality) - the clearest indication of that is high contrast in some part of the image, suggesting you want the maximum value of adjacent-pixel contrasts. That said, some focused pixtures can still have very low local contrasts (e.g. a picture of a solid coloured surface). Further, damaged pixel elements on the sensor, dirt on the lens/sensor, and high-ISO / long-exposure noise may all manifest as spots of extremely high contrast. So the validity of your result's always going to be questionable, but it might be ball-park right a useful percentage of the time.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
3

I recommend you to make a frequential analysis of the image. Energy in high band will tell you that the image is quite sharpened, while energy in low band usually means that image is blurry. For computing spectrum, you can use FFTW library.

Regards,

Didac Perez Parera
  • 3,734
  • 3
  • 52
  • 87
  • Thanks for your reply. There is a code snippet in this link http://stackoverflow.com/questions/7765810/is-there-a-way-to-detect-if-an-image-is-blurry a function named GetSharpness. Does it calculates the sharpness of the image? your comments on it? – 2vision2 Dec 20 '12 at 09:41
  • Yes! that's it. These convolution is a sharpness metric of an image. Another easy option is to blur your image using a Gaussian filter or Laplacian and then compute the difference between the result and the original image. If the difference >> 0 means that the original image was already blurry! otherwise the image was sharpened. Computer vision is like this... two thousand ways to do the same... well, not the same at all :-) – Didac Perez Parera Dec 20 '12 at 09:52