MATLAB image processing learning - statistical characteristics of images + spatial filtering

Table of contents

1. Statistical properties of images

 (1) Introduction

 (2) Image mean

 (3) The standard deviation of the image

 (4) Correlation coefficient of image

 (5) Contour lines of the image

 2. Spatial filtering

 (1) Linear average filtering (domain phase convolution calculation)

 (2) Linear average filtering (two-dimensional convolution calculation)

 (3) Two-dimensional linear digital filtering

 (4) Nonlinear spatial filtering

 (5) Median filtering

 (6) Sorting and filtering

 (7) Adaptive filtering

 (8) Laplacian sharpening filter


1. Statistical properties of images

(1) Introduction

In MATLAB, a grayscale image is a two-dimensional matrix, and an RGB color image is a three-dimensional matrix. The image is used as a matrix, and statistical characteristics such as its mean, variance, and correlation coefficient can be calculated.

(2) Image mean

Use the function mean2() to calculate the mean value of the matrix. For RGB images, the calling method is as follows:

J=mean2(I) : get the mean of all the color values ​​of image I

J=mean2(I (: , : , N) ) : Gets the mean of each color value. If the N parameter is 1, it means red, if it is 2, it means green, and if it is 3, it means blue.

Code (calculates average of grayscale image and color image):

close all;clear all;clc;
I=imread('D:/resource_photo/8.jpg');
J=rgb2gray(I);
gray=mean2(J);
rgb=mean2(I);
r=mean2(I(:,:,1));
g=mean2(I(:,:,2));
b=mean2(I(:,:,3));
figure;
subplot(121);imshow(uint8(I));
subplot(122);imshow(uint8(J));

operation result:

(3) The standard deviation of the image

In matlab, the function std() is used to calculate the standard deviation of the vector, and the standard deviation of the matrix is ​​calculated by std2() .

eg. The pixels of the grayscale image are two-dimensional matrix A, then the standard deviation of the image is std2(A).

Code (to calculate the standard deviation of a grayscale image):

close all;clear all;clc;
I=imread('D:/resource_photo/8.jpg');
J=rgb2gray(I);
s1=std2(J); %计算标准差
K=histeq(J); %直方图均衡化
s2=std2(K);
figure;
subplot(121);imshow(uint8(J));
subplot(122);imshow(uint8(K));

operation result:

 (4) Correlation coefficient of image

The function corr2() is used to calculate the correlation coefficient of two grayscale images, and the calling method is as follows:

r=corr2(A,B) : where A and B are two-dimensional matrices of equal size, and r is the correlation coefficient of the two matrices.

Code (to calculate the correlation coefficient of two grayscale images):

close all;clear all;clc;
I=imread('D:/resource_photo/8.jpg');
J=rgb2gray(I);
K=medfilt2(J); %中值滤波
r=corr2(J,K); %计算相关系数
figure;
subplot(121);imshow(uint8(J));
subplot(122);imshow(uint8(K));

operation result:

 (5) Contour lines of the image

In matlab, the contour line of the grayscale image can be drawn through the function imcontour(), and the calling method is as follows:

imcontour(I) : In this function, I is the two-dimensional data matrix of the grayscale image, and the contour line of the grayscale image is drawn.

imcontour(I,n) : This function sets the number of contour lines to n. If n is not specified, the function will automatically select n.

Code (computes contours of a grayscale image):

close all;clear all;clc;
I=imread('D:/resource_photo/8.jpg');
J=rgb2gray(I);
figure;
subplot(121);imshow(J);
subplot(122);imcontour(J,3);

operation result:

 2. Spatial filtering

(1) Linear average filtering (domain phase convolution calculation)

  Linear average filtering is the most commonly used linear spatial domain filtering. The linear average filter is actually a low-pass filter , which passes the low-frequency part of the signal and prevents the high-frequency part from passing. Since the edge of the image is in the high-frequency part, the edge of the image will be blurred after linear average filtering.

  When performing image filtering, a method of convolution between the template and the domain of the image can be used. Use the function imfilter() .

Code (the image is smoothed by the function imfilter):

close all;clear all;clc;
I=imread('D:/resource_photo/8.jpg');
J=imnoise(I,'salt & pepper',0.02); %加入椒盐噪声
h=ones(3,3)/5;
h(1,1)=0; h(1,3)=0;
h(3,1)=0; h(1,3)=0;
K=imfilter(J,h); %图像的滤波
figure,imshow(I);
figure,imshow(J);
figure,imshow(K);

operation result:

(As shown in the above two pictures, the left is the original picture, and the right is the image after adding salt and pepper noise)

 

 (The picture above is the image after using linear average filtering on the image with noise added)

 (2) Linear average filtering (two-dimensional convolution calculation)

In matlab, use conv2() for two-dimensional convolution calculation, the calling method is as follows:

