Coder Social home page Coder Social logo

blur-detection's Introduction

This comes from some work I did on a contract a few years ago. While I obviously can’t publish everything related to that work, I had retained the copyright to some of the low-level implementations exactly so that I could open source them later.

(N.B.: This code is now explictly licensed, per the LICENSE file.)

What follows are some notes I had written about a broader approach that was built on this code. I expect that research in this area has since surpassed this approach.

Overview

This is an attempt to synthesize an algorithm for automatic ranking of digital photographs, based on the following basic assumptions about quality:

  • good photos have the subject matter in focus;
  • the focal mass will generally lie close to the points defined by the rule of thirds;
  • the subject matter will also be separated from the background by brightness and saturation.

We note that the sharpness of a region is a reasonable predictor of focus, as most prominent subject matter contains detailed edge structures that are destroyed by out-of-focus/motion blur.

After the description of the algorithm and the work on which it is based, I discuss directions for improvement.

Review of Prior Work

Our approach broadly builds upon the approach suggested by Lim, Yen, and Wulim-yen-wu, though their goal is simply making a decision as to whether an image is out-of-focus or not.

A related set of ideas for determining the aesthetic value of an image is given by Liu et al.liu-et-al, but their focus is on reframing an existing image to capture the most aesthetic possible cropping of an image.

Description of the Algorithm

Given an image, we first decompose it into square blocks of pixels. We have chosen 64x64 pixels rather than the arbitrary 100x100 suggested in lim-yen-wu to facilitate the incorporation of Discrete Cosine Transform (DCT) information in the common case that the image is JPEG encoded.

Blockwise Metrics

For each block, we compute the hue, saturation, and intensity of each pixel. Note that it might be possible to speed up the algorithm here by skipping this computation and using only luminance values for blurred blocks.

We wish to make a boolean decision about whether this block is likely to be in focus, based on whether the sharpness of this block exceeds a threshold. In the present implementation, we have three different sharpness metrics. We will describe each, as well as a proposed unification of the methods.

Sharpness via DCT coefficients

In the case that we have access to DCT information, we can perform a simple histogram algorithm due to Marichal, Ma, and Zhang (marichal-ma-zhang). For each 8x8 DCT block in this image block, we count the occurance of coefficients greater than a minimum value in a histogram. After each DCT block has been considered, we compare each value in the histogram against the histogram value for the first coefficient (0,0), summing a weighted value when the value exceeds a threshold.

This provides an extremely rapid approximation of blur extent. In the case that this value exceeds certain thresholds, it would be possible to skip further computation of more accurate sharpness metrics.

Sharpness via a Wavelet-based Edge Detector

Tong et al.tong-li-zhang-zhang present an approach to blur estimation based on the Haar wavelet transform, whereby specific edge structures can be detected, and the ratio of Dirac and abrupt-step structures to roof and gradual step edge structures provides an acceptable estimation of the sharpness of the given block.

In our case, we have modified this algorithm to skip the windowing step, as we felt that adjusting the thresholds by which a point was classified was more effective than performing non-maximum suppression (NMS).

Unfortunately, even without NMS, this is the most computationally expensive of the algorithms implemented.

In his master’s thesis ramakrishnan-thesis, Harish Ramakrishnan presents some variants on this approach and evaluates their performance.

Sharpness via an IIR Filter

Shaked and Tastlshaked-tastl derive a model of sharpness based on the ratio between high-frequency data and low-frequency data, and present an approach to computing this metric via a pair of one-dimensional IIR filters. The advantage of this decomposable approach is that rows or columns can be skipped to speed up the process at the cost of accuracy.

Presently we use simple Butterworth high- and low-pass filters on row and column data to compute the sharpness metric. However, it seems likely that better filter designs could help improve the accuracy and performance of this model.

Alternative Sharpness Measures

A Canny edge detector could be used in much the same way as the Haar transform approach given above. It is possible that this could be more efficient.

Another alternative, discussed in ramakrishnan-thesis, is the perceptual blur metric given by Marziliano et al. in marziliano-et-al. We have thus far not considered using this blur metric as it seems less efficient than the methods we already implement.

Combining Sharpness Metrics

In general, we avoid using the IIR filter in preference to the wavelet transform approach or the DCT coefficient histogram. Ideally, we would use the fastest and least-accurate methods (DCT coefficients, IIR filter with a large row/column skip) to eliminate clearly blurry blocks, and then only use the more expensive wavelet transform approach on blocks whose sharpness isn’t evident.

Other block-level metrics

Along with a sharpness value for each block, we also compute the means of the block’s hue, intensity, and saturation values, as well as a ratio of the number of pixels having a hue similar to that of blue sky (per lim-yen-wu).

Global merits

Having computed these metrics for each block in the image, we compute several global indicators of quality. Blocks whose dominant hue is that of blue sky are ignored during this process.

We compute a composition score as a sum of the blocks which are sharp, weighted by the Manhattan distance of the block from one of the four “power points” representing intersections of the division of the image in thirds, as per the rule of thirds.

We compute brightness and saturation indices as the difference between the mean of those values for blocks considered sharp and the mean of those values for the remaining blocks.

We also compute the density of sharp blocks, as the ratio of sharp blocks to unsharp ones, and the median block sharpness.

Weighted ranking

This is where work needs to proceed: improving the quality of indices other than composition so they can be combined with appropriate weights to produce a single ranking value.

