Digital Image Processing: Experiment 6 Image Segmentation

Experiment 6 Image Segmentation

Data segmentation is a key step from image processing to image analysis, and is an essential preprocessing for image recognition and computer vision. The extracted objects after image segmentation can be used in image recognition, feature extraction, image search and other fields. The basic strategy of image segmentation is mainly based on two characteristics of image gray value, that is, gray discontinuity and gray similarity, so image segmentation methods can be divided into edge-based segmentation methods and region-based segmentation methods. This experiment further deepens the understanding of image segmentation by completing simple image segmentation.

1. Purpose of the experiment

Further master the MATLAB program design method;
consolidate the theory and method of image segmentation;
implement image segmentation by programming, compare the effect of threshold-based segmentation method and edge-based segmentation method, and analyze the factors that affect the segmentation effect.

2. Experimental basis

1.基于阈值的分割
基于阈值的分割属于区域分割的一种,是图像分割中最常用的一类方法,对于一个具有双峰分布的简单图像阈值分割方法,实际上是将输入图像f(i,j)通过变换得到输出图像g(i,j)的过程,即二值化过程。
阈值分割方法的关键是阈值T的确定,如果能确定一个合适的阈值,就可以准确地将图像分割开来,对于灰度直方图具有双峰分布的图像。可以选择古典处的灰度值作为阈值,在许多情况下物体和背景的对比度在图像中的各处是不同的,很难用一个统一的阈值将物体与背景分开。此时可以根据图像的局部特征分别采用不同的阈值进行分割。并分割的方法包括判断分析法,最佳熵自动阈值法,最小误差分割等。
阈值分割的优点是直观计算简单,运算效率较高,速度快。

2.基于边缘的分割
图像分割的另一种方式是通过边缘检测时限的边缘是指通过中像素灰度有阶跃或屋顶变化的像素级和边缘能勾画出物体轮廓,对图像识别和分析十分有用,是图像识别时提取的重要信息,由于边缘处的灰度是不连续的,因此可以利用边缘检测算子将边缘点检测出来,常用的边缘检测算子有梯度算子、Prewitt算子、Sobel算子、LOG算子和Canny算子等。
通过edge函数可以利用各种边缘检测算子检测图像边缘。

3.边缘跟踪
将检测的边缘点连接成线称为边缘跟踪,在识别图像中的目标时,往往需要对目标边缘做跟踪处理,即按一定顺序找出边缘点来绘制出编辑,如果图像是二值图像或图像中不同区域具有不同的像素值,但每个区域内的像素只是相同的,则可以完成基于四联通或八连通区域的轮廓跟踪。
利用bwtraceboundary函数可以在二值图像中追踪目标的轮廓线。

4.Hough变换检测
Hough变换是Paul Hough于1962年提出,是图像处理技术中用于识别几何形状的一种常用方法,它实现了一种从图像空间到参数空间的映射关系。Hough变换的基本原理是利用点线间的对偶性,将原始图像空间的给定曲线通过曲线表达形式变换成为参数空间的一个点。
MATLAB中常用的关于Hough变换的函数有:hough、houghpeaks和houghline。

3. Experimental instruments and equipment

Computer, Matlab image processing software, image to be processed

4. Experimental content and steps

  1. Familiar with related functions in MATLAB, master the specific usage of each function
(1)edge:边缘检测函数。
BW = edge(I)
采用灰度或一个二值化图像I作为它的输入,并返回一个与I相同大小的二值化图像BW,在函数检测到边缘的地方为1,其他地方为0。
BW = edge(I,'sobel') 自动选择阈值用Sobel算子进行边缘检测。
BW = edge(I,'sobel',thresh) 根据所指定的敏感度阈值thresh,用Sobel算子进行边缘检测,它忽略了所有小于阈值的边缘。当thresh为空时,自动选择阈值。
BW = edge(I,'sobel',thresh,direction) 根据所指定的敏感度阈值thresh,在所指定的方向direction上,用Sobel 算子进行边缘检测。Direction可取的字符串值为horizontal(水平方向)、vertical(垂直方向)或both(两个方向)。
[BW,thresh] = edge(I,'sobel',...) 返回阈值
(2)graythresh:利用Otsu算法(最大类间方差)获取全局阈值。
(3)im2bw:将灰度影像转换为二值图像。
(4)bwtraceboundary:在二值图像中追踪目标轮廓线。

