我是靠谱客的博主 欢喜皮卡丘,最近开发中收集的这篇文章主要介绍OpenCV threshold 二值化1. 函数定义2. 例程,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

::返回OpenCV算子速查表

cv::threshold

  • 1. 函数定义
  • 2. 例程

1. 函数定义

	double threshold(
		InputArray src, 
		OutputArray dst,
		double thresh, //阈值
		double maxval, //给定的最大值
		int type  //操作类型
		);
  • 《学习OpenCV3》的说明
阈值类型操作
cv::THRESH_BINARYDST=(SRC>thresh)?MAXVALUE:0
cv::THRESH_BINARY_INVDST=(SRC>thresh)?0:MAXVALUE
cv::THRESH_TRUNCDST=(SRC>thresh)?THRESH:SRC
cv::THRESH_TOZERODST=(SRC>thresh)?SRC:0
cv::THRESH_TOZERO_INVDST=(SRC>thresh)?0:SRC

在这里插入图片描述

  • OpenCV官方文档的说明
    https://docs.opencv.org/4.5.4/d7/d1b/group__imgproc__misc.html#gaa9e58d2860d4afa658ef70a9b1115576
    请添加图片描述
    在这里插入图片描述
  • 特殊值THRESH_OTSUTHRESH_TRIANGLE可以与上述值之一组合使用。在这些情况下,函数使用Otsu或Triangle算法确定最佳阈值,并使用它而不是指定的阈值。

2. 例程

在这里插入图片描述

#include "stdafx.h"
#include <opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{	
	//命名窗口
	namedWindow("电芯", WINDOW_NORMAL);
	namedWindow("THRESH_BINARY", WINDOW_NORMAL);
	namedWindow("THRESH_BINARY_INV", WINDOW_NORMAL);
	namedWindow("THRESH_TRUNC", WINDOW_NORMAL);
	namedWindow("THRESH_TOZERO", WINDOW_NORMAL);
	namedWindow("THRESH_TOZERO_INV", WINDOW_NORMAL);
	//namedWindow("THRESH_MASK", WINDOW_NORMAL); //不支持 官方文档也找不到它的解释
	//特殊值THRESH_OTSU或THRESH_TRIANGLE可以与上述值之一组合使用。
	//在这些情况下,函数使用Otsu或Triangle算法确定最佳阈值,并使用它而不是指定的阈值。
	namedWindow("THRESH_OTSU", WINDOW_NORMAL);
	namedWindow("THRESH_TRIANGLE", WINDOW_NORMAL);


	//定义图像变量
	Mat m_ImgCell = imread("./1.bmp", IMREAD_GRAYSCALE);
	Mat m_ImgThreshBinary;
	Mat m_ImgThreshBinaryInv;
	Mat m_ImgThreshTrunc;
	Mat m_ImgThreshToZero;
	Mat m_ImgThreshToZeroInv;
	//Mat m_ImgThreshMask;
	Mat m_ImgThreshOtsu;
	Mat m_ImgThreshTriangle;
	
	//设置阈值和最大值
	double m_dbThresh = 150;
	double m_dbMaxValue = 255;	

	//阈值化操作
	threshold(m_ImgCell, m_ImgThreshBinary, m_dbThresh, m_dbMaxValue, THRESH_BINARY);
	threshold(m_ImgCell, m_ImgThreshBinaryInv, m_dbThresh, m_dbMaxValue, THRESH_BINARY_INV);
	threshold(m_ImgCell, m_ImgThreshTrunc, m_dbThresh, m_dbMaxValue, THRESH_TRUNC);
	threshold(m_ImgCell, m_ImgThreshToZero, m_dbThresh - 70, m_dbMaxValue, THRESH_TOZERO);
	threshold(m_ImgCell, m_ImgThreshToZeroInv, m_dbThresh - 70, m_dbMaxValue, THRESH_TOZERO_INV);
	//threshold(m_ImgCell, m_ImgThreshMask, m_dbThresh, m_dbMaxValue, THRESH_MASK);
	//以下这两种自己设置的阈值不再起作用,而真正起作用的阈值会作为返回值返回
	double m_dbOtsuThresh = threshold(m_ImgCell, m_ImgThreshOtsu, 0, m_dbMaxValue, THRESH_BINARY | THRESH_OTSU);
	double m_dbTriangleThresh = threshold(m_ImgCell, m_ImgThreshTriangle, 0, m_dbMaxValue, THRESH_BINARY | THRESH_TRIANGLE);
	
	//显示图像
	imshow("电芯", m_ImgCell);
	imshow("THRESH_BINARY", m_ImgThreshBinary);
	imshow("THRESH_BINARY_INV", m_ImgThreshBinaryInv);
	imshow("THRESH_TRUNC", m_ImgThreshTrunc);
	imshow("THRESH_TOZERO", m_ImgThreshToZero);
	imshow("THRESH_TOZERO_INV", m_ImgThreshToZeroInv);
	//imshow("THRESH_MASK", m_ImgThreshMask);
	imshow("THRESH_OTSU", m_ImgThreshOtsu);
	imshow("THRESH_TRIANGLE", m_ImgThreshTriangle);
	
	cout << m_dbOtsuThresh << endl;
	cout << m_dbTriangleThresh << endl;
	waitKey(0);
    return 0;
}

最后

以上就是欢喜皮卡丘为你收集整理的OpenCV threshold 二值化1. 函数定义2. 例程的全部内容,希望文章能够帮你解决OpenCV threshold 二值化1. 函数定义2. 例程所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部