1
$\begingroup$

This picture

enter image description here

shows two photos captured by a camera from a black photographic paper. The cross is marked by laser. The left one shows a 9 pixel pattern of noise.

enter image description here

This gets in the way of auto-focus process.

Backgroud: My boss asked me to improve the auto-focus algorithm of a camera to a higher precision (say from 1mm to 0.01mm). This auto-focus process is a preparation stage for laser marking.

The original algorithm uses "Sobel" to calculate sharpness and compare the sharpness of photos at consecutive camera distance to see which one corresponds to the distance nearest to focal length.

Sobel(im_gray, grad_x);convertScaleAbs(grad_x, abs_grad_x);
Sobel(im_gray, grad_y);convertScaleAbs(grad_y, abs_grad_y);
addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad);
for (int i = 0; i < grad.rows; i++)
    for (int j = 0; j < grad.cols; j++)
       sharpness += = grad.at<unsigned char>(i, j);

This algorithm works fine for complicated photo (with higher brightness and more info), despite the noise, the sharpness value changes monotonically. But for simple photo (with less brightness and less info), the sharpness value doesn't change monotonically.

I first noticed brightness variance gets in the way of calculating the correct sharpness so I used histogram equalization (already tried "BrightnessAndContrastAuto", not working), and it improves the result to some extent.

equalizeHist(im_gray, im_gray);

After inspecting the problematic shapness values, I realized the noise is another interfering factor. So I used GaussianBlur both of size 3x3 and 5x5 to denoise (already tried "fastNlMeansDenoising", not working ) before histogram equalization. Still there are problematic sharpness values ( certain values break the monotonic trend).

GaussianBlur(im_gray, im_gray, Size(5, 5), 0);
result: 
-0.2    41.5362
-0.18   41.73
-0.16   41.9194
-0.14   42.2535
-0.12   42.4438
-0.1    42.9528
-0.08   **42.6879**
-0.06   43.4243
-0.04   43.7608
-0.02   43.9139
0       44.1061
0.02    44.3472
0.04    44.7846
0.06    44.9305
0.08    45.0761
0.1     **44.8107**
0.12    45.1979
0.14    45.7114
0.16    45.9627
0.18    46.2388
0.2     46.6344

To sum up,my current algorithm is as follows:

GaussianBlur(im_gray, im_gray, Size(5, 5), 0);
equalizeHist(im_gray, im_gray);
Sobel(im_gray, grad_x);convertScaleAbs(grad_x, abs_grad_x);
Sobel(im_gray, grad_y);convertScaleAbs(grad_y, abs_grad_y);
addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad);
for (int i = 0; i < grad.rows; i++)
    for (int j = 0; j < grad.cols; j++)
       sharpness += = grad.at<unsigned char>(i, j);

Question: Could someone tell me how I can remove the noise by adjusting the sigma or size parameter of GaussianBlur or using another denoising algorithm?

$\endgroup$
  • $\begingroup$ you can use canny $\endgroup$ – MimSaad Jul 14 '17 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.