Reputation: 7448
I am programming automatic focusing on a microscope with programmable axis controller. For testing, I've implemented a simulation, which returns an image depending on exposure, axis position, etc. The simulation takes a good image, and distorts it - e.g. makes brighter, darker.
The primer indicator for good focus are sharp edges (works well for my type of images). Basically I sum the intensity differences between neighboirung pixels. The higher the sum, the better focus.
My question is, how to simulate unfocused image? Did anyone implemented it already? A sequence of filters would be great.
I've tried cvSmooth, but it didn't give realistic results.
PS: My current workaround is changing ROI-size inversily proportional to distance from focus-position. It works well for testig my algorithms, but not good for demonstrations - as the image doesn't change during simulation.
Upvotes: 4
Views: 1782
Reputation: 36049
(I'm not sure if stack overflow is the right site for your problem, since it is heavily domain-specific)
View your microscope within the terms of a Point Spread Function (PSF). The PSF is the function that describes the image of a point-like light source in the microscope. If you take a single plane from the 3D point spread function, you have the defocussing behaviour for this axial distance. Fold your image with the point spread function image, and you have the defocussed image. This operation is usually called "folding", "convolution" or "smooth with kernel".
Of course, you'll need a point spread function for your microscope, and there are plenty of details to consider - most importantly, the type of optics involved, the numerical aperture, etc. Ask the relevant optical literature. Sibaritas Deconvolution Microscopy seems like a good start.
Be aware that, in general, 3D defocussing involves integrating a Bessel function through the phases. You might be able to approximate the behaviour with a Gaussian mask if the axial defocus is within roughly 2 times the lateral resolution of your microscope. This is something like one or two microns on normal microscopes. For larger axial defocusings, you need to compute the integral. I doubt that this is within OpenCV's scope, so you'd need to pre-compute a defocus function.
My dissertation project rapidSTORM has an implementation for oil objectives, but the code (pixelatedBessel.cpp) is not in opencv.
Especially for high-aperture microscopes, the computations become pretty involved, however - at my lab, we usually preferred to just put a point-like light source (e.g. a quantum dot) on the microscope and let the Z stage do the job.
Upvotes: 4