Matlab learning 4- image processing image addition, image subtraction, noise addition

Image processing, image addition (such as image superposition, brightening color, etc.), image subtraction (such as capturing the trajectory of moving images)
environment matlab2020

Use imadd (addition), imsubtract (subtraction), imresize (change), imnoise (image noise addition)

matlab function

imadd(X, Y):
Add two images or add a constant to the image;
Z=imadd(X, Y) adds each element in the array X to the corresponding element in the array Y, and outputs the array Return the sum in the corresponding elements of Z.
imsubtract(X,Y)
subtracts one image from another or subtracts a constant from an image;
Z = imsubtract(X,Y) subtracts from each element in array X the corresponding element in array Y, and Return the difference in the corresponding elements of the output array Z.
rgb2gray()
converts an RGB image or color map to a grayscale image;
I=rgb2gray(RGB) converts a true color image RGB to a grayscale image I. Converts an RGB image to grayscale by removing hue and saturation information while preserving luminance.
imnoise(): Add noise to the image
J = imnoise(I, 'gaussian') adds zero-mean white Gaussian noise with a variance of 0.01 to the grayscale image I.
J = immoise(I, 'gaussian', m) adds Gaussian white noise with mean m and variance 0.01.
J = imnoise(I, 'gaussian', m, var_gauss) adds Gaussian white noise with mean m and variance var_gauss.
J = imnoise(I, 'localvar', var_local) adds zero-mean white Gaussian noise with local variance var_local.
J = imnoise(I, 'localvar', intensity_map, var_local) adds zero-mean white Gaussian noise. The local variance var_1ocal of the noise is a function of the image intensity values ​​in I. The mapping of image intensity values ​​to noise variance is specified by the vector intensity_map.
J = imnoise(I, 'poisson') generates Poisson noise from the data instead of adding artificial noise to the data. See Algorithms for details.
J = imnoise(I, 'salt&pepper') adds salt and pepper noise, the default noise density is 0.05. This affects about 5% of the pixels. 3 = imnoise(I, 'salt&pepper', d) adds salt and pepper noise,
where d is the noise density .This affects about d numel(I) pixels. =imnoise(I,'speckle') adds multiplicative noise using the equation ]=I+nT, where n is uniformly distributed random noise with mean 0 and variance 0.05 .J
= imnoise(I, 'speckle', var_speckle) adds multiplicative noise with variance var_speckle.

1. Image addition

The effect is as follows:
insert image description here

test.m code:

%图像加法
img1=imread("A.bmp");
img2=imread("B.bmp");
%rbg图像转变灰度图像
img3=rgb2gray(img1);
img4=rgb2gray(img2);
%灰度图像相加
img5=imadd(img3,img4);
%显示
subplot(1,3,1),imshow(img3),xlabel("A");
subplot(1,3,2),imshow(img4),xlabel("B");
subplot(1,3,3),imshow(img5),xlabel("A+B");

2. Image Fusion

The effect is as follows:
insert image description here

test.m code:

%图像融合
img1=imread("man.tif");
img2=imread("test1.png");
%修改图像大小
% img1=imresize(img1,[200,200]);
% img2=imresize(img2,[200,200]);

%图像相加,x,y必须具有相同大小和类
img3=imadd(img1,img2);

subplot(1,3,1),imshow(img1),xlabel("A");
subplot(1,3,2),imshow(img2),xlabel("B");
subplot(1,3,3),imshow(img3),xlabel("A+B");

3. Image fusion, using specific parameters

The effect is as follows:
insert image description here

test.m code:

%图像融合,使用具体参数
img1=imread("test1.png");

%图像相加
img2=imadd(img1,50);
img3=imadd(img1,-50);

subplot(1,3,1),imshow(img1),xlabel("原图像");
subplot(1,3,2),imshow(img2),xlabel("增加亮度后的图像");
subplot(1,3,3),imshow(img3),xlabel("减弱亮度后的图像");

4. Image restoration

The effect is as follows:
insert image description here

test.m code:

%图像还原
%原始图像
img=imread("test.png");
img=rgb2gray(img);
subplot(2,3,1),imshow(img),xlabel("(a)原图像");
[m,n]=size(img);

%加噪图像(被还原图像)
img1=imnoise(img,"gaussian",0,0.01);%叠加零均值高斯噪声(方差为0.01subplot(2,3,2),imshow(img1),xlabel("(b)加噪图像");

%2幅同类图像进行相加平均
J=zeros(m,n);
J=double(J);
for i=1:2
    X=imnoise(img,"gaussian");
    Y=double(X);
    J=J+Y/2;
end
subplot(2,3,3),imshow(mat2gray(J)),xlabel("(c)2幅图像平均");
%4幅同类图像进行相加平均
J=zeros(m,n);
J=double(J);
for i=1:4
    X=imnoise(img,"gaussian");
    Y=double(X);
    J=J+Y/4;
end
subplot(2,3,4),imshow(mat2gray(J)),xlabel("(d)4幅图像平均");
%8幅同类图像进行相加平均
J=zeros(m,n);
J=double(J);
for i=1:8
    X=imnoise(img,"gaussian");
    Y=double(X);
    J=J+Y/8;
end
subplot(2,3,5),imshow(mat2gray(J)),xlabel("(e)8幅图像平均");
%16幅同类图像进行相加平均
J=zeros(m,n);
J=double(J);
for i=1:16
    X=imnoise(img,"gaussian");
    Y=double(X);
    J=J+Y/16;
end
subplot(2,3,6),imshow(mat2gray(J)),xlabel("(f)16幅图像平均");

5. Image subtraction

The effect is as follows:
insert image description here

test.m code:

%图像减法
img1=imread("A.bmp");
img2=imread("B.bmp");
%修改图像大小
% img1=imresize(img1,[200,200]);
% img2=imresize(img2,[200,200]);

%图像相减,要求图像矩阵相同大小
img3=imsubtract(img1,img2);
subplot(1,3,1),imshow(img1),xlabel("A");
subplot(1,3,2),imshow(img2),xlabel("B");
subplot(1,3,3),imshow(img3),xlabel("A-B");

6. Image subtraction, capture image motion trajectory

The effect is as follows:
insert image description here

test.m code:

%图像减法,运动图像轨迹
img1=imread("F4_8a.bmp");
img2=imread("F4_8b.bmp");
img3=imread("F4_8c.bmp");
subplot(2,3,1),imshow(img1),xlabel("a");
subplot(2,3,2),imshow(img2),xlabel("b");
subplot(2,3,3),imshow(img3),xlabel("c");

%图像相减
img4=imsubtract(img1,img2);
img5=imsubtract(img2,img3);
img6=imsubtract(img1,img3);


subplot(2,3,4),imshow(img4),xlabel("a-b");
subplot(2,3,5),imshow(img5),xlabel("b-c");
subplot(2,3,6),imshow(img6),xlabel("a-c");

Material: https://gitee.com/CYFreud/matlab/tree/master/image_processing/demo4_220328

Guess you like

Origin blog.csdn.net/CHengYuP/article/details/123816014