
how use formula for image vignetting
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
waheed
am 24 Jul. 2020
Bearbeitet: Image Analyst
am 14 Nov. 2020
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);
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 24 Jul. 2020
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.
2 Kommentare
Yogesh Bhambhwani
am 13 Nov. 2020
Hi I am working on a similiar problem as well I used a loop to get the pixel distance and then i divided it by the max to get r which i plugged into VinColor = Color(my image) * (1-r^2). All i get when i display VinColor is a black image how do I fix that.
my work:
Color = imread('sunflower.jpg');
Center = size(Color)/2+.5;
[l,h,~] = size(Color);
for x=1:l
for y=1:h
d = sqrt((x-Center(1))^2+(y-Center(2))^2);
end
D = 246.9261
r = d./D
end
imshow(Color)
VinColor = uint8(double(Color) .* (1-r));
figure(2)
imshow(VinColor)
Image Analyst
am 14 Nov. 2020
Bearbeitet: Image Analyst
am 14 Nov. 2020
Yogesh, try it this way:
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);
echo off;
%===============================================================================
% Read in RGB Image image.
baseFileName = 'peppers.png';
rgbImage = imread(baseFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);
% Display the original image.
subplot(2, 2, 1);
imshow(rgbImage);
axis on;
caption = sprintf('Original Image : %s', baseFileName);
title(caption, 'FontSize', fontSize);
impixelinfo;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0.1, 1, 0.9]);
[rows, columns, numberOfColorChannels] = size(rgbImage); % Need all 3 outputs of size, unlike what you did, getting only 2.
Center = [rows/2, columns/2] +.5;
for row=1:rows
for col=1:columns
vignette(row, col) = sqrt((row-Center(1))^2+(col-Center(2))^2);
end
end
% Normalize 0 to 1.
vignette = vignette / max(vignette(:));
% Invert so it's 1 at the middle instead of 0
vignette = 1 - vignette;
% Make the vignette mask RGB.
vignette = cat(3, vignette, vignette, vignette);
subplot(2, 2, 2);
imshow(vignette);
axis on;
caption = sprintf('Vignette Image');
title(caption, 'FontSize', fontSize);
impixelinfo;
% Multiply the images.
VinColor = uint8(double(rgbImage) .* vignette);
subplot(2, 2, 3);
imshow(VinColor);
title('Multiplied Together', 'FontSize', fontSize);
impixelinfo;
fprintf('Done running %s.m ...\n', mfilename);

However, note that actual vignetting in the real world does not use that formula you chose. In the real world it depends on the quality of the lens and how far the aperture stop is from the principal planes (sorry for the optics lingo, but that's what it depends on). The further the aperture stop (iris diaphragm), the more the vignetting pattern will look like a smaller blurred circle. If the aperture stop is at the principal plane, it will darken the image uniformly (because that's pretty much the definition of principal plane) however the glass of the lens may still introduce a shading pattern. As the aperture moves away from the principal plane of the lens, the more you'll see the image of the stop (a circle or multi-leaf iris diaphragm). It will get smaller and sharper as you move it away until eventually it would be just like looking through a sharp hole in an opaque screen or card. As you bring it closer, it will get larger and blurrier until it disappears when the stop is at the principal plane.
Weitere Antworten (0)
Siehe auch
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!