Algebraic operations and logical operations of images - Matlab implementation

Algebraic operations and logical operations of images - Matlab implementation

1. Addition operation

Addition operations are often used in various situations such as average noise reduction. Image addition is generally used to average multiple images of the same scene to effectively reduce additive noise. When a picture is interfered by a random noise source, the noise can be eliminated or reduced by averaging multiple still images.During the averaging process, the static part of the image will not change, and since the noise of the image is random, different noise patterns accumulate very slowly, so the random noise can be reduced by averaging multiple images. Influence.

If M images are averaged, the larger M is, the better the noise reduction effect will be.

Add Gaussian noise to an image, and then remove the noise through multiple additions and averages . The matlab program is as follows:

%% 把一副图像加上高斯噪声,再通过多次相加求平均的方法去除噪声
% 验证相加次数和降噪效果是否成正比
clc;clear;close all;    % 初始化
I=imread('eight.tif');
J=imnoise(I,'gaussian',0,0.02); % 向这副图片加入高斯噪声
subplot(231);imshow(I);title('原图');
subplot(232);imshow(J);title('加噪声');
[M,N]=size(J);  K=zeros(M,N);   % 产生全零的矩阵,大小与图片的一样
a=100;
for i=1:a
    J=imnoise(I,'gaussian',0,0.02);
    J1=im2double(J);
    K=K+J1; % 相加
end
K=K/a;  % 求平均值
subplot(233);imshow(K);title([num2str(a),'次求平均后的图']);

The running results are as follows:
Insert image description here

When the average number is greater, the noise reduction effect is better.

Detailed explanation of the imnoise function involved in the code:

The function imnoise in MATLAB means adding noise to pollute an image, which is called the noise pollution image function.

g = imnoise(I,type)
g = imnoise(I,type,parameters)
g = imnoise(I,‘gaussian’,m,v)
g = imnoise(I,‘localvar’,V)
g = imnoise(I,‘localvar’,image_intensity,var)
g = imnoise(I,‘poisson’)
g = imnoise(I,‘salt & pepper’,d)
g = imnoise(I,‘speckle’,v)

f is the input image. The function imnoise converts the image into a double image in the range [0,1] before adding noise to it. This must be taken into account when specifying noise parameters.

g=imnoise(f,'gaussian',m,var) adds Gaussian noise with mean m and variance var to image f. The default value is noise with mean m 0 and variance var 0.01.

g=imnoise(f,'localvar',V) adds Gaussian noise with a mean of 0 and a local variance of V to the image f, where V is an array of the same size as f, which contains the ideal square of each point. difference.

g=imnoise(f,'localvar',image_intensity,var) adds Gaussian noise with mean 0 to image f, where the local variance of the noise var is a function of the intensity value of image f. The parameters image_intensity and var are vectors of the same size. plot(image_intensity,var) plots the functional relationship between noise variance and image brightness. The vector image_intensity must contain normalized intensity values ​​in the range [0,1].

g=imnoise(f,'salt & pepper',d) pollutes image f with salt and pepper noise, where d is the noise density (i.e. the percentage of the image area that includes noise values). Therefore, approximately d*numel(f) pixels are affected. The default noise density is 0.05.

g=imnoise(f,'speckle',var) adds multiplicative noise to the image f using the equation g=f+n*f), where n is uniformly distributed random noise with mean 0 and variance var, var The default value is 0.04.

g=imnoise(f,'poisson') generates Poisson noise from the data instead of adding artificial noise to the data. In order to comply with Poisson statistics, the brightness of the unit8 and unit16 class images must match the number of photons. When the number of photons per pixel is greater than 65535, a double-precision image is used. The brightness value varies between 0 and 1 and corresponds to the number of photons divided by 10e12.

2. Subtraction operation

Image subtraction is often used to detect changing and moving objects. Image subtraction operation is also called image difference operation . Difference methods can be divided into simple difference methods in controlled environments and difference methods based on background models. In a controlled environment, or within a short period of time, the background can be considered to be fixed, and differential operations can be used directly to detect changing or moving objects.

Subtracting images of the same scene taken at different times or images of the same scene in different wavebands is the subtraction method, which is actually the subtraction operation of the image. Difference images provide difference information between images and can be used to guide dynamic monitoring, detection and tracking of moving targets, image background elimination and target recognition, etc.

