提问人:Johnny 提问时间:10/21/2023 最后编辑:Johnny 更新时间:10/28/2023 访问量:69
如何更正与数字图像处理相关的代码?
How can I correct my code which is related to digital image processing?
问:
我的目标是获得给定的照片,其中包括左上半部分的黑色和右下半部分的白色。在照片中,可以清楚地看到,黑色(0)通过对角线以及x和y坐标将是白色(255)。它是在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');
答:
0赞
Zak.
10/28/2023
#1
看起来你走在正确的轨道上,但看起来有点复杂。这样的事情怎么样?
image = uint8(zeros(400));
width = size(image,1);
height = size(image,2);
maxR = sqrt(width^2 + height^2);
for i = 1:width
for j = 1:height
r = sqrt(i^2 + j^2);
image(i,j) = uint8(floor(255*r/maxR));
end
end
imshow(image)
评论
grayValue = uint8((distance2 / maxRadius) * 255);