matlab中的平均过滤器

时间:2012-11-10 19:03:22

标签: matlab image-processing filter

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

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);

有什么想法吗?

3 个答案:

答案 0 :(得分:4)

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

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

了解差异b / t imshow()imagesc(),您会看到混乱。

答案 1 :(得分: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,:);

答案 2 :(得分: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);