当前位置:网站首页>与二值化阈值处理相关的OpenCV函数、方法汇总,便于对比和拿来使用
与二值化阈值处理相关的OpenCV函数、方法汇总,便于对比和拿来使用
2022-07-04 17:41:00 【昊虹图像算法】
二值化阈值处理是很多图像处理需求实现中的基础,并且是基础中的基础,这一步对后续的影响挺大的。
为了让算法的性能提升,有必须要尽可能选取合适的阈值进行处理。
本文就把OpenCV中与二值化阈值处理相关的函数、方法进行汇总,便于在实际开发时的对比和使用。
本文的基础是函数threshold(),如果您想先详细了解下函数threshold(),可以参考博文 https://blog.csdn.net/wenhao_ir/article/details/125592684
目录
01-通过手动控制滑动条用人眼寻找最佳阈值
这个方法及示例代码见博文 https://blog.csdn.net/wenhao_ir/article/details/51539122
02-通过OTSU(最大类间方差法)找寻二值化的阈值
这个方法的原理和C++示例代码见博文 https://blog.csdn.net/wenhao_ir/article/details/51179117
上面的博文中只给出了C++的示例代码,这里再给一个Python代码。
以下面这幅图作为样例:
上面这幅图的百度网盘下载链接:https://pan.baidu.com/s/1IaJ8nrQzGuHt3RA8jbu0GQ?pwd=bjkm
Python示例代码如下:
# 博主微信/QQ 2487872782
# 有问题可以联系博主交流
# 有图像处理需求也请联系博主
# 图像处理技术交流QQ群 271891601
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# OpenCV的版本为4.4.0
import numpy as np
import cv2 as cv
import sys
image = cv.imread('F:/material/images/2022/2022-06/img_300_320.jpg')
if image is None:
print('Error: Could not load image')
sys.exit()
# cv.imshow('Source Image', image)
img_gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
cv.imshow('img_gray', img_gray)
OTSU_threshold, img_B = cv.threshold(img_gray, 71, 255, cv.THRESH_OTSU)
cv.imshow('img_B', img_B)
cv.waitKey(0)
cv.destroyAllWindows()
运行结果如下:
从上面的截图中我们可以看到通过OTSU算法找到的阈值为135
03-通过TRIANGLE(三角形法)找寻二值化的阈值
Python示例代码如下:
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# OpenCV的版本为4.4.0
import numpy as np
import cv2 as cv
import sys
image = cv.imread('F:/material/images/2022/2022-06/img_300_320.jpg')
if image is None:
print('Error: Could not load image')
sys.exit()
# cv.imshow('Source Image', image)
img_gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
cv.imshow('img_gray', img_gray)
TRIANGLE_threshold, img_B = cv.threshold(img_gray, 71, 255, cv.THRESH_TRIANGLE)
cv.imshow('img_B', img_B)
cv.waitKey(0)
cv.destroyAllWindows()
运行结果如下:

04-通过图像直方图的最大熵得到图像二值化分割的阈值
通过图像直方图的最大熵得到图像二值化分割的阈值的原理及代码见博文 https://blog.csdn.net/wenhao_ir/article/details/51671023
对于图像
上面博文中的代码其运行结果如下:
05-利用OpenCV的函数adaptiveThreshold()实现图像的自适应二值化阈值分割
函数adaptiveThreshold()的详细介绍和C++示例代码见博文 https://blog.csdn.net/wenhao_ir/article/details/51565517
下面再补充一个Python示例代码。
由于函数adaptiveThreshold()进行图像二值化的过程中,阈值是不断变化的,所以咱们就不能像函数threshold()那样得到其进行图像二值化中的阈值了。
其Python原型如下:
dst = cv.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C[, dst])
函数adaptiveThreshold()提供了两种自适应计算二值化阈值的算法,分别为ADAPTIVE_THRESH_MEAN_C和ADAPTIVE_THRESH_GAUSSIAN_C ,我们把两种方法都试下。
示例代码如下:
# 博主微信/QQ 2487872782
# 有问题可以联系博主交流
# 有图像处理需求也请联系博主
# 图像处理技术交流QQ群 271891601
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# OpenCV的版本为4.4.0
import numpy as np
import cv2 as cv
import sys
image = cv.imread('F:/material/images/2022/2022-06/img_300_320.jpg')
if image is None:
print('Error: Could not load image')
sys.exit()
# cv.imshow('Source Image', image)
img_gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
cv.imshow('img_gray', img_gray)
# 函数adaptiveThreshold()需要的部分参数
blockSize = 5
constValue = 0
maxVal = 255
img_B_MEAN = cv.adaptiveThreshold(img_gray, maxVal, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, blockSize, constValue)
img_B_GAUSSIAN = cv.adaptiveThreshold(img_gray, maxVal, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, blockSize, constValue)
cv.imshow('img_B_MEAN', img_B_MEAN)
cv.imshow('img_B_GAUSSIAN', img_B_GAUSSIAN)
cv.waitKey(0)
cv.destroyAllWindows()

