启动摄像头代码,网上已经很多了,好像启动摄像头之后阈值化的没有多少,下面直接上代码,不过基础的东西还是自己尝试一下。
#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);
}