OpenCV学习笔记(二)之图像阈值化

本文章由公号【开发小鸽】发布!欢迎关注!!!


老规矩–妹妹镇楼:

一. 图像阈值(Threshold)

(一). 阈值类型

1. 阈值二值化(Threshold Binary)

       首先指定像素的灰度值的阈值,遍历图像中像素值,如果像素的灰度值大于这个阈值,则将这个像素设置为最大像素值(8位灰度值最大为255);若像素的灰度值小于阈值,则将该像素点像素值赋值为0。公式以及示意图如下:
蓝色水平线代表阈值

2. 阈值反二值化(Threshold Binary Inverted)

       首先也要指定一个阈值,不同的是在对图像进行阈值化操作时与阈值二值化相反,当像素的灰度值超过这个阈值的时候为该像素点赋值为0;当该像素的灰度值低于该阈值时赋值为最大值。​公式及示意图如下:
在这里插入图片描述

3. 截断(Truncate)

       给定像素值阈值,在图像中像素的灰度值大于该阈值的像素点被设置为该阈值,而小于该阈值的像素值保持不变。公式以及示意图如下:
在这里插入图片描述

4. 阈值取零(Threshold To Zero)

       与截断阈值化相反,像素点的灰度值如果大于该阈值则像素值不变,如果像素点的灰度值小于该阈值,则该像素值设置为0.公式以及示意图如下:
在这里插入图片描述

5. 阈值反取零(Threshold To Zero Inverted)

       像素值大于阈值的像素赋值为0,而小于该阈值的像素值则保持不变,公式以及示意图如下:
在这里插入图片描述

二. API介绍

       阈值化API: cv::threshold​

double cv::threshold  ( 
	InputArray  src,  
  	OutputArray  dst,  
	double  thresh,  
	double  maxval,  
	int  type  
 )

       src:输入图像,须为单通道灰度图。
       dst:输出的边缘图像,为单通道黑白图。
       threshold:表示阈值
       max_value:表示最大值。
       threshold_type:表示二值化类型(即对图像取阈值的方法)

       下面是代码展示:

/*****图像阈值化*****/

#include<iostream>
#include<opencv2/opencv.hpp>
#include<string>
using namespace std;

cv::Mat src, dst;
string input_title = "input image";
string output_title = "binary image";
//阈值设置和最大值
int threshold_value = 127;
int threshold_max = 255;
void Threshold_Demo(int, void*);
//阈值类型设置
int type_value = 2;
int type_max = 4;

int main() {
	
	src = cv::imread("1.jpg");
	cv::namedWindow(input_title, cv::WINDOW_NORMAL);
	cv::namedWindow(output_title, cv::WINDOW_NORMAL);
	cv::imshow(input_title, src);

	//阈值调节
	cv::createTrackbar("Threshold Value:", output_title, &threshold_value,
		threshold_max, Threshold_Demo);
	//阈值类型调节
	cv::createTrackbar("Type Value:", output_title, &type_value,
		type_max, Threshold_Demo);
	Threshold_Demo(0, 0);

	cv::waitKey(0);
	return 0;
}

void Threshold_Demo(int, void*) {
	cv::cvtColor(src, dst, cv::COLOR_BGR2GRAY);
	cv::threshold(dst, dst, threshold_value, threshold_max,
		type_value);
	cv::imshow(output_title, dst);

}

!**

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值