当前位置:网站首页>Opencv functions and methods related to binary threshold processing are summarized for comparison and use
Opencv functions and methods related to binary threshold processing are summarized for comparison and use
2022-07-04 19:28:00 【Haohong image algorithm】
Binary threshold processing is the basis of many image processing requirements , And it is the foundation of the foundation , This step has a great impact on the follow-up .
In order to improve the performance of the algorithm , It is necessary to select the appropriate threshold for processing as much as possible .
In this paper OpenCV Functions related to binarization threshold processing in 、 Methods to summarize , It is convenient for comparison and use in actual development .
The basis of this paper is function threshold(), If you want to know more about functions first threshold(), You can refer to the blog https://blog.csdn.net/wenhao_ir/article/details/125592684
Catalog
- 01- Manually control the slider to find the best threshold with human eyes
- 02- adopt OTSU( Maximum inter class variance method ) Look for the threshold of binarization
- 03- adopt TRIANGLE( Triangle method ) Look for the threshold of binarization
- 04- The threshold of image binary segmentation is obtained by the maximum entropy of image histogram
- 05- utilize OpenCV Function of adaptiveThreshold() Realize adaptive binary threshold segmentation of image
- 06- Integrated approach 1、2、3、5 Code for
01- Manually control the slider to find the best threshold with human eyes
See the blog for this method and sample code https://blog.csdn.net/wenhao_ir/article/details/51539122
02- adopt OTSU( Maximum inter class variance method ) Look for the threshold of binarization
The principle of this method and C++ See the blog for example code https://blog.csdn.net/wenhao_ir/article/details/51179117
The above blog post only gives C++ Example code for , Here's another one Python Code .
Take the following figure as an example :
Baidu online disk download link of the above picture :https://pan.baidu.com/s/1IaJ8nrQzGuHt3RA8jbu0GQ?pwd=bjkm
Python The sample code is as follows :
# Blogger WeChat /QQ 2487872782
# If you have any questions, you can contact the blogger
# Please contact the blogger if you need image processing
# Image processing technology exchange QQ Group 271891601
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# OpenCV The version is 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()
The operation results are as follows :
From the screenshot above, we can see that OTSU The threshold found by the algorithm is 135
03- adopt TRIANGLE( Triangle method ) Look for the threshold of binarization
Python The sample code is as follows :
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# OpenCV The version is 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()
The operation results are as follows :
04- The threshold of image binary segmentation is obtained by the maximum entropy of image histogram
The principle and code of getting the threshold value of image binary segmentation through the maximum entropy of image histogram are shown in the blog https://blog.csdn.net/wenhao_ir/article/details/51671023
For the image
The running results of the code in the above blog post are as follows :
05- utilize OpenCV Function of adaptiveThreshold() Realize adaptive binary threshold segmentation of image
function adaptiveThreshold() And C++ See the blog for example code https://blog.csdn.net/wenhao_ir/article/details/51565517
Let's add another Python Sample code .
Due to function adaptiveThreshold() In the process of image binarization , The threshold is constantly changing , So we can't act like a function threshold() In that way, the threshold value for image binarization is obtained .
Its Python The prototype is as follows :
dst = cv.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C[, dst])
function adaptiveThreshold() Two algorithms for adaptive calculation of binary threshold are provided , Respectively ADAPTIVE_THRESH_MEAN_C and ADAPTIVE_THRESH_GAUSSIAN_C , Let's try both methods .
The sample code is as follows :
# Blogger WeChat /QQ 2487872782
# If you have any questions, you can contact the blogger
# Please contact the blogger if you need image processing
# Image processing technology exchange QQ Group 271891601
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# OpenCV The version is 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)
# function adaptiveThreshold() Some parameters required
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()
From the above results, we can see , Due to function adaptiveThreshold() It is to process the original gray-scale image piece by piece , Therefore, the details of the image are preserved more .
06- Integrated approach 1、2、3、5 Code for
Due to the method in this article 01、02、03、05 It's all direct calls OpenCV Library function implementation of , So we can write these four in a sample code , It is convenient for comparison when it is convenient for application , Then choose a method that fits your needs better .
The synthesis code is as follows :
# Blogger WeChat /QQ 2487872782
# If you have any questions, you can contact the blogger
# Please contact the blogger if you need image processing
# Image processing technology exchange QQ Group 271891601
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# OpenCV The version is 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)
# Method 1: The user specifies the threshold value for binarization
Manual_threshold, img_bi_manual = cv.threshold(img_gray, 71, 255, cv.THRESH_BINARY)
cv.imshow('img_bi_manual', img_bi_manual)
# Method 2: use OTSU Method to determine the threshold value of binarization
OTSU_threshold, img_bi_OTSU = cv.threshold(img_gray, 71, 255, cv.THRESH_OTSU)
cv.imshow('img_bi_OTSU', img_bi_OTSU)
# Method 3: Determine the threshold value of binarization by triangle method
TRIANGLE_threshold, img_bi_TRIANGLE = cv.threshold(img_gray, 71, 255, cv.THRESH_TRIANGLE)
cv.imshow('img_bi_TRIANGLE', img_bi_TRIANGLE)
# Method 5: Using functions adaptiveThreshold() Adaptive threshold processing
blockSize = 5
constValue = 0
maxVal = 255
# Method 5-1:MEAN_C Adaptive threshold
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)
# Method 5-1:GAUSSIAN_C Adaptive threshold
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()
The operation results are as follows :
边栏推荐
- A method of using tree LSTM reinforcement learning for connection sequence selection
- Using SSH
- 1672. Total assets of the richest customers
- Have you guys ever used CDC direct Mysql to Clickhouse
- Li Chi's work and life summary in June 2022
- Caché JSON 使用JSON适配器
- 每日一题(2022-07-02)——最低加油次数
- 关于判断点是否位于轮廓内的一点思考
- 启牛开的证券账户安全吗?
- Learning path PHP -- phpstudy "hosts file does not exist or is blocked from opening" when creating the project
猜你喜欢
OpenCV的二值化处理函数threshold()详解
2022CoCa: Contrastive Captioners are Image-Text Fountion Models
Scala basic tutorial -- 19 -- actor
关于判断点是否位于轮廓内的一点思考
Summary and sorting of 8 pits of redis distributed lock
To sort out messy header files, I use include what you use
Use canal and rocketmq to listen to MySQL binlog logs
DeFi生态NFT流动性挖矿系统开发搭建
node_exporter部署
FPGA时序约束分享01_四大步骤简述
随机推荐
Hough Transform 霍夫变换原理
Is Guoyuan futures a regular platform? Is it safe to open an account in Guoyuan futures?
如何使用Async-Awati异步任务处理代替BackgroundWorker?
联想首次详解绿色智城数字孪生平台 破解城市双碳升级难点
问下各位大佬有用过cdc直接mysql to clickhouse的么
数组中的第K个最大元素
LM10丨余弦波动顺势网格策略
每日一题(2022-07-02)——最低加油次数
Guys, for help, I use MySQL CDC 2.2.1 (Flink 1.14.5) to write Kafka and set
使用SSH
Shell programming core technology "I"
在线SQL转Excel(xls/xlsx)工具
Learning path PHP -- phpstudy "hosts file does not exist or is blocked from opening" when creating the project
876. Intermediate node of linked list
Unity adds a function case similar to editor extension to its script, the use of ContextMenu
How test engineers "attack the city" (Part 2)
启牛开的证券账户安全吗?
FTP, SFTP file transfer
QT realizes interface sliding switching effect
自由小兵儿