Ask Your Question

Calculating sharpest image

asked 2015-02-24 11:25:07 -0500

aries gravatar image

I am working on a project wherein I have a camera and I need to find the distance between the lens and the object to produce the sharpest image. The camera has the provision to adjust the position of the lens. I have to determine the distance from the lens and the object which can produce the sharpest image. Can anyone please provide me an idea on how this can be achieved?

edit retag flag offensive close merge delete


If you have a concept questions, not specifically OpenCV, would you assist us on this:

Royi gravatar imageRoyi ( 2015-02-26 04:15:57 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-02-24 13:53:02 -0500

matman gravatar image

updated 2015-02-24 13:54:34 -0500

There are several possibilities to find the right focus plane.

For example when you use a contrast method you can analyze your images by filtering it with a sobel filter. The sobel filter gives you the deviation of your image. Now you can, for example, sum up the deviation image and use this value for comparing: The more your image is in the focus plane the more edges are existing, the more increases you have in your deviated image, the higher is your value.

Consider this method only works on images with structure in it and you need to analyze several focus planes without changing the motive because you only get a relative value dependend of your motive.

Another contrast method is to transform your image with dft or fft an looking for the one with the most ratio of high frequencies. For this the same restrictions counts like for analyzing with a sobel filter. A third one is to shift your image by one or two pixel in X and Y, than subtract it from the origin and look for edges but I never tested this method.

edit flag offensive delete link more


@matman Thanks for the reply. I found on the internet that laplacian method is quite good technique to compute the sharpness. I was trying to implement it. How do we get the sharpness measure after applying the Laplacian function? Below is the code:

    Mat src_gray, dst;
    int kernel_size = 3;
    int scale = 1;
    int delta = 0;
    int ddepth = CV_16S;

    GaussianBlur( src, src, Size(3,3), 0, 0, BORDER_DEFAULT );

    /// Convert the image to grayscale
    cvtColor( src, src_gray, CV_RGB2GRAY );

    /// Apply Laplace function
    Mat abs_dst;

    Laplacian( src_gray, dst, ddepth, kernel_size, scale, delta, BORDER_DEFAULT );

    //compute sharpness
aries gravatar imagearies ( 2015-02-25 04:30:47 -0500 )edit

Try to average dst. Sorry I have no papers here, so I think it should something like:

//compute sharpness
int sharpnessValue = mean(dst);

and compare different focus plane images, so the sharper ones should give you higher values. I have to check this tomorrow, I didn't deal with this for a few months so its from memory.

Edit: when I watch at your code in detail the GaussianBlur strikes me. If you look for the sharpest Image so why do you filter it and make a simulated out of focus?

matman gravatar imagematman ( 2015-02-26 13:19:57 -0500 )edit

Question Tools

1 follower


Asked: 2015-02-24 11:25:07 -0500

Seen: 2,639 times

Last updated: Feb 24 '15