1. Experiment purpose
1. Image 3*3 mean filtering and median filtering.
2. Use gradient, sobel, and Laplacian operators to sharpen the image.
3. Use multiple operators (at least five) to extract edges from the image. Compare the characteristics of each operator, analyze the processing results, find out where in the image the processing effect is not good, and what are the possible reasons.
2. Experimental instruments
PC, matlab
3. Experimental principles
- Image filtering, that is, suppressing the noise of the target image while retaining the detailed features of the image as much as possible, is an indispensable operation in image preprocessing. The quality of its processing effect will directly affect the effectiveness and effectiveness of subsequent image processing and analysis. reliability.
- Common image filtering methods include: linear filtering (box filtering, mean filtering, Gaussian filtering); nonlinear filtering (median filtering, bilateral filtering).
- Mean filtering adopts the idea of averaging multiple measurements, replacing each pixel with the average value of the surrounding pixels. Mean filtering is a special case of normalized box filtering. Advantages: The pixels affected by the noise can be repaired using the pixel values around the noise, and the filtering of salt and pepper noise is better. Disadvantages: It cannot protect image details very well. While denoising the image, it also destroys the details of the image, thus making the image blurry.
- Median filtering sorts the pixels surrounding the pixel to be processed from small to large, and replaces the pixel with the median value. Advantages: It can remove impulse noise and salt-and-pepper noise while retaining image edge details. Disadvantages: When the convolution kernel is large, the image still becomes blurred, and the amount of calculation is large.
- Image sharpening is to compensate for the contours of the image and enhance the edges and grayscale transitions of the image to make the image clearer. It is divided into two categories: spatial domain processing and frequency domain processing. Image sharpening is to highlight the edges, contours, or characteristics of certain linear target features on the image. This filtering method improves the contrast between the edge of the ground object and the surrounding pixels, so it is also called edge enhancement.
- The basic idea of edge detection: calculating local differential operators. The first-order differential is calculated using the gradient operator, and the second-order differential is calculated using Laplacian.
- The Sobel operator is one of the most important operators in pixel image edge detection. Technically, it is a discrete first-order difference operator used to calculate the approximation of the first-order gradient of the image brightness function. Using this operator at any point in the image will generate the gradient vector or its normal vector corresponding to that point.
- The Laplacian operator is a second-order differential operator in n-dimensional Euclidean space, defined as the divergence div() of the gradient grad().
- The Roberts edge detection operator is an operator that uses local difference operators to find edges. The edges of the image processed by the Robert operator are not very smooth. After analysis, since the Robert operator usually produces a wider response in the area near the edge of the image, the edge image detected by the above operator often needs to be refined, and the accuracy of edge positioning is not very high.
- The Prewitt operator is a first-order differential operator for edge detection. It uses the grayscale difference between the upper and lower and left and right adjacent points of a pixel to reach the extreme value at the edge to detect the edge, remove some false edges, and have a smoothing effect on noise. The principle is to use two directional templates to perform neighborhood convolution with the image in the image space. One of these two directional templates detects horizontal edges and the other detects vertical edges.
4. Experiment complete code
Mean filter experiment
%1.Import images img = imread('1.jpg'); %Import images that need to be filtered n = 3; % template size [height,width] = size(img); % Get the size of the image imshow(img); %Show original image %2. Add salt and pepper noise to the image img = imnoise(img,'salt & amp; pepper'); figure; imshow(img); %Display the image after adding noise % perform mean filtering img1 = double(img); %data type conversion img2 = img1; %Assign the converted data to img2 for i = 1:height-n + 1 for j = 1:width-n + 1 c = img1(i:i + (n-1),j:j + (n-1)); %Get the template size block from the beginning in img1 and assign it to c e=c(1,:); %e stores the first row of matrix c for u=2:n % Take out other row elements in c and connect them to e to make e a row matrix e=[e,c(u,:)]; end s=sum(e); %Get the sum of a row img2(i + (n-1)/2,j + (n-1)/2)=s/(n*n); % assign the mean of each element of the template to the element at the center of the template end end d = uint8(img2); %The elements that have not been assigned a value take the original value figure; imshow(d); %Show filtered pictures
Median filter experiment
% median filtering experiment %1.Import images img = imread('1.jpg'); %Import images that need to be filtered n = 3; % template size [height,width] = size(img); % Get the size of the image imshow(img); %Show original image %2. Add salt and pepper noise to the image img = imnoise(img,'salt & amp; pepper'); figure; imshow(img); %Display the image after adding noise %Perform median filtering img1 = double(img); %data type conversion img2 = img1; for i=1:height-n + 1 for j=1:width-n + 1 c=img1(i:i + (n-1),j:j + (n-1)); % Take the template size block from the beginning in x1 and assign it to c e=c(1,:); %e stores the first row of matrix c for u=2:n % Take out other row elements in c and connect them to e to make e a row matrix e=[e,c(u,:)]; end med=median(e); %Get the median value of a row img2(i + (n-1)/2,j + (n-1)/2)=med; %Assign the median value of each element of the template to the element at the center of the template end end d = uint8(img2); %The elements that have not been assigned a value take the original value figure; imshow(d); %Show filtered pictures
Image edge extraction experiment
%1.Import images img = imread('2.jpg'); %Import images that need to be filtered imshow(img) %2. Convert color image to gray image img1 = rgb2gray(img); imshow(img1); %Sobel operator edge extraction image model=[-1,0,1; -2,0,2; -1,0,1]; [m,n]=size(img1); img2=double(img1); for i=2:m-1 for j=2:n-1 img2(i,j)=img1(i + 1,j + 1) + 2*img1(i + 1,j) + img1(i + 1,j-1)-img1(i-1,j + 1) -2*img1(i-1,j)-img1(i-1,j-1); end end imshow(img2) % displays the image of Sobel operator edge detection %Robert operator edge detection image model=[0,-1;1,0]; [m,n]=size(img1); img2=double(img1); for i=2:m-1 for j=2:n-1 img2(i,j)=img1(i + 1,j)-img1(i,j + 1); end end imshow(img2) %Prewitt operator edge detection model=[-1,0,1; -1,0,1; -1,0,1]; [m,n]=size(img1); I2=img1; for i=2:m-1 for j=2:n-1 tem=img1(i-1:i + 1,j-1:j + 1); tem=double(tem).*model; img2(i,j)=sum(sum(tem)); end end %log operator edge detection model=[-1,0,1; -1,0,1; -1,0,1]; for i=3:m-2 for j=3:n-2 tem=double(t(i-2:i + 2,j-2:j + 2)).*model; x=sum(sum(tem)); tt(i,j)=x; end end %Laplacian operator I=imread('2.jpg'); I1=mat2gray(I);% implements the normalization operation of the image matrix [m,n]=size(I1); newGrayPic=I1;% is to retain one pixel from the edge of the image LaplacianNum=0;%The value of each pixel calculated by the Laplacian operator LaplacianThreshold=0.2;% set threshold for j=2:m-1 % perform boundary extraction for k=2:n-1 LaplacianNum=abs(4*I1(j,k)-I1(j-1,k)-I1(j + 1,k)-I1(j,k + 1)-I1(j,k-1)); if(LaplacianNum > LaplacianThreshold) newGrayPic(j,k)=255; else newGrayPic(j,k)=0; end end end
Image sharpening experiment
Use the sobel and Laplacian operators in Experiment 3 for image sharpening, and supplement the gradient operator for image sharpening as follows:
%1.Import images img = imread('2.jpg'); %Import images that need to be filtered imshow(img) %2. Convert color image to gray image img1 = rgb2gray(img); imshow(img1); %Sobel operator edge extraction image model=[-1,0,1; -2,0,2; -1,0,1]; [m,n]=size(img1); img2=double(img1); for i=2:m-1 for j=2:n-1 img2(i,j)=img1(i + 1,j + 1) + 2*img1(i + 1,j) + img1(i + 1,j-1)-img1(i-1,j + 1) -2*img1(i-1,j)-img1(i-1,j-1); end end imshow(img2) % displays the image of Sobel operator edge detection %Robert operator edge detection image model=[0,-1;1,0]; [m,n]=size(img1); img2=double(img1); for i=2:m-1 for j=2:n-1 img2(i,j)=img1(i + 1,j)-img1(i,j + 1); end end imshow(img2) %Prewitt operator edge detection model=[-1,0,1; -1,0,1; -1,0,1]; [m,n]=size(img1); I2=img1; for i=2:m-1 for j=2:n-1 tem=img1(i-1:i + 1,j-1:j + 1); tem=double(tem).*model; img2(i,j)=sum(sum(tem)); end end %log operator edge detection model=[-1,0,1; -1,0,1; -1,0,1]; for i=3:m-2 for j=3:n-2 tem=double(t(i-2:i + 2,j-2:j + 2)).*model; x=sum(sum(tem)); tt(i,j)=x; end end %Laplacian operator I=imread('2.jpg'); I1=mat2gray(I);% implements the normalization operation of the image matrix [m,n]=size(I1); newGrayPic=I1;% is to retain one pixel from the edge of the image LaplacianNum=0;%The value of each pixel calculated by the Laplacian operator LaplacianThreshold=0.2;% set threshold for j=2:m-1 % perform boundary extraction for k=2:n-1 LaplacianNum=abs(4*I1(j,k)-I1(j-1,k)-I1(j + 1,k)-I1(j,k + 1)-I1(j,k-1)); if(LaplacianNum > LaplacianThreshold) newGrayPic(j,k)=255; else newGrayPic(j,k)=0; end end end I2=rgb2gray(I); %Convert the color image into gray image subplot(131),imshow(I2),title('original picture'); subplot(132), imshow(newGrayPic); title('Processing results of Laplacian operator') t=I1 + newGrayPic; subplot(133),imshow(t),title('After sharpening the image') %1. clc; I1=imread('2.jpg'); %Read image %I1=rgb2gray(I); %Convert the color image into gray image subplot(1,3,1); imshow(I1),title('original picture'); mode=[1 0 -1;2 0 -2;1 0 -1];% vertical sharpening template [m,n]=size(I1); I2=double(I1); for i=2:m-1 for j=2:n-1 I2(i,j)=I1(i-1,j-1)-I1(i-1,j + 1) + 2*I1(i,j-1)-2*I1(i,j + 1) + I1(i + 1,j-1)-I1(i + 1,j + 1); end end subplot(1,3,2); imshow(I2),title('Image after edge extraction'); I3=I1; for i=2:m-1 for j=2:n-1 I3(i,j)=I1(i-1,j-1)-I1(i-1,j + 1) + 2*I1(i,j-1)-2*I1(i,j + 1) + I1(i + 1,j-1)-I1(i + 1,j + 1) + I1(i,j); end end subplot(1,3,3); I4 = rgb2gray(uint8(I3)); imshow(I4),title('sharpened image');
5. Experimental results and analysis
The experimental results of mean filtering are as follows:
- Original picture
- Image after adding noise
- Image after mean filtering
Analysis and summary of the results of Experiment 1:
As can be seen from the image after mean filtering in Figure 1.3, the effect of mean filtering is poor and cannot protect image details well. While denoising the image, it also destroys the details of the image, thus making the image blurry.
The experimental results of median filtering are as follows:
In Experiment 1, the image that has been added with noise is subjected to median filtering, and the obtained filtered image is as follows:
- Median filtered image
Analysis and summary of the results of Experiment 2:
As can be seen from the image after median filtering in Figure 2.1, the effect of median filtering is better than that of mean filtering, and the filtered image is basically consistent with the original image.
Edge extraction experimental results
- Original picture
- Grayscale converted image
- Sobel operator edge detection
- Robert operator edge detection
- Prewitt operator edge detection
- Log operator edge detection
- Canny operator edge detection
From Figures 3.3 to 3.7, we can see that the better edge detection operators are Roberts, Sobel, and Prewitt operators. The lines extracted by the edges of the Roberts operator are the thickest. Since the Roberts edge detection operator is an operator that uses local detection operators to find edges, the edges of the image processing results are not very smooth.
The Canny operator is able to detect weak edges, uses two different thresholds to detect strong edges and weak edges respectively, and only includes weak edges in the output image when the weak edges are connected to strong edges.
Both Sobel and Prewitt perform weighted smoothing on the image first, and then perform differential operations. The difference is that the weights of the smoothing part are somewhat different, so they have a certain ability to suppress noise, but they cannot completely rule out falsehoods in the detection results. edge.
Laplacian is a second-order differential operator that does not depend on the edge direction. It accurately locates step edges in the image. It is sensitive to noise and easily loses the direction information of part of the edge, resulting in some discontinuous detected edges.
Image sharpening experimental results
Gradient operator (horizontal direction) image sharpening:
Gradient operator (vertical direction) image sharpening:
Sobel operator image sharpening:
Laplacian operator image sharpening:
Result analysis and summary of Experiment 4:
In the gradient operator, horizontal sharpening is to enhance the edge information in the horizontal direction, and vertical sharpening is to enhance the edge information in the vertical direction.
Sobel has a smoothing effect on noise and can provide more accurate edge direction information.
The Laplace operator is an isotropic operator and a second-order differential operator. It is more suitable when you only care about the position of the edge without considering the grayscale difference of the surrounding pixels. The Laplace operator responds more strongly to isolated pixels than to edges or lines, so it is only suitable for noise-free images. In the presence of noise, low-pass filtering is required before using the Laplacian operator to detect edges.