MATLAB: How use formula for image vignetting

Image Processing Toolboxshadingvignetting

I want use this code for image vignnetting
i=imread('flower.jpg')
im=double(i)
[x,y,z]=size(im);
c=im(round(x/2),round(y/2));
%pix_1 = [p11,p12];
%pix_2 = [p21,p22];
%distance = sqrt( (p21-p11)^2 + (p22-p12)^2 );
distane=sqrt((x1-cx)^2 + (y1 - cy)^2)
x1,y1 ar cx, cy er distance - sqrt((x1-cx)^2 + (y1 - cy)^2);

Best Answer

Lots wrong with that, starting with this:
[x,y,z]=size(im);
The size function does not return the size of the image in that order. It returns the size in the order [y, x, z] because it returns [rows, columns, slices]. Plus if you don't ask for the third dimension (you did but many others don't), you're in trouble. See this link for more info: Steve Eddin's blog about the size function
Try this corrected code:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
fprintf('Beginning to run %s.m ...\n', mfilename);
% Original, bad code from original poster.
% i=imread('flower.jpg')
% im=double(i)
% [x,y,z]=size(im);
% c=im(round(x/2),round(y/2));
% %pix_1 = [p11,p12];
% %pix_2 = [p21,p22];
% %distance = sqrt( (p21-p11)^2 + (p22-p12)^2 );
% distane=sqrt((x1-cx)^2 + (y1 - cy)^2)
% x1,y1 ar cx, cy er distance - sqrt((x1-cx)^2 + (y1 - cy)^2);
% 0 Comments
% Corrected code:
rgbImage = imread('flower.jpg');
rgbImage = imread('peppers.png');
[rows, columns, numberOfColorChannels] = size(rgbImage)
subplot(2, 2, 1);
imshow(rgbImage);
impixelinfo;
title('Original Color Image', 'FontSize', 20);
axis('on', 'image');
% Get vignetting pattern (not really, this is not the right formula, but let's just pretend this function is it).
[x, y] = meshgrid(1:columns, 1:rows);
distanceImage = sqrt((x-columns/2).^2 + (y - rows/2).^2);
% Normalize to a percentage so it's in the 0-1 range:
distanceImage = 1 - distanceImage / max(distanceImage(:));
% Get a profile through the middle and plot it.
yMid = int32(rows/2);
horizontalProfile = distanceImage(yMid, :);
subplot(2, 2, 3);
plot(horizontalProfile, 'b-', 'LineWidth', 2);
title('Horizontal Profile', 'FontSize', 20);
xlabel('Column', 'FontSize', 20);
ylabel('Attenuation Factor', 'FontSize', 20);
grid on;
% Convert to color
if numberOfColorChannels == 3
distanceImage = cat(3, distanceImage, distanceImage, distanceImage);
end
subplot(2, 2, 2);
imshow(distanceImage, []);
yline(yMid, 'Color', 'b', 'LineWidth', 2);
impixelinfo;
axis('on', 'image');
title('Distance Image', 'FontSize', 20);
% Multiply the images together.
outputImage = uint8(distanceImage .* double(rgbImage));
subplot(2, 2, 4);
imshow(outputImage, []);
axis('on', 'image');
title('Output Image', 'FontSize', 20);
impixelinfo;
By the way, that's not a true optical vignetting function. Actually it will be much flatter in the middle and fall off much more sharply at the edge if the aperture gets in the way, or if the aperture is not in the way (and you have just general/typical lens shading), it will be much flatter - it won't fall off to zero.
Related Question