OPENCV学习笔记之基本阈值操作

本文详细介绍使用OpenCV进行图像阈值处理的过程,包括如何通过创建滑动条动态调整阈值和模式,实现对二值图像的实时预览。代码示例展示了如何读取图像、转换为灰度图,并应用不同的阈值类型。

包含头文件,现在有很大的疑惑,什么时候要包含什么样的头文件

#include<opencv2/opencv.hpp>
//#include<opencv2/core/core.hpp>
//#include<opencv2/highgui/highgui.hpp>
//#include<opencv2/imgproc/imgproc.hpp>
//#include<iostream>
#include<math.h>
using namespace cv;
using namespace std;

定义变量及声明函数

Mat src, gray, dst;
int threshold_value =127;
int type_value=2;
int type_max = 4;
int threshold_max = 255;
const char*output_title = "binary image";
void Threshold_Demo(int, void*);

主函数:

int main(int angc, char** argv)
{
	src = imread("1.jpg");
	cvtColor(src, gray, COLOR_RGB2GRAY);//转为灰度图。
    imshow("载入灰度图", gray);
//另一想法是直接 src=imread("1.jpg",0);//载入灰度图
			 //imshow("载入灰度图", src);
	namedWindow(output_title, WINDOW_AUTOSIZE);


	createTrackbar("阈值", output_title, &threshold_value, threshold_max, Threshold_Demo);//滑块名称,窗口名称,改变的量,量的最大值,
	createTrackbar("模式", output_title, &type_value, type_max, Threshold_Demo);
	Threshold_Demo(0, 0);

	waitKey(0);
	return(0);
	
}

Threshold_Demo()函数

void Threshold_Demo(int, void*)
{
	printf("%d", THRESH_BINARY);
	threshold(gray, dst, threshold_value, threshold_max, type_value);
	imshow(output_title, dst);
}

效果如下

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值