0

我正在尝试仅使用左右邻居计算每个像素的平均值,但在处理结束时我只得到一张白色图像,我找不到我的错误在哪里。这是我的代码

imageIn = imread('Prueba.jpg');
imageIn = rgb2gray(imageIn);
imageOut = zeros(size(imageIn));

ny = size(imageIn, 1);
nx = size(imageIn, 2);

imshow(imageIn);

u = [];
v = [];

tic

for i = 1:ny
    u = imageIn(i,:);
    v = zeros(1, ny);
    for k = 2:ny-1
        v(k) = (uint32(u(k-1))+uint32(u(k))+uint32(u(k+1)))/3;
    end
    %Special cases first and last pixel
    v(1) = (uint32(u(2))+uint32(u(1))+uint32(u(2)))/3;
    v(ny) = (uint32(u(ny-1))+uint32(u(ny))+uint32(u(ny-1)))/3;
    imageOut(i,:) = v;
end

toc
imshow(imageOut);

有任何想法吗?

4

3 回答 3

4

将代码的最后一行更改为imagesc(imageOut),您会看到图像实际上不是白色的。

你的代码很好;使用该imshow()函数图像显示为白色的原因是因为在应用局部平均值后,像素强度的范围要小得多,并且使用的默认缩放imshow()不足以显示图像的对比度。

阅读有关 b/t 的差异imshow()imagesc()您会看到混乱。

于 2012-11-10T19:57:04.907 回答
0

为什么不直接创建一个第二个矩阵,它是第一个矩阵的克隆,将其移过来,然后对这两个矩阵求平均?

imIn = imread('Prueba.jpg');

nx = size(d,1);
ny = size(d,2);

% Create temporary matrices padded with nan
tmp1 = [nan(ny,2), d];
tmp2 = [d, nan(ny,2)];

imOut = tmp1;
imOut(:,:,2) = tmp2;

% use nanmean so the mean is just the value of the 1 column
imOut = nanmean(imOut,3);

out = imOut(2:end-1,:);
于 2012-11-10T19:23:41.123 回答
0

尝试使用这个

imageIn = imread('Prueba.jpg');
imageIn = rgb2gray(imageIn);
imageOut = zeros(size(imageIn));

ny = size(imageIn, 1);
nx = size(imageIn, 2);

imshow(imageIn);

u = [];
v = [];

tic

for i = 1:ny
u = imageIn(i,:);
v = zeros(1, ny);
for k = 2:ny-1
v(k) = (uint32(u(k-1))+uint32(u(k))+uint32(u(k+1)))/3;
end
%Special cases first and last pixel
v(1) = (uint32(u(2))+uint32(u(1))+uint32(u(2)))/3;
v(ny) = (uint32(u(ny-1))+uint32(u(ny))+uint32(u(ny-1)))/3;
imageOut(i,:) = v;
end

toc

imshow(图像输出);

于 2012-11-10T19:37:16.110 回答