问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

数字图像处理:图像分割应用

创作时间:
作者:
@小白创作中心

数字图像处理:图像分割应用

引用
CSDN
1.
https://blog.csdn.net/weixin_48524215/article/details/143063395

图像分割是数字图像处理中的关键技术之一,它能够将图像划分为具有不同特征的区域,从而为后续的分析和处理提供基础。本文将介绍三种主要的图像分割方法:阈值分割法、区域生长法和边缘检测法,并通过Python代码展示这些方法的具体实现。

1.1 阈值分割法

阈值分割法(Thresholding)是一种基于图像灰度级或颜色的分割方法,它通过设置一个或多个阈值,将图像划分为前景和背景。它的基本思想是根据像素的灰度值来判断该像素是否属于某个目标区域。

基本原理:

  • 将像素的灰度值与设定的阈值进行比较。
  • 如果像素值高于阈值,则归为目标区域;如果低于阈值,则归为背景。
  • 在多阈值分割中,多个阈值将图像分成多个区域。

1.2 区域生长法

区域生长法(Region Growing)是一种基于像素相似性的分割方法,它从某些初始的“种子点”开始,逐步将与种子点相似的邻域像素归入同一分割区域,直到没有更多满足条件的像素可以合并。

基本原理:

  • 从选定的“种子点”开始,根据预定义的相似性准则(如灰度值相似度或纹理相似度),将相邻的像素添加到种子区域中。
  • 相邻像素的相似性准则一般基于像素的灰度差异、颜色差异或纹理等特征。
  • 当不再有符合相似性条件的像素时,生长过程停止,得到一个完整的区域。

1.3 边缘检测法

边缘检测法(Edge Detection)是一种基于图像梯度信息的分割方法,它通过检测图像中灰度值或颜色发生显著变化的位置来确定区域的边界,适用于具有明显边界的目标区域分割。

基本原理:

  • 利用图像梯度(即像素灰度值或颜色的变化率)来识别边缘。
  • 边缘通常是图像中像素灰度值或颜色发生急剧变化的地方,通常代表物体的边界。
  • 常用的边缘检测算法包括Sobel算子、Canny算子、Prewitt算子等。

1.4 代码实现

以下是使用Python实现上述三种图像分割方法的代码示例:

import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt

# 读取图像并转换为RGB格式
img_dir = r'D:\Document\Experiment\data\image.png'
rgb = cv.imread(img_dir)
rgb = cv.cvtColor(rgb, cv.COLOR_BGR2RGB)  # 转换为RGB格式

# 转换为灰度图
gray = cv.cvtColor(rgb, cv.COLOR_RGB2GRAY)

# (1)阈值分割法
def threshold_segmentation(image, threshold_value=128):
    """使用简单的阈值分割法"""
    _, thresholded_image = cv.threshold(image, threshold_value, 255, cv.THRESH_BINARY)
    return thresholded_image

# (2)区域生长法
def region_growing(image, seed_point, threshold=5):
    """使用简单的区域生长算法"""
    h, w = image.shape
    segmented_image = np.zeros_like(image)
    visited = np.zeros_like(image, dtype=bool)
    seed_value = image[seed_point[1], seed_point[0]]
    
    # 初始化种子列表
    seeds = [seed_point]
    segmented_image[seed_point[1], seed_point[0]] = 255
    visited[seed_point[1], seed_point[0]] = True
    # 区域生长
    while seeds:
        x, y = seeds.pop(0)
        for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:  # 邻域4连通
            nx, ny = x + dx, y + dy
            if 0 <= nx < w and 0 <= ny < h and not visited[ny, nx]:
                if abs(int(image[ny, nx]) - int(seed_value)) < threshold:
                    segmented_image[ny, nx] = 255
                    seeds.append((nx, ny))
                visited[ny, nx] = True
    return segmented_image

# (3)边缘检测法
def edge_detection(image):
    """使用Canny边缘检测法"""
    edges = cv.Canny(image, 100, 200)  # 调整阈值可以影响边缘检测效果
    return edges

# 应用阈值分割法
threshold_image = threshold_segmentation(gray, threshold_value=128)

# 应用区域生长法
seed_point = (1055, 788)  # 随机选择一个种子点
region_growing_image = region_growing(gray, seed_point, threshold=30)

# 应用边缘检测法
edges_image = edge_detection(gray)

# 展示分割结果
plt.figure(figsize=(15, 5))
# 展示原始图像
plt.subplot(1, 4, 1)
plt.title("Original")
plt.axis('off')
plt.imshow(rgb)
plt.subplot(1, 4, 2)
plt.title("Threshold Segmentation")
plt.axis('off')
plt.imshow(threshold_image, cmap='gray')
plt.subplot(1, 4, 3)
plt.title("Region Growing Segmentation")
plt.axis('off')
plt.imshow(region_growing_image, cmap='gray')
plt.subplot(1, 4, 4)
plt.title("Edge Detection")
plt.axis('off')
plt.imshow(edges_image, cmap='gray')
plt.show()

此外,还可以使用Segment Anything模型进行图像分割,效果如下:

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号