从上面的运行结果我们可以看出,由于函数adaptiveThreshold()是将原灰度图进行了一小块一小块的处理,所以对图像的细节信息保留得比较多。
06-综合方法1、2、3、5的代码
由于本文中的方法01、02、03、05都是直接调用OpenCV的库函数实现,所以我们可以把这四种写在一个示例代码中,便于应用的时候方便对比,进而选用与自己需求更贴合的方法。
综合代码如下:
# 博主微信/QQ 2487872782
# 有问题可以联系博主交流
# 有图像处理需求也请联系博主
# 图像处理技术交流QQ群 271891601
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# OpenCV的版本为4.4.0
import numpy as np
import cv2 as cv
import sys
image = cv.imread('F:/material/images/2022/2022-06/img_300_320.jpg')
if image is None:
print('Error: Could not load image')
sys.exit()
# cv.imshow('Source Image', image)
img_gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
cv.imshow('img_gray', img_gray)
# 方法1:用户自己指定二值化时的阈值
Manual_threshold, img_bi_manual = cv.threshold(img_gray, 71, 255, cv.THRESH_BINARY)
cv.imshow('img_bi_manual', img_bi_manual)
# 方法2:用OTSU法确定二值化时的阈值
OTSU_threshold, img_bi_OTSU = cv.threshold(img_gray, 71, 255, cv.THRESH_OTSU)
cv.imshow('img_bi_OTSU', img_bi_OTSU)
# 方法3:用三角形法确定二值化时的阈值
TRIANGLE_threshold, img_bi_TRIANGLE = cv.threshold(img_gray, 71, 255, cv.THRESH_TRIANGLE)
cv.imshow('img_bi_TRIANGLE', img_bi_TRIANGLE)
# 方法5:使用函数adaptiveThreshold()进行自适应阈值处理
blockSize = 5
constValue = 0
maxVal = 255
# 方法5-1:MEAN_C自适应阈值
img_B_MEAN = cv.adaptiveThreshold(img_gray, maxVal, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, blockSize, constValue)
cv.imshow('img_B_MEAN', img_B_MEAN)
# 方法5-1:GAUSSIAN_C自适应阈值
img_B_GAUSSIAN = cv.adaptiveThreshold(img_gray, maxVal, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, blockSize, constValue)
cv.imshow('img_B_GAUSSIAN', img_B_GAUSSIAN)
cv.waitKey(0)
cv.destroyAllWindows()
运行结果如下:

边栏推荐
- Guys, for help, I use MySQL CDC 2.2.1 (Flink 1.14.5) to write Kafka and set
- Don't just learn Oracle and MySQL!
- From automation to digital twins, what can Tupo do?
- C # implementation defines a set of SQL statements that can be executed across databases in the middle of SQL (detailed explanation of the case)
- 2022CoCa: Contrastive Captioners are Image-Text Fountion Models
- Download the first Tencent technology open day course essence!
- 启牛开的证券账户安全吗?
- 6.26cf simulation match B: solution to array reduction problem
- 学习路之PHP--phpstudy创建项目时“hosts文件不存在或被阻止打开”
- The CDC of sqlserver can read the data for the first time, but it can't read the data after adding, deleting and modifying. What's the reason
猜你喜欢

神经网络物联网平台搭建(物联网平台搭建实战教程)

自由小兵儿

Halcon template matching

My colleagues quietly told me that flying Book notification can still play like this
![[发布] 一个测试 WebService 和数据库连接的工具 - DBTest v1.0](/img/4e/4154fec22035725d6c7aecd3371b05.jpg)
[发布] 一个测试 WebService 和数据库连接的工具 - DBTest v1.0

Scala basic tutorial -- 13 -- advanced function

Lex and yacc based lexical analyzer + parser

学习路之PHP--phpstudy创建项目时“hosts文件不存在或被阻止打开”

Use canal and rocketmq to listen to MySQL binlog logs

MXNet对GoogLeNet的实现(并行连结网络)
随机推荐
正则替换【JS,正则表达式】
【uniapp】uniapp开发app在线预览pdf文件
删除字符串中出现次数最少的字符【JS,Map排序,正则】
Li Kou brush question diary /day1/2022.6.23
2021 合肥市信息学竞赛小学组
Scala basic tutorial -- 14 -- implicit conversion
Halcon template matching
其他InterSystems %Net工具
Scala基础教程--16--泛型
2022年字节跳动日常实习面经(抖音)
国元期货是正规平台吗?在国元期货开户安全吗?
Torchdrug tutorial
Scala基础教程--18--集合(二)
Uni app and uviewui realize the imitation of Xiaomi mall app (with source code)
神经网络物联网应用技术学什么
Nebula importer data import practice
PB的扩展DLL开发(超级篇)(七)
Guys, for help, I use MySQL CDC 2.2.1 (Flink 1.14.5) to write Kafka and set
Learning path PHP -- phpstudy "hosts file does not exist or is blocked from opening" when creating the project
Li Kou brush question diary /day2/2022.6.24