启动摄像头代码,网上已经很多了,好像启动摄像头之后阈值化的没有多少,下面直接上代码,不过基础的东西还是自己尝试一下。
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include<opencv2/opencv.hpp>
using namespace cv;
#define WINDOW_NAME "窗口"
int g_nThresholdValue=100; //阈值初始值
int g_nThresholdType=0;
void on_Threshold(int,void*);
Mat frame;
Mat edges;
int main()
{
VideoCapture cap(0);
if(!cap.isOpened())
{
return -1;
}
bool stop = false;
while(!stop)
{
cap>>frame;
cvtColor(frame, edges, CV_BGR2GRAY);
createTrackbar("模式",WINDOW_NAME,&g_nThresholdType,4,on_Threshold);
createTrackbar("阈值",WINDOW_NAME,&g_nThresholdValue,255,on_Threshold);
on_Threshold(0,0);
if(waitKey(30) >=0)
stop = true;
}
return 0;
}
void on_Threshold(int,void*)
{
//进行阈值分割
threshold(edges,edges,g_nThresholdValue,255,g_nThresholdType);
//显示结果
imshow(WINDOW_NAME,edges);
}
本文介绍了一个使用OpenCV库实现的摄像头阈值处理程序。通过调节不同的阈值类型和阈值大小,可以实现实时视频流的灰度转换及二值化处理。代码中还包含了用于调整阈值的滑动条,方便用户直观地看到不同设置下的效果。
1129

被折叠的 条评论
为什么被折叠?



