This picture:
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
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);
Z Pos Sharpness
-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?
Additional background: According to the comments, I noticed I have to clarify where I got this set of pictures. Actually they are not raw ouput of camera. They are from a software assisting laser marking. This software has a child window showing the grayscale real-time image of camera. The software has following features: 1. move camera to a certain position; 2.adjust the brightness and contrastness of the image; 3. save the image. When I capture the series of images, I first fix brightness and contrast setting, then move camera in Z direction consecutively, then click 'save the image' after each move. And the image showing in the window is stored in a series of .bmp files.
So in short, I captured images that were captured by a software. The raw image is already processed by grayscale, brightness and contrastness. I will add the new algorithm to this software once it's done, then the input to the algorithm will be raw output of camera. But currently I don't have bottom interface. And I believe the processing won't get in the way of coping with time-varying brightness and noise.
However, the processing by software is one factor interfering with the sharpness algorithm. It sets the 'easy or hard mode'. With high brightness and contrast setting the origial algorithm with only Sobel works fine. But with low brightness and contrast setting, the picture is showing less information, the time-varying brightness and noise comes into power. These are different types of factors from the software brightness and contrast setting, which is a fixed pipeline. They are intrinsic features of the image. In other words, with the brightness and position setting fixed, the image showing in the window is changing by itself in brightness and noise, whether randomly or in certain frequency. So when I 'save the image', the brightness and noise variance creeps in.
The two pictures at the top, are two pictures in .bmp captured at ajacent Z position with a difference of 0.02mm. I expect them to change only in sharpness, but the left one let the demon in and is reluctant to reveal its true self.