Presently we can provide a ranking based on Lim et al.’s process for deciding if an image is in focus: compute the sum of composition, brightness, and saturation weighted by density and median sharpness, as well as individual weights prioritizing composition over brightness and saturation.

Directions for Further Work

The composition weighting could be improved in a number of ways. Perhaps the Manhattan distance from a power point is insufficient, and a more sophisticated model of the rule of thirds is required. Another possible direction is incorporating the idea of balanced masses as discussed in liu-et-al; note that within our presented approach, we could simplify that scheme by considering each block a unit of mass based on its sharpness value.

Lim et al.’s sky hue ratio is questionable and it remains to be seen if it is indeed an effective metric for our purposes.

References

lim-yen-wu Suk Hwan Lim, Jonathan Yen, Peng Wu, *Detection of Out-Of-Focus Digital Photographs*, HPL-2005-14, 2005.

tong-li-zhang-zhang Hanghang Tong, Mingjing Li, Hongjiang Zhang, Changshui Zhang, *Blur detection for digital images using wavelet transform*, Proceedings of the IEEE International Conference on Multimedia & Expo, 2004.

marichal-ma-zhang Xavier Marichal, Wei-Ying Ma, Hongjiang Zhang, *Blur determination in the compressed domain using DCT information*, Proceedings of the IEEE ICIP, pp.386-390, 1999.

liu-et-al Ligang Liu, Renjie Chen, Lior Wolf, Daniel Cohen-Or, *Optimizing Photo Composition*, Computer Graphics Forum, 29: 469-478, 2010.

ramakrishnan-thesis Harish Ramakrishnan, *Detection and Estimation of Image Blur*, Master’s Thesis, 2010.

shaked-tastl Dored Shaked, Ingeborg Tastl, *Sharpness Measure: Towards Automatic Image Enhancement*, HPL-2004-84, 2004.

marziliano-et-al Marziliano, P.; Dufaux, F.; Winkler, S.; Ebrahimi, T; *A no-reference perceptual blur metric*, Proceedings of International Conference on Image Processing, 2002.

blur-detection's People

Contributors

saurabheights avatar t6nand avatar tokenrove avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

blur-detection's Issues

Change floats to doubles

Most of the uses of float in this code should be double from the point of view of accuracy, and should have negligible speed impact.

Support for other Image Formats / Opencv - Discussion Issue

Hi tokenrove,
Hope you are doing good. This is not much of an issue, but for discussion. As mentioned earlier, I have recently ported IIR technique for local block metrics computation to Opencv. This should allow testing on any image format already supported in Opencv(which I am assuming are almost all we see in our daily life).

The problem is implementing other techniques like computing

  1. Sharpness via DCT coefficients, and
  2. Sharpness via Wavelet-based Edge Detector.

The problem is computing DCT. Opencv AFAIK, doesn't provide api for extracting DCT coefficients from Image directly. One has to do DCT of decoded image(even if it was jpeg). Will this be an issue?

On side note: Meanwhile, having test suite will definitely help. I wouldn't have been able to notice the earlier bug without my test image. Please, no hurries on my part. My changes should also resolve your thoughts on making it work on stream of images.

Best Regards.

libjpeg incorrect image read inconsistency

Hi Tokenrove,
I have recently ported the C code to C++ with using only IIR filter for local metrics computation. I tested the whole code in parallel with the C code.

I noticed an issue and to further identify it, I used below image which has 4 blocks of 64x64 size. These blocks have R=G=B and block have intensity 0, 1, 16, 17(out of 255). [This was a image cropped from a larger image having intensities 0-255 blocks.]

grayscaleopencv

For second block(top-right), this is the output I got(added these lines while HSI calculation)
i = 0 j = 41 R = 0 G = 0 B = 0 [should be 1,1,1]
i = 0 j = 42 R = 0 G = 0 B = 1 [should be 1,1,1]
i = 0 j = 43 R = 1 G = 1 B = 1

This is the output for all rows of second block. Till around 41st pixel of the row, libjpeg always gives me value of 0 instead of 1.

When the image is read from Opencv, I found pixels to be of consistent intensities within a block. I even used minMaxLoc function to verify over each local block.

Please can you share any thoughts you may have on this.

Add a test suite using a publically-available image corpus

Since people are actually using this code, I have some plans for improvement. Before that can happen, we need a test suite so we can evaluate the accuracy and efficiency of the existing implementations and ensure that there are no regressions.

lim-yen-wu behaves badly on all-sky images

Looks like we get some strange results if the image is entirely sky blocks, which is unsurprising as there's no attempt to guard against this. Add a test case and something preventative (probably returning ill-focused and 0 merits).

Autofocus usage?

I wonder if this code can be used for determining focus? I work in KStars and currently we use Half-Flux-Radius method of a star centeroid to measure and adjust focusing of astronomical images, but was researching the feasibility of wavelet based methods.

License

Julian,

In your source files, you have maintained a copyright. This means that your code cannot be used open-source style; in order to be used in other projects the code must be released under an opensource license.

In the Readme you state "so that I could open source them later" and I believe you meant to opensource your code by publishing them to GH. But in the current state of the repo, the code is not open sourced, just viewable.

You have written a slick implementation of these algorithms and I would like to use them for a project of mine, but in their current state, I legally can't. I would greatly appreciate it if you would release your code under a true opensource license.

Thanks,
bsilvereagle

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.