我需要从输入中获取一个方形区域,并对其应用平均滤波器。 试过这个,但是给出了错误:
I=imread('img1.jpg');
h=fspecial('average');
figure;
h_img = imshow(I);
sq=imrect();
mask = createMask(sq,h_img);
I2 = roifilt2(h,I,mask);
最后一行给出错误。
答案 0 :(得分:0)
我想我知道这个问题 您可能正在尝试将滤镜应用于RGB图像。
检查以下代码示例:
I = imread('peppers.png');
h = fspecial('average', 10);
figure;
h_img = imshow(I);
sq=imrect();
mask = createMask(sq,h_img);
if (ndims(I) == 3)
classI = class(I);
if (isequal(classI, 'uint8'))
I = double(I)/255; %Convert I to double before applying filter.
end
I2 = zeros(size(I));
I2(:,:,1) = roifilt2(h,I(:,:,1), mask); %Filter Red plain
I2(:,:,2) = roifilt2(h,I(:,:,2), mask); %Filter Green plain
I2(:,:,3) = roifilt2(h,I(:,:,3) ,mask); %Filter Blue plain
if (isequal(classI, 'uint8'))
I2 = uint8(round(double(I2)*255)); %Convert I2 to uint8 after applying filter.
end
end
figure;imshow(I2);