OpenCV threshold 二值化

::返回OpenCV算子速查表

cv::threshold

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

在这里插入图片描述

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;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MechMaster

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值