OpenCV 13 - 图像对比度和亮度的调节基本阈值操作

本文详细介绍了图像阈值在OpenCV中的应用,包括各种阈值类型如二值化、反二值化等,以及自动计算阈值的Otsu算法和THRESH_TRIANGLE方法。并提供了C++代码示例,展示了如何在实际项目中使用这些技术处理图像。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 图像阈值

图像阈值是一种图像处理技术,用于将图像转换为二值图像。通过设定一个阈值,将图像中的像素值与阈值进行比较,将大于或小于阈值的像素分别设置为不同的像素值,从而实现图像的二值化处理。

2 阈值类型

2-1 阈值类型-阈值二值化(threshold binary)

在这里插入图片描述

2-2 阈值类型-阈值反二值化(threshold binary Inverted)

在这里插入图片描述

2-3 阈值类型-截断(truncate)

在这里插入图片描述

2-4 阈值类型-阈值取零(threshold to zero)

在这里插入图片描述

2-5 阈值类型-阈值反取零(threshold to zero inverted)

在这里插入图片描述

2-6 自动计算阈值:THRESH_OTSU

使用Otsu算法选择最佳阈值

2-7 自动计算阈值:THRESH_TRIANGLE

使用三角算法选择最佳阈值

3 代码演示

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

using namespace cv;

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

int type = 1;
int type_max = 4;


const char *WINDNAME = "binary image";

void Threshold_demo(int, void*) {

	cvtColor(src, gray_src, CV_RGB2GRAY);
	threshold(gray_src, dst, threshold_value, threshold_max, type);
	//threshold(gray_src, dst, threshold_value, threshold_max, THRESH_OTSU| type);
	//threshold(gray_src, dst, threshold_value, threshold_max, THRESH_TRIANGLE | type);
	imshow(WINDNAME, dst);

}
int main(int argc, char** argv)
{
	src = imread("./1.png");
	if (!src.data)	//判断图片是否加载成功!
	{
		std::cout << "打开图片失败!" << std::endl;
		return -1;
	}
	imshow("src image", src);

	
	namedWindow(WINDNAME,WINDOW_AUTOSIZE);
	createTrackbar("threshold value:", WINDNAME, &threshold_value, threshold_max, Threshold_demo);
	createTrackbar("type is:", WINDNAME, &type, type_max, Threshold_demo);
	waitKey(100000);
	return 0;
}

总结:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

江凡心

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

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

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

打赏作者

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

抵扣说明:

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

余额充值