图像分割-传统算法-阈值分割原理与实践

固定阈值法------直方图双峰法

手动设定一个全局阈值 T。 简单、速度快。

缺点:对光照不均、对比度变化敏感。

python 复制代码
img_ori = cv2.imread('./test/test_img.jpg', 0)
width, height = img_ori.shape
cv2.imshow('img', img_ori)
''' 直方图双峰法 '''
img = copy.deepcopy(img_ori)
x, img1 = cv2.threshold(img_ori, 150, 255, cv2.THRESH_BINARY)
x, img2 = cv2.threshold(img_ori, 150, 255, cv2.THRESH_TRUNC)
x, img3 = cv2.threshold(img_ori, 150, 255, cv2.THRESH_TOZERO)
cv2.imshow('BINARY', img1)
cv2.imshow('TRUNC', img2)
cv2.imshow('TOZERO', img3)
cv2.waitKey(0)
cv2.destroyAllWindows()

迭代阈值图像分割

通过对图像的灰度值进行迭代,不断调整阈值使得分割更加准确。

对于大尺寸图像,迭代阈值图像分割的计算速度可能成为其一个缺点。

python 复制代码
img = copy.deepcopy(img_ori)
threshold = 150
delta = 1
while delta > 0:
    foreground = []
    background = []
    for y in range(height):
        for x in range(width):
            if img[x, y] > threshold:
                background.append(img[x, y])
            else:
                foreground.append(img[x, y])
    avr_foreground = np.mean(foreground)
    avr_background = np.mean(background)
    new_threshold = (avr_foreground + avr_background) // 2
    delta = abs(new_threshold - threshold)
    print('delta', delta)
    threshold = new_threshold
for y in range(height):
    for x in range(width):
        if img[x, y] > threshold:
            img[x, y] = 255
        else:
            img[x, y] = 0

cv2.imshow('iter result', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

自适应阈值图像分割

根据像素邻域的灰度分布(如均值、高斯加权和)动态计算阈值。 能处理光照不均或渐变图像。

缺点:速度较慢,可能有"块效应"。

python 复制代码
img = copy.deepcopy(img_ori)
block_size = 3
img_thresh = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
cv2.imshow('adapt result', img_thresh)

大津法(OTSU)

自动寻找使类间方差最大的阈值。 全自动,对双峰直方图效果极佳。

缺点:对非双峰直方图或噪声敏感。

python 复制代码
img = copy.deepcopy(img_ori)
ret, img_thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
cv2.imshow('OTSU result', img_thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()

全部代码

python 复制代码
import cv2
import numpy as np
import copy

img_ori = cv2.imread('./test/test_img.jpg', 0)
width, height = img_ori.shape

''' 直方图双峰法 '''
img = copy.deepcopy(img_ori)
x, img1 = cv2.threshold(img, 150, 255, cv2.THRESH_BINARY)
x, img2 = cv2.threshold(img, 150, 255, cv2.THRESH_TRUNC)
x, img3 = cv2.threshold(img, 150, 255, cv2.THRESH_TOZERO)
cv2.imshow('img', img_ori)
cv2.imshow('BINARY', img1)
cv2.imshow('TRUNC', img2)
cv2.imshow('TOZERO', img3)
cv2.waitKey(0)
cv2.destroyAllWindows()

''' 迭代阈值图像分割 '''
img = copy.deepcopy(img_ori)
threshold = 150
delta = 1
while delta > 0:
    foreground = []
    background = []
    for y in range(height):
        for x in range(width):
            if img[x, y] > threshold:
                background.append(img[x, y])
            else:
                foreground.append(img[x, y])
    avr_foreground = np.mean(foreground)
    avr_background = np.mean(background)

    new_threshold = (avr_foreground + avr_background) // 2
    delta = abs(new_threshold - threshold)
    print('delta', delta)
    threshold = new_threshold
for y in range(height):
    for x in range(width):
        if img[x, y] > threshold:
            img[x, y] = 255
        else:
            img[x, y] = 0

cv2.imshow('img', img_ori)
cv2.imshow('iter result', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

''' 自适应阈值图像分割 '''
img = copy.deepcopy(img_ori)
block_size = 3
img_thresh = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
cv2.imshow('img', img_ori)
cv2.imshow('adapt result', img_thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()

'''  大津法(OTSU) '''
# 大津法阈值分割
img = copy.deepcopy(img_ori)
ret, img_thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
cv2.imshow('img', img_ori)
cv2.imshow('OTSU result', img_thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()

参考
图像分割参考1
图像分割参考2

相关推荐
_dindong14 分钟前
cf1091div2 C.Grid Covering(数论)
c++·算法
AI成长日志14 分钟前
【Agentic RL】1.1 什么是Agentic RL:从传统RL到智能体学习
人工智能·学习·算法
黎阳之光1 小时前
黎阳之光:视频孪生领跑者,铸就中国数字科技全球竞争力
大数据·人工智能·算法·安全·数字孪生
skywalker_111 小时前
力扣hot100-3(最长连续序列),4(移动零)
数据结构·算法·leetcode
6Hzlia1 小时前
【Hot 100 刷题计划】 LeetCode 17. 电话号码的字母组合 | C++ 回溯算法经典模板
c++·算法·leetcode
wfbcg1 小时前
每日算法练习:LeetCode 209. 长度最小的子数组 ✅
算法·leetcode·职场和发展
AI人工智能+1 小时前
基于高精度身份证OCR识别、炫彩活体检测及人脸比对技术的人脸核身系统,为通信行业数字化转型提供了坚实的安全底座
人工智能·计算机视觉·人脸识别·ocr·人脸核身
_日拱一卒1 小时前
LeetCode:除了自身以外数组的乘积
数据结构·算法·leetcode
计算机安禾2 小时前
【数据结构与算法】第36篇:排序大总结:稳定性、时间复杂度与适用场景
c语言·数据结构·c++·算法·链表·线性回归·visual studio
AI人工智能+2 小时前
一种以深度学习与计算机视觉技术为核心的表格识别系统,实现了结构化、半结构化表格的精准文字提取、布局解析与版面完整还原
深度学习·计算机视觉·ocr·表格识别