help bwtraceboundary
bwtraceboundary Trace object in binary image.
B = bwtraceboundary(BW,P,FSTEP) traces the outline of an object in a binary image BW, in which nonzero pixels belong to an object and 0-pixels constitute the background. P is a two-element vector specifying the row and column coordinates of the initial point on the object boundary. FSTEP is a string or char vector specifying the initial search direction for the next object pixel connected to P. FSTEP can be any of the following strings or char vectors: ‘N’,‘NE’,‘E’,‘SE’,‘S’,‘SW’,‘W’,‘NW’, where N stands for north, NE stands for northeast, etc. B is a Q-by-2 matrix, where Q is the number of boundary pixels for the region. B holds the row and column coordinates of the boundary pixels.
B = bwtraceboundary(BW,P,FSTEP,CONN) specifies the connectivity to use when tracing the boundary. CONN may be either 8 or 4. The default value for CONN is 8. For CONN equal to 4, FSTEP is limited to ‘N’,‘E’,‘S’ and ‘W’.
B = bwtraceboundary(…,N,DIR) provides the option to specify the maximum number of boundary pixels, N, to extract and direction, DIR, in which to trace the boundary. DIR can be either ‘clockwise’ or ‘counterclockwise’. By default, or when N is set to Inf, the algorithmextracts all of the pixels from the boundary and, if DIR is not specified, it searches in the clockwise direction.

(5)hough:Hough变换函数。
1.hought函数
语法:[H, theta, rho] = hough(BW)
      [H, theta, rho] = hough(BW, ParameterName, ParameterValue)
描述:[H, theta, rho] = hough(BW)计算二值图像BW的标准hough变换。函数返回的H称为hough转换矩阵。theta和rho是一维数组,theta记录径,rho记录角度。BW可以是逻辑型,也可以是数值型。
(6)houghpeaks:Hough变换峰值识别。
peaks = houghpeaks(H, NumPeaks) 
peaks = houghpeaks(..., 'Threshold', val1, 'NHoodSize', val2) 
其中,"…"指出来自默认语法和peaks的输入是持有峰值行和列坐标的Q×2大小的矩阵。Q的范围是0到NumPeaks,H是霍夫变换矩阵。参数val1是非负的标量,指定了H中的什么值被考虑为峰值;val1可以从0到Inf变化,默认值是0.5*max(H(:))。参数val2是奇整数的两元素矢量,指定量围绕峰值的邻域大小。
(7)houghline:基于Hough变换提取线元。
lines = houghlines(f, theta, rho, peaks) 
lines = houghlines(..., 'FillGap', val1, 'MinLength', val2)
其中,theta和rho是来自函数hough的输出,peaks是函数houghpeaks的输出。输出lines是结构数组(可能检测到多条直线),长度等于找到的线段数。结构中的每个元素可以看成一条线,并含有下列字段:
point1:两元素向量[r1, c1],指定了线段起点的行列坐标。
point2:两元素向量[r2, c2],指定了线段终点的行列坐标。
theta:与线相关的霍夫变换的以度计量的角度。
rho:与线相关的霍夫变换的ρ轴位置。
其他参数如下:
val1是正的标量,指定了与相同的霍夫变换相关的两条线段的距离。当两条线段之间的距离小于指定的值时,函数houghlines把线段合并为一条线段(默认的距离是20个像素)。参数val2是正的标量,指定合并的线是保留还是丢弃。如果合并的线比val2指定的值短,就丢弃(默认值是40)。
  1. Threshold segmentation of simple images
    (1) Threshold segmentation using histogram
    insert image description here
    insert image description here

