2

This picture:

img

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

img

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.

ricecakebear
  • 301
  • 4
  • 15
  • The results may not be perfect, but do appear to show a clear trend. Is there a peak and does it correspond to best focus? – DisappointedByUnaccountableMod Jul 14 '17 at 09:33
  • Can you increase the contribution from the black/white transitions (which presumably you want to be the main contributor to the sharpness value) by reducing the spacing between the white lines? Can you subtract the noise by using a reference picture? Why is there the bloom there anyway if the thing your camera is looking at is supposed to be black/white? – DisappointedByUnaccountableMod Jul 14 '17 at 09:39
  • 0.01mm? good luck with that... – Piglet Jul 14 '17 at 09:47
  • I would suggest a size of 3x3 since you have only one pixel in between the noisy ones. Also you may try other de noising techniques like median filter. Maybe some localized filter will work better, one that targets this pattern, because most of the filter may alter the image per se, and you may get worst results ( like blur ) – api55 Jul 14 '17 at 11:31
  • Interesting -- is the left part the raw input image, or was it already processed somehow? (Could you post a full raw input image, please?) Noise I would expect to be random, whereas this seems like a regular grid aligned with image edges -- more like an artefact. I'd focus on investigating where it comes from and try to develop a model. What sort of camera (model, color/mono, etc.) and optics? How is it set up? What sort of image format do you capture the data in and what file format do you use to store it? – Dan Mašek Jul 14 '17 at 18:19
  • @barny Hey barny. 0.02 maybe correspond to the best focus, and I have no way to verify that. I don't quite understand "reducing the spacing between white lines", which "white lines" are you referring to. And could you elaborate on "subtracting noise"? I add some additional background in my post for your reference. – ricecakebear Jul 17 '17 at 01:00
  • @Piglet Thanks, I need that. Finding the focus of camera is to help find focus of laser. Laser is very sensitive to that stuff. – ricecakebear Jul 17 '17 at 01:03
  • @DanMašek I add some additional background in my post. The camera is a cheap camera, the noise maybe come from CCD as temperature increases. – ricecakebear Jul 17 '17 at 01:07
  • @api55 I tried "medianBlur(im_gray, im_gray, 9);" and "GaussianBlur(im_gray, im_gray, Size(3,3),0);" as you suggested, problematic values showed at the same spot (-0.08 and 0.1), which are the noisy ones. – ricecakebear Jul 17 '17 at 02:01
  • I wouldn't suggest blur, blur will soften the noise, but also blur the laser (similar to defocusing).... maybe you should try to elaborate your solution differently, for instance, if it is detecting the laser markers, maybe some finding some long lines in the image or maybe doing threshold or something similar may work better. – api55 Jul 17 '17 at 06:43
  • what's your depth of field? what's the resolution of your focussing drive? – Piglet Jul 17 '17 at 07:35
  • @Piglet The camera is BASLER acA2440-75μm。 – ricecakebear Jul 17 '17 at 08:50
  • If you had more white lines in the field of view you would have more edge info to contribute to the "sharpness", and if there were less of the not-black "black" (because there were more white lines) then there would be less noise to interfere with the sharpness. Read the background info you added. Sounds to me like trying to do post-processing of the already-processed image is like closing the gate after the horse has bolted - you should be working on the raw image. – DisappointedByUnaccountableMod Jul 17 '17 at 16:44
  • However if noise is unavoidable and gaussian then averaging across several (many?) images will reduce it, maybe? Or if the noise is systematic (e.g. due to variation in sensor sensitivity/black level, then maybe taking a reference (all "black") image and subtracting it from the focussing images would help, but I guess that's not going to work if the image has already been processed. – DisappointedByUnaccountableMod Jul 17 '17 at 16:44
  • @barny I agree with you on more info support better detection. But the processed image might be simulating a dimly lit environment. As to subtracting a pure noise image or averaging across adjacent images, how do I align them, since the images could be experiencing offset in X and Y direction. – ricecakebear Jul 18 '17 at 02:53
  • @api55 I'm currently trying extracting the noise by finding local maximum. This is the method I'm trying https://stackoverflow.com/questions/10621312/opencv-filter-image-replace-kernel-with-local-maximum I have to deduce the intensity of the local maximum, and make sure this works for noisy one and also not deteriorate the normal one. – ricecakebear Jul 18 '17 at 03:01
  • "How can I align a reference image" once in position, can you turn the lasers off, take a picture, then turn them back on again? "How can I average across images?" with the camera in a fixed position, take several pictures and add them together/average the pixel values, this will reduce gaussian noise. – DisappointedByUnaccountableMod Jul 19 '17 at 11:46

0 Answers0