13 OpenCV基本阈值操作threshold

一、基本阈值操作

opencv提供了threshold()函数对图像的阈值进行处理,threshold()共支持五种类型的阈值化方式,分别是阈值二值化、阈值反二值化、截断、阈值取零和阈值反取零。
在这里插入图片描述

  • 阈值二值化 threshold binary
    在这里插入图片描述
  • 阈值反二值化 threshold binary inverted
    在这里插入图片描述
  • 截断 truncate
    在这里插入图片描述
  • 阈值取零 threshold to zero
    在这里插入图片描述
  • 阈值反取零 threshold to zero inverted
    在这里插入图片描述

二、threshold()

double cv::threshold  ( InputArray  src,  
  OutputArray  dst,  
  double  thresh,  
  double  maxval,  
  int  type  
 ) 
  • InputArray src: 输入图像,可以是Mat类型,图像必须为单通道8位或32位浮点型图像
  • OutputArray dst: 输出图像,与输入图像尺寸和类型相同
  • double thresh: 设定的阈值
  • double maxval: 使用THRESH_BINARY和THRESH_BINARY_INV类型的最大值
  • int type: 阈值化类型

三、示例

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;

Mat src, dst, gray_src;
int threshold_value = 127;
int threshold_max = 255;

const char* output_title = "binary image";
void Threshold_Demo(int, void*);

int main()
{
	src = imread("images/02.png");
	if (!src.data) {
		cout << "could not load image1..." << endl;
		return -1;
	}
	namedWindow("input_image", CV_WINDOW_AUTOSIZE);
	imshow("input_image", src);

	Threshold_Demo(0, 0);
	createTrackbar("Threshold Value:", output_title, &threshold_value, threshold_max, Threshold_Demo);
	

	waitKey();
	return 0;
}
void Threshold_Demo(int, void*)
{
	cvtColor(src, gray_src, CV_BGR2GRAY);
	
	threshold(gray_src, dst, threshold_value,threshold_max,THRESH_BINARY);
	imshow(output_title, dst);
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值