When performing subtraction operations on images, the corresponding points of the two subtracted images must be located on the same target in space. Otherwise, geometric calibration and matching must be performed first. When a scene series of images is subtracted to detect other changes, it is difficult to ensure accurate alignment, and further analysis is required.

It is known that a picture is interfered by salt and pepper noise, and the noise is extracted through subtraction operation. The matlab code is as follows:

%% 已知一副收到椒盐噪声干扰的图片,通过减法运算提取出噪声
clc;clear;close all;    % 初始化
I=imread('C:\Users\xcz\Desktop\matlab_example\Images\lena.jpg');
I1=imnoise(I,'salt & pepper',0.02);
K=imsubtract(I1,I); % 实现两幅图片相减,也可使用imabsdiff函数,从而避免负值为0
K1=255-K;   % 图像求反显示
subplot(131);imshow(I);title('原图');
subplot(132);imshow(I1);title('加椒盐噪声后');
subplot(133);imshow(K1);title('提取的噪声');

The running results are as follows:
Insert image description here

Detailed explanation of imsubtract function involved in matlab code:

The original English text is as follows
Insert image description here

Notice: The imsubtract function will intercept the negative value data after subtraction to 0; while the imabsdiff function will find the absolute difference between the two images and will not intercept the negative value to 0. Therefore, when using it, in order to avoid the occurrence of differences Negative values, and to avoid too small differences between the pixel value calculation results (if the result is negative after subtraction, it will become 0), it is recommended to call the function imabsdiff.

3. Multiplication operation

Simple multiplication operations can be used to change the gray level of the image to achieve gray level transformation. Multiplication operations can also be used to cover certain parts of the image. Its typical application is to obtain a mask image. For areas that need to be preserved, the value of the mask image is set to 1, and for areas that need to be suppressed, the value of the mask image is set to 0. Multiplication is sometimes used as a technique to implement convolution or correlation processing.

The matlab code is implemented as follows:

%% 实现图像的乘法运算
clc;clear;close all;    % 初始化
I=imread('moon.tif');
J=immultiply(I,1.2);    % 将此图片乘以1.2
K=immultiply(I,2);      % 将此图片乘以2
subplot(131);imshow(I);title('原图');
subplot(132);imshow(J);title('乘以1.2');
subplot(133);imshow(K);title('乘以2');

The running results are as follows:
Insert image description here

The immultiply function involved in the code is used for image multiplication operations. For details, see the help document.

4. Division operation

A simple division operation can be used to change the grayscale of an image. A typical application of division operations is ratio image processing. For example, the division operation can be used to correct the nonlinear effects of imaging equipment, and is used in the processing of special forms of images (such as medical images represented by CT). In addition, the division operation is used to eliminate the spatial effects of the image digitizing device.

The matlab code is implemented as follows:

%% 实现图像的除法运算
clc;clear;close all;    % 初始化
moon=imread('moon.tif');I=double(moon); % 读入图像,并将其数据类型转为double
J=I*0.43+90;K=I*0.1+90;L=I*0.01+90;
moon2=uint8(J);moon3=uint8(K);moon4=uint8(L);
J=imdivide(moon,moon2);K=imdivide(moon,moon3);
L=imdivide(moon,moon4);
subplot(221);imshow(moon);title('原图');
subplot(222);imshow(J,[]);title('J=I*0.43+90');
subplot(223);imshow(K,[]);title('K=I*0.1+90');
subplot(224);imshow(L,[]);title('L=I*0.01+90');

The running results are as follows:
Insert image description here

5. Logical operations

Common image logical operations include and, or, not, etc. They are mainly aimed at binary images and are useful in the field of image understanding and analysis. Using this method can provide a template for the image, which can be combined with other calculation methods to achieve special effects.

Two binary images are logically AND, OR, and NOT. The matlab code is as follows:

%% 两幅二值图像进行逻辑与、或、非
clc;clear;close all; %初始化
A=zeros(128);A(40:67,60:100)=1; % 创建两个矩阵
B=zeros(128);B(50:80,40:70)=1;
C=and(A,B);D=or(A,B);E=not(A); %对A图和B图进行逻辑运算
subplot(231);imshow(A);title('A图');
subplot(232);imshow(B);title('B图');
subplot(233);imshow(C);title('C图,A,B相与');
subplot(234);imshow(D);title('D图,A,B相或');
subplot(235);imshow(E);title('E图,A取反');

The running results are as follows:
Insert image description here

Guess you like

Origin blog.csdn.net/xcz8023/article/details/126217393