创建一个opencv程序,代码如下
test.cpp
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
using namespace cv;
using namespace std;
int main()
{
//VideoCapture capture("video.aiv");//打开指定视频
VideoCapture capture(0);//打开摄像头
if (!capture.isOpened())//没有打开摄像头的话,就返回。
return 1;
Mat img1;
Mat img_binary;
while (true)
{
Mat frame;
capture >> frame;
cvtColor(frame, img1, CV_BGR2GRAY);//将原图转为灰阶图
threshold(img1, img_binary, 100, 255, CV_THRESH_BINARY);//将灰度图转为二值图
putText(frame, "Hello Word!!", Point(40, 60), FONT_HERSHEY_COMPLEX_SMALL, 2, Scalar(125, 125, 225), 1, 8, false);//显示字体在原图中
imshow("原图", frame);
imshow("灰度图", img1);
imshow("二值图", img_binary);
//waitKey(40);//等待40毫秒
if (cvWaitKey(50) == 27){//按esc键退出
IplImage tmp = IplImage(frame);
CvArr* arr = (CvArr*)&tmp;
cvSaveImage("Image.png", arr, 0);//保存当前屏幕上的图像到本地
break;
}
}
return 0;
}
利用g++和pkg_conofig进行编译:
g++ test.cpp -o test `pkg-config --cflags --libs opencv`