MATLAB: How to make intensity attenuated image or defocused image

digital image processingfilterimage processing

when an image is imaged through a lens, if the screen is on the imaging point then focused image would be shown. But the screen is on the wrong distance then blurred image should be shown. I want a know how to make this situation respect to the distance between the lens and the screen. Actually to make focused image is easy cause it can be solved just pixel mapping but I want to know how to make blurred image when it is defocused. Moreover when two images, which are located different location from a lens, are imaged through a lens then one image is focused and the other is defocused on the fixed screen position. (the pixel on the original image will be magnified respect to the magnification of the lens. I can handel the magnification thing.)

Best Answer

Your question is not very clear, but I think you want to know how to take a focused image and generate a new image that simulates the situation when the camera's lens is defocused.
The basic approach is to convolve the focused image with the point spread function of the lens. What you use for the PSF depends on how accurate you want to be, but a simple approximation might be OK. Thus you might approximate the PSF with a circular disk, whose radius depends on the amount of defocusing needed, like this:
% some data - focused image, grey levels in range 0 to 1
focused_image = double(rgb2gray(imread('saturn.png')))/256;
% approximate psf as a disk
r = 10; % defocusing parameter - radius of psf
[x, y] = meshgrid(-r:r);
disk = double(x.^2 + y.^2 <= r.^2);
disk = disk./sum(disk(:));
defocused_image = conv2(focused_image, disk, 'valid');
imshow(defocused_image);
Another possible PSF is a Gaussian function. An easy way to apply that is to use gsmooth2 from this FEX contribution.
Related Question