当前位置:网站首页>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 :
边栏推荐
- 基于NCF的多模块协同实例
- 1672. Total assets of the richest customers
- Don't just learn Oracle and MySQL!
- LM10丨余弦波动顺势网格策略
- 2019年蜀山区第十五届青少年信息学竞赛
- 奥迪AUDI EDI INVOIC发票报文详解
- One question per day (2022-07-02) - Minimum refueling times
- 在线SQL转Excel(xls/xlsx)工具
- 2022-07-04: what is the output of the following go language code? A:true; B:false; C: Compilation error. package main import 'fmt' func
- Li Chi's work and life summary in June 2022
猜你喜欢
随机推荐
Scala basic tutorial -- 20 -- akka
The page element is vertically and horizontally centered, realizing the vertical and horizontal centering of known or unknown width.
升级智能开关,“零火版”、“单火”接线方式差异有多大?
联想首次详解绿色智城数字孪生平台 破解城市双碳升级难点
Using FTP
Oracle with as ora-00903: invalid table name multi report error
There are multiple divs in the large div, which are displayed on the same line. After overflow, scroll bars are generated without line breaks
876. 链表的中间结点
C#实现定义一套中间SQL可以跨库执行的SQL语句(案例详解)
repeat_P1002 [NOIP2002 普及组] 过河卒_dp
QT realizes interface sliding switching effect
Oracle with as ORA-00903: invalid table name 多表报错
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-07-02)——最低加油次数
Wechat reading notes of "work, consumerism and the new poor"
In flinksql, in addition to data statistics, is the saved data itself a state
Caché WebSocket
How test engineers "attack the city" (Part I)
Go microservice (II) - detailed introduction to protobuf
node_exporter部署