​J = conv2(A,B) : Returns the two-dimensional convolution of matrices A and B.

J = conv2(u,v,A) : Firstly calculate the convolution of each column of A with the vector u, and then calculate the convolution of each row result with the vector v.

J = conv2(A,B,shape) : returns the convolved subregion according to shape. For example, C = conv2(A,B,'same') returns the center part of the convolution that is the same size as A.

Code (smooth image using 2D convolution function):

close all;clear all;clc;
I=imread('D:/resource_photo/7.jpg');
I=rgb2gray(I);
I=im2double(I);
J=imnoise(I,'gaussian',0,0.01); %加入高斯噪声
h=ones(3,3)/9;%产生模板
K=conv2(J,h,'same'); %对图像进行二维卷积计算(滤波)
figure,imshow(I);
figure,imshow(J);
figure,imshow(K);

operation result:

 

(3) Two-dimensional linear digital filtering

You can use filter2() to perform two-dimensional linear digital filtering, and use the function fspecial() to generate a filter template.

Code (using conv2() to smooth the image):

close all;clear all;clc;
I=imread('D:/resource_photo/7.jpg');
I=rgb2gray(I);
I=im2double(I);
J=imnoise(I,'gaussian',0,0.01); %加入高斯噪声
h=fspecial('average',3);%生成3*3模板
K=filter2(h,J); %对图像进行二维卷积计算(滤波)
figure,imshow(I);
figure,imshow(J);
figure,imshow(K);

 operation result:

 

(4) Nonlinear spatial filtering

Nonlinear spatial filtering mainly includes median filtering, order statistical filtering and adaptive filtering .

(5) Median filtering

Median filtering is a non-linear image smoothing method that preserves edges.

Two-dimensional median filtering is used for image enhancement. The median filter can remove the salt and pepper noise in the image, the smoothing effect is better than the mean filter, and it can keep the edge of the image clear while suppressing the noise.

In matlab, the function medfilt2() is used to perform two-dimensional median filtering of images.

Code (median filtering the image):

close all;clear all;clc;
I=imread('D:/resource_photo/7.jpg');
I=rgb2gray(I);
I=im2double(I);
J=imnoise(I,'salt & pepper',0.03); %加入椒盐噪声
K=medfilt2(J); %对图像进行二维中值滤波
figure;
subplot(131);imshow(I);%原始图像
subplot(132);imshow(J);%加入噪声图像
subplot(133);imshow(K);%滤波处理后图像

operation result:

 (6) Sorting and filtering

In matlab, the ordfilt2() method is used for sorting and filtering.

(When the function medfilt2() performs filtering, the sorted median value is selected, so when the calling method is J=ordfilt2(I,median(1:m*n),[m,n]), it is equivalent to the median value filtering)

Code (sorting and filtering images):

close all;clear all;clc;
I=imread('D:/resource_photo/7.jpg');
I=rgb2gray(I);
I=im2double(I);
J=imnoise(I,'salt & pepper',0.03); %加入椒盐噪声
K1=ordfilt2(J,1,true(5)); %对图像进行排序滤波(选取排序后第一个作为输出结果)
K2=ordfilt2(J,25,true(5));%选取排序后第25个作为输出结果
figure;
subplot(131);imshow(J);%原始图像
subplot(132);imshow(K2);
subplot(133);imshow(K1);%滤波处理后图像

operation result:

 (7) Adaptive filtering

Use wiener2() in matlab to perform adaptive filtering according to the noise of the image; adjust the output of the filter according to the local variance of the image.

Code (adaptive filtering of images):

close all;clear all;clc;
I=imread('D:/resource_photo/7.jpg');
I=rgb2gray(I);
I=im2double(I);
J=imnoise(I,'gaussian',0,0.1); %加入椒盐噪声
K=wiener2(J,[5 5]);%对图像进行自适应滤波
figure;
subplot(131);imshow(I);%原始图像
subplot(132);imshow(J);%加入噪声图像
subplot(133);imshow(K);%滤波处理后图像

operation result:

 (8) Laplacian sharpening filter

For blurred images, the sharpening filter can compensate the outline of the image and make the image clear.

For sharpening filters try Laplacian. The Laplacian operator is more suitable for improving image blur caused by diffuse reflection of light.

Code (sharpening and filtering the image):

close all;clear all;clc;
I=imread('D:/resource_photo/7.jpg');
I=rgb2gray(I);
I=im2double(I);
h=[0,1,0;1,-4,1;0,1,0];%拉普拉斯算子
J=conv2(I,h,'same');%二维卷积
K=I-J;
figure;
subplot(121);imshow(I);%原始图像
subplot(122);imshow(K);%滤波处理后图像

operation result:

 

Guess you like

Origin blog.csdn.net/weixin_52135595/article/details/126938012