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

hsgswve4  于 6个月前  发布在  Matlab
关注(0)|答案(1)|浏览(85)


的数据
我的目标是获得给定的照片,其中包括黑色的左上部分和白色的右下部分。在照片中,可以清楚地看到,黑色(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');

字符串

ecfsfe2w

ecfsfe2w1#

看起来你的思路是对的,但是看起来有点复杂。像这样的怎么样?

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)

字符串

相关问题