您的位置:首页 > 运维架构

opencv对摄像头采集视频的几种变换

2014-04-15 19:49 513 查看
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace cv;
using namespace std;

Mat Sobel(Mat src);//Sobel边缘检测;
Mat Laplace(Mat src);//拉普拉斯边缘检测;
Mat Threshold(Mat src);//改变阈值
void threshold(int , void*);//改变阈值的回调函数

int threshold_type = 0;//阈值操作类型;
int threshold_value = 0;//阈值
int const max_type = 3;
int const max_value = 255;
int const max_binvalue = 255;
Mat thresh_temp,thresh_dst;

int main(){
VideoCapture cap(0);
if(!cap.isOpened()){
cout<<"摄像头未能打开"<<endl;
}
namedWindow("Video");
namedWindow("Sobel");
namedWindow("Laplace");
namedWindow("Threshold");
while(true){
Mat frame;
cap>>frame;
if(cap.isOpened() && (char)waitKey(1)!='q'){
imshow("Video",frame);
imshow("Sobel",Sobel(frame));
imshow("Laplace",Laplace(frame));
imshow("Threshold",Threshold(frame));
}
else break;
}
return 0;
}
Mat Sobel(Mat src){
Mat dst,temp,abs_dst;
cvtColor(src,temp,CV_RGB2GRAY);
Sobel(temp,dst,CV_16S,0,1,3,1,0,BORDER_DEFAULT);
convertScaleAbs(dst,abs_dst);
return abs_dst;
}
Mat Laplace(Mat src){
Mat dst,temp,abs_dst;
cvtColor(src,temp,CV_RGB2GRAY);
Laplacian(temp,dst,CV_16S,3,1,0,BORDER_DEFAULT);
convertScaleAbs(dst,abs_dst);
return abs_dst;
}

Mat Threshold(Mat src){

cvtColor(src,thresh_temp,CV_RGB2GRAY);
createTrackbar("ThresholdType","Threshold",&threshold_type,max_type,threshold);
createTrackbar("ThresholdValue","Threshold",&threshold_value,max_value,threshold);
threshold(0,0);
return thresh_dst;
}
void threshold(int,void*){
threshold(thresh_temp,thresh_dst,threshold_value,255,threshold_type);
}
具有一定的可玩性。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息