matlab图像操作与空间滤波

使用matlab进行图片操作与空间滤波

两幅尺寸大小不同的图像进行连接

例如,两幅图像的高(宽)不同,把高度(宽度)较大的图像等比例缩小,使得两幅图像的高度(宽度)一致,然后把两幅图像横着(竖着)连接起来并显示。

img1 = imread(path1);
img2 = imread(path2);
[h1, w1, i1] = size(img1);
[h2, w2, i2] = size(img2);
if h1 < h2
    img2 = imresize(img2, [h1, w2]);
    img3 = [img1, img2];  % 横向连接
    imshow(img3)
else
    img1 = imresize(img1, [h2, w1]);
    img3 = [img1; img2];  % 竖直连接
    imshow(img3)
end

matlab图像操作与空间滤波

把图片水平和竖直翻转
function task4(path)
img = imread(path);
subplot(1, 3, 1);imshow(img);title('原图');
% 水平翻转
img_h = fliplr(img);
subplot(1, 3, 2);imshow(img_h);title('水平翻转');

% 竖直翻转
img_v = flipud(img);
subplot(1, 3, 3);imshow(img_v);title('竖直翻转');

matlab图像操作与空间滤波

使用均值滤波

关于imfilter函数的使用介绍:imfilter()

function task5(path)
img = imread(path);
subplot(2, 3, 1);imshow(img);title('原图');

filter5 = fspecial('average', 5);
img5 = imfilter(img, filter5, 'corr', 'replicate', 'same');
subplot(2, 3, 2);imshow(img5);title('5*5均值滤波');

filter9 = fspecial('average', 9);
img9 = imfilter(img, filter9, 'corr', 'replicate', 'same');
subplot(2, 3, 3);imshow(img9);title('9*9均值滤波');

filter15 = fspecial('average', 15);
img15 = imfilter(img, filter15, 'corr', 'replicate', 'same');
subplot(2, 3, 4);imshow(img15);title('15*15均值滤波');

filter35 = fspecial('average', 35);
img35 = imfilter(img, filter35, 'corr', 'replicate', 'same');
subplot(2, 3, 5);imshow(img35);title('35*35均值滤波');

matlab图像操作与空间滤波

使用中值滤波

中值滤波可以降低图片的椒盐噪声。

function task6(path)
img = imread(path);
subplot(1, 2, 1);imshow(img);title('原图');
% imshow(img)
% 进行3 * 3中值滤波
img_mid = medfilt2(img, [3, 3]);
subplot(1, 2, 2);imshow(img_mid);title('3*3中值滤波');

matlab图像操作与空间滤波