当前位置:网站首页>与二值化阈值处理相关的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()
运行结果如下:
边栏推荐
- redis分布式锁的8大坑总结梳理
- 【OpenCV入门到精通之九】OpenCV之视频截取、图片与视频互转
- [opencv introduction to mastery 9] opencv video capture, image and video conversion
- 一种将Tree-LSTM的强化学习用于连接顺序选择的方法
- [go ~ 0 to 1] read, write and create files on the sixth day
- 2014 Hefei 31st youth informatics Olympic Games (primary school group) test questions
- Rookie post station management system based on C language
- C # implementation defines a set of SQL statements that can be executed across databases in the middle of SQL (detailed explanation of the case)
- 2022年字节跳动日常实习面经(抖音)
- Learning path PHP -- phpstudy "hosts file does not exist or is blocked from opening" when creating the project
猜你喜欢
Scala basic tutorial -- 15 -- recursion
Li Kou brush question diary /day3/2022.6.25
Nebula importer data import practice
Mxnet implementation of googlenet (parallel connection network)
[mathematical modeling of graduate students in Jiangxi Province in 2022] analysis and code implementation of haze removal by nucleation of water vapor supersaturation
Halcon template matching
基于C语言的菜鸟驿站管理系统
Li Kou brush question diary /day1/2022.6.23
Scala基础教程--18--集合(二)
Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
随机推荐
SSL证书续费相关问题详解
Scala基础教程--15--递归
Learning path PHP -- phpstudy "hosts file does not exist or is blocked from opening" when creating the project
Nebula Importer 数据导入实践
正则替换【JS,正则表达式】
LeetCode第300场周赛(20220703)
Torchdrug tutorial
Unity编辑器扩展C#遍历文件夹以及子目录下的所有图片
What if the self incrementing ID of online MySQL is exhausted?
C#实现定义一套中间SQL可以跨库执行的SQL语句(案例详解)
In flinksql, in addition to data statistics, is the saved data itself a state
Nature Microbiology | 可感染阿斯加德古菌的六种深海沉积物中的病毒基因组
26. 删除有序数组中的重复项 C#解答
[mathematical basis of machine learning] (I) linear algebra (Part 1 +)
【OpenCV入门到精通之九】OpenCV之视频截取、图片与视频互转
My colleagues quietly told me that flying Book notification can still play like this
Detailed explanation of issues related to SSL certificate renewal
[2022 Jiangxi graduate mathematical modeling] curling movement idea analysis and code implementation
Scala basic tutorial -- 19 -- actor
问下各位大佬有用过cdc直接mysql to clickhouse的么