如何纠正与数字图像处理相关的代码?

问题描述 投票:0回答:1

我的目标是获得给定的照片,其中左上部分为黑色,右下部分为白色。在照片中,可以清楚地看到,通过对角线以及 x 和 y 坐标,黑色 (0) 将变为白色 (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');
image matlab processing digital
1个回答
0
投票

看起来您的方向是正确的,但看起来有点复杂。像这样的怎么样?

image = uint8(zeros(400)); 
w = size(image,1); 
h = size(image,2); 
maxR = (w^2 + h^2)^(1/2); 
for i = 1:size(image,1)
    for j = 1:size(image,2)
        r = sqrt(i^2 + j^2);
        image(i,j) = uint8(floor(255*r/maxR));
    end 
end 
imshow(image)
© www.soinside.com 2019 - 2024. All rights reserved.