Figure 1 Threshold segmentation
(2) Using the maximum between-class variance to automatically determine the threshold for segmentation
insert image description here
insert image description here

Figure 2 Maximum between-class variance
3. Edge-based threshold segmentation
(1) Use various edge detection operators to detect edges
insert image description here
insert image description here

Figure 3 Edge operator
Thinking: compare different

Roberts算子检测方法对具有陡峭的低噪声的图像处理效果较好,但是利用roberts算子提取边缘的结果是边缘比较粗,因此边缘的定位不是很准确。

Sobel算子检测方法对灰度渐变和噪声较多的图像处理效果较好,sobel算子对边缘定位不是很准确,图像的边缘不止一个像素。

Prewitt算子检测方法对灰度渐变和噪声较多的图像处理效果较好。但边缘较宽,而且间断点多。

Laplacian算子法对噪声比较敏感,所以很少用该算子检测边缘,而是用来判断边缘像素视为与图像的明区还是暗区。

Canny方法不容易受噪声干扰,能够检测到真正的弱边缘。优点在于,使用两种不同的阈值分别检测强边缘和弱边缘,并且当弱边缘和强边缘相连时,才将弱边缘包含在输出图像中。

(2) Edge tracking (bwtraceboundary) function
insert image description here
insert image description here

Figure 4 Edge Tracking

思考:通过修改起始点的横纵坐标,可以改变找到的硬币边缘。
  1. Hough transform detection line segment
clc;clear;
i=imread('circuit.tif');
i1=imrotate(i,30,'crop');%用crop方式显示,旋转后的图像跟原图像大小一样,多余的地方舍去
bw=edge(i1,'canny');
subplot(2,3,1);imshow(i);xlabel('原始图像');
subplot(2,3,2);imshow(i1);xlabel('旋转后的图像');
subplot(2,3,3);imshow(bw);xlabel('边缘检测图像');
[H,theta,rho] = hough(bw);%利用hough函数进行hough变换
subplot(2,3,4);hold on;imshow(imadjust(mat2gray(H)),[],'XData',theta,'YData',rho,'Initialmagnification','fit');
title('峰值检测');
xlabel('\theta(degrees)');ylabel('\rho');
axis on,axis normal,hold on
P=houghpeaks(H,10,'threshold',ceil(0.3*max(H(:))));
x=theta(P(:,2));
y=rho(P(:,1));
plot(x,y,'s','color','red');
lines=houghlines(bw,theta,rho,P,'FillGap',5,'MinLength',7);
subplot(2,3,5),imshow(i1);title('检测到的线段');hold on;
max_len=0;
for k=1:length(lines)
    xy=[lines(k).point1;lines(k).point2];
    plot(xy(:,1),xy(:,2),'LineWidth',2,'color','green');
    %绘制线段起终点
    plot(xy(1,1),xy(1,2),'LineWidth',2,'color','yellow');
    plot(xy(2,1),xy(2,2),'LineWidth',2,'color','blue');
    %确定最长线段的端点
    len=norm(lines(k).point1-lines(k).point2);
    if(len>max_len)
        max_len=len;
        xy_long=xy;
    end
end
%突出显示最长一条直线
plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'color','red');

insert image description here

Figure 5 hough transform

5. Experiment experience

  1. Image segmentation is one of the important steps of image processing. Algorithm is one of the main methods in the process of image segmentation. However, each algorithm has its own characteristics, and its operation results are also very different.
  2. There are always various mistakes in the programming of the algorithm in the experiment, and the mistakes will be corrected after careful inspection and patient correction. The types can be roughly divided into: syntax errors, function calls, and error functions that cannot be called.

Guess you like

Origin blog.csdn.net/chengzilhc/article/details/124556845