Image Processing Basics - Morphological Processing - Edge Detection (Matlab Simulation and Image Processing Series Issue 4)

Teachers in image processing, learn the following content and code on the first day:

Image reading and display: In Matlab, you can use the imread function to read an image, and use the imshow function to display the image. Here is a simple sample code:

% 读取图像
img = imread('lena.jpg');

% 显示图像
imshow(img);
图像的基本操作:在Matlab中,可以通过对图像进行像素级别的操作来实现图像处理任务。以下是一些基本的图像操作示例代码:
ini
Copy
% 获取图像大小和通道数
[rows, cols, channels] = size(img);

% 获取图像的灰度值
gray_img = rgb2gray(img);

% 对图像进行缩放
scaled_img = imresize(img, 0.5);

% 对图像进行旋转
rotated_img = imrotate(img, 45);

Image filtering: Filtering is a commonly used technique in image processing, which can be used to remove noise, smooth images, etc. Here are some common filter sample codes:

% 均值滤波
avg_filter = fspecial('average', [3 3]);
avg_img = imfilter(img, avg_filter);

% 中值滤波
median_img = medfilt2(img);

% 高斯滤波
gaussian_filter = fspecial('gaussian', [3 3], 1);
gaussian_img = imfilter(img, gaussian_filter);

Alright, as your image processing teacher, I'd be happy to introduce you to edge detection and morphological processing, with the corresponding code. The following is a detailed introduction to edge detection and morphological processing:

edge detection

Edge detection is a common image processing technique used to detect edges in images. In Matlab, operators such as Sobel, Prewitt, and Laplacian can be used to realize edge detection. Here is some sample code:

  1. Sobel operator

The Sobel operator is a gradient-based edge detection operator, which is often used for edge detection in images. The following is the code for edge detection using the Sobel operator:

% 读取图像
img = imread('lena.jpg');

% 灰度化
gray_img = rgb2gray(img);

% Sobel算子
sobel_filter_x = [-1 0 1; -2 0 2; -1 0 1];
sobel_filter_y = [-1 -2 -1; 0 0 0; 1 2 1];
dx = imfilter(double(gray_img), sobel_filter_x);
dy = imfilter(double(gray_img), sobel_filter_y);
sobel_img = sqrt(dx.^2 + dy.^2);

% 显示图像
imshow(sobel_img);
  1. Prewitt operator

The Prewitt operator is also a gradient-based edge detection operator, similar to the Sobel operator. The following is the code for edge detection using the Prewitt operator:

% 读取图像
img = imread('lena.jpg');

% 灰度化
gray_img = rgb2gray(img);

% Prewitt算子
prewitt_filter_x = [-1 0 1; -1 0 1; -1 0 1];
prewitt_filter_y = [-1 -1 -1; 0 0 0; 1 1 1];
dx = imfilter(double(gray_img), prewitt_filter_x);
dy = imfilter(double(gray_img), prewitt_filter_y);
prewitt_img = sqrt(dx.^2 + dy.^2);

% 显示图像
imshow(prewitt_img);
  1. Laplacian operator

The Laplacian operator is an edge detection operator based on the second derivative, which can detect the edges and corners in the image. The following is the code for edge detection using the Laplacian operator:

% 读取图像
img = imread('lena.jpg');

% 灰度化
gray_img = rgb2gray(img);

% Laplacian算子
laplacian_filter = [0 -1 0; -1 4 -1; 0 -1 0];
laplacian_img = imfilter(double(gray_img), laplacian_filter);

% 显示图像
imshow(laplacian_img);

The above are some sample codes for edge detection using Matlab. You can choose different operators for edge detection according to your needs, or combine different operators for optimization.

Morphological processing

Morphological processing is a mathematical method for image analysis and processing, mainly used for image morphological feature extraction and image morphological operations. In Matlab, some morphological processing functions can be used to perform image morphological operations. Here is some sample code:

  1. corrosion operation

Erosion operation is a basic operation in morphological processing, which can be used to remove small objects and details in the image. The following is the code to implement the corrosion operation using Matlab:

% 读取图像
img = imread('text.png');

% 二值化
bw_img = imbinarize(img);

% 腐蚀操作
se = strel('disk', 5);
eroded_img = imerode(bw_img, se);

% 显示图像
imshow(eroded_img);
  1. expansion operation

The dilation operation is a basic operation in morphological processing, which can be used to fill holes in the image and connect objects. The following is the code to implement the dilation operation using Matlab:

% 读取图像
img = imread('text.png');

% 二值化
bw_img = imbinarize(img);

% 膨胀操作
se = strel('disk', 5);
dilated_img = imdilate(bw_img, se);

% 显示图像
imshow(dilated_img);
  1. open operation

The opening operation is a combined operation in morphological processing. It first performs an erosion operation and then an expansion operation, which can be used to remove small objects and details and retain the shape of large objects. The following is the code to implement the opening operation using Matlab:

% 读取图像
img = imread('text.png');

% 二值化
bw_img = imbinarize(img);

% 开运算
se = strel('disk', 5);
opened_img = imopen(bw_img, se);

% 显示图像
imshow(opened_img);
  1. Close operation

Closing operation is a combination operation in morphological processing. It first performs expansion operation and then erosion operation, which can be used to fill small holes and connect objects. The following is the code to implement the closing operation using Matlab:

% 读取图像
img = imread('text.png');

% 二值化
bw_img = imbinarize(img);

% 闭运算
se = strel('disk', 5);
closed_img = imclose(bw_img, se);

% 显示图像
imshow(closed_img);

Summarize:

Edge detection and morphological processing are commonly used techniques in image processing, which can be used for image feature extraction and image morphological operations. In Matlab, different operators can be used for edge detection, and different morphological processing functions can be used for image morphological operations. Hope the above introduction is helpful to you!

Guess you like

Origin blog.csdn.net/ALiLiLiYa/article/details/131426040