的数据
我的目标是获得给定的照片,其中包括黑色的左上部分和白色的右下部分。在照片中,可以清楚地看到,黑色(0)将是白色颜色(255),通过对角线以及x和y坐标。这是在matlab中通过数字图像处理完成的,但我不能100%得到它。1)你应该一张照片作为灰度照片(在代码grayImage2).然后,你需要应用你的程序,以获得我上面提到的结果,以获得照片. 2)此外,我的灰度图像大小是400x400.你能帮我纠正代码?提前感谢.
[x,y,z]= size(grayImage2);
centerX2 = y;
centerY2 = x;
radius2 = 0; % Change this value to set the desired radius
% Calculate the maximum radius from the center to the corners
maxRadius = sqrt((centerX2 - 1)^2 + (centerY2 - 1)^2);
% Iterate through the diagonal and change pixel values to gray tones
% Iterate through all pixels
for i = 1:x
for j = 1:y
if(j>=256)% because of being 400 for one edge for size
% Calculate the distance from the center to the current pixel
distance2 = sqrt((j - centerX2)^2 + (i - centerY2)^2);
% Calculate the gray value based on the distance
grayValue = uint8((distance2 / maxRadius) * (255-(400-j)));
% Set the pixel to the calculated gray value
grayImage2(i, j) = grayValue;
else
% Calculate the distance from the center to the current pixel
distance2 = sqrt((j - centerX2)^2 + (i - centerY2)^2);
% Calculate the gray value based on the distance
grayValue = uint8((distance2 / maxRadius) * (255-(255-j+1)));
% Set the pixel to the calculated gray value
grayImage2(i, j) = grayValue;
end
end
end
figure;
% Display the grayscale image
imshow(grayImage2);
title('Grayscale Image with Radial Gradient');
字符串
1条答案
按热度按时间ecfsfe2w1#
看起来你的思路是对的,但是看起来有点复杂。像这样的怎么样?
字符串