基于阈值的图像分割在MATLAB中的实现
在基于计算机的图像分析和处理中,图像分割是一项重要的任务。图像分割允许我们从图像中提取特定的物体或区域。在数字图像处理中,最广泛使用的图像分割技术是基于阈值的图像分割。这种图像分割技术根据像素强度值将图像中的不同区域分离。
在本文中,我们将学习使用MATLAB编程实现基于阈值的图像分割。但在学习之前,让我们先了解阈值的基本概念及其类型。
什么是阈值化?
在数字图像处理中,阈值化是一种根据像素强度将图像的前景和背景区域分离的方法。为此,我们必须指定一个像素强度的阈值。然后,图像处理工具(在本例中为MATLAB)将像素强度高于和低于指定阈值的区域分成两个不同的区域。
阈值化的类型
基于图像和图像分割的不同需求,已经开发出几种不同的阈值化技术。这些阈值化方法描述如下:
全局阈值化 - 在全局阈值化的情况下,为整个图像的分割指定一个常数或固定阈值。这种阈值化技术主要用于光照条件均匀的图像。
自适应阈值化 - 自适应阈值化技术用于分割光照条件不均匀且具有强度变化的图像。这种阈值化技术首先将图像分成较小的区域,然后局部地对每个区域应用特定的阈值进行分割。
Otsu阈值化 - Otsu阈值化技术能够根据图像自动确定最佳阈值进行分割。为此,它最大化图像前景和背景像素值之间的类间方差。
在概述了阈值化及其类型之后,让我们现在学习使用MATLAB编程实现基于阈值的图像分割。
示例
% MATLAB program for thresholding-based image segmentation using global thresholding % Read the input image img = imread('https://tutorialspoint.com/assets/questions/media/14304-1687425236.jpg'); % Convert the RGB image to grayscale gray_img = rgb2gray(img); % Compute the global threshold value GT_value = graythresh(gray_img); % Perform global thresholding based image segmentation binary_img = imbinarize(gray_img, GT_value); % Display the original and globally segmented images subplot(1, 2, 1); imshow(img); title('Original Image'); subplot(1, 2, 2); imshow(binary_img); title('Global Segmented Image');
输出

解释
在这个MATLAB程序中,我们演示了使用全局阈值化技术实现基于阈值的图像分割。
在这个代码中,我们首先读取输入图像,然后如果它不是灰度图像,则将其转换为灰度图像。接下来,我们使用“graythresh”函数确定给定图像的全局阈值。之后,我们使用全局阈值和“imbinarize”函数对灰度图像执行全局阈值化。最后,我们使用“imshow”函数显示原始图像和全局分割图像。
示例
% MATLAB program for thresholding-based image segmentation using adaptive thresholding % Read the input image img = imread('https://tutorialspoint.com/assets/questions/media/14304-1687425236.jpg'); % Convert the RGB image to grayscale gray_img = rgb2gray(img); % Calculate the adaptive threshold value AT_value = adaptthresh(gray_img, 0.5); % Set the sensitivity parameter if required % Perform adaptive thresholding-based image segmentation adaptive_img = imbinarize(gray_img, AT_value); % Display the original and adaptive segmented images subplot(1, 2, 1); imshow(img); title('Original Image'); subplot(1, 2, 2); imshow(adaptive_img); title('Adaptive Segmented Image');
输出

解释
这个MATLAB程序演示了使用自适应阈值化方法实现基于阈值的图像分割。
在这个MATLAB代码中,我们首先使用“imread”函数读取输入图像,然后如果它不是灰度图像,则将其转换为灰度图像。接下来,我们使用“adaptthresh”函数确定给定图像的自适应阈值。之后,我们使用自适应阈值和“imbinarize”函数对灰度图像执行自适应阈值化分割。最后,我们使用“imshow”函数显示原始图像和自适应分割图像,并附带合适的标题。
示例
% MATLAB program for thresholding-based image segmentation using Otsu’s thresholding % Read the input image img = imread('https://tutorialspoint.com/assets/questions/media/14304-1687425236.jpg'); % Convert the image to grayscale if required if size(img, 3) > 1 gray_img = rgb2gray(img); end % Calculate the histogram for the image [counts, x] = imhist(gray_img, 16); % Calculate the Otsu's thresholding value Otsu_value = otsuthresh(counts); % Perform the Otsu's thresholding-based image segmentation Otsu_img = imbinarize(gray_img, Otsu_value); % Display the original and Otsu’s segmented image subplot(1, 2, 1); imshow(img); title('Original Image'); subplot(1, 2, 2); imshow(Otsu_img); title('Otsu Segmented Image');
输出

解释
这个MATLAB程序使用Otsu阈值化方法执行基于阈值的图像分割。
在这个代码中,我们首先使用“imread”函数读取输入图像,然后根据需要将其转换为灰度图像。接下来,我们使用“imhist”函数计算灰度图像的直方图,以获取图像中像素强度的信息。之后,我们使用“otsuthresh”函数计算Otsu阈值。
接下来,我们使用“imbinarize”函数执行基于Otsu阈值的输入图像分割。最后,我们调用“imshow”函数显示原始图像和Otsu分割图像。
结论
因此,这就是我们如何使用MATLAB编程执行基于阈值的图像分割的方法。MATLAB提供三种类型的阈值化,即全局阈值化、自适应阈值化和Otsu阈值化,以执行图像分割。基于阈值的图像分割是基于计算机的图像分析中的一项重要任务。基于阈值的图像分割最常用,因为它提供了一种简单有效的方法来从图像中提取物体/区域。此外,MATLAB提供了各种内置函数和简单的编程环境来执行基于阈值的图像分割。