(1)在树莓派USB接口中插入webcamera,使用如下命令检测是否检测到camera ? ? ?cd /dev ? ? ?ls | grep video 如果有个设备名字是videox(x是数字)
(2)编写代码,流程很简单,首先获取摄像头,然后再while(1)中显示采集到的视频,按下esc退出图像采集。
#include <opencv/cv.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc,char **argv)
{
??????? //获取摄像头
??? CvCapture* pCapture = cvCreateCameraCapture(-1);
??? cvSetCaptureProperty(pCapture,CV_CAP_PROP_FRAME_WIDTH,320);
??? cvSetCaptureProperty(pCapture,CV_CAP_PROP_FRAME_HEIGHT,240);
??? //cvSetCaptureProperty(pCapture, CV_CAP_PROP_FPS, 5);
??????? //声明IplImage指针
??? IplImage *pFrame = 0;
??? if (NULL == pCapture)
??? {
??????? fprintf(stderr, “Can’t initialize webcam!\n”);
??????? return 1;
??? }
??? //创建窗口
??? cvNamedWindow(“WebCamera”);
?? //视频显示
??? while(1)
??? {
??????? pFrame = cvQueryFrame(pCapture);? // query a frame
??????? cvShowImage(“WebCamera”,pFrame);
??????? char c = cvWaitKey(40);
??????? if(c==27)
??????????????? break;
??? }
??? if(NULL == pFrame)
??? {
?????vps云服务器?? fprintf(stderr, “Can’t get a frame!\n” );
??????? return 1;
??? }
??? cvReleaseCapture(&pCapture);? // free memory
??? cvDestroyWindow(“WebCamera”);
??? return 0;
}
(3)编写makefile CC = g++
# 可执行文件
TARGET = webcamera
# C文件
SRCS = webcamera.cpp
# 目标文件
OBJS = $(SRCS:.cpp=.o)
# 库文件
DLIBS = -lopencv_core -lopencv_imgproc -lopencv_highgui
# 链接可执行文件
$(TARGET):$(OBJS)
??????? $(CC) -o $@ $^ $(DLIBS)
clean:
??????? rm -rf $(TARGET) $(OBJS)
# 编译规则 $@代表目标文件 $<代表第一个依赖文件
%.o:%.cpp
??????? $(CC) -o $@ -c $<
(4)编译,make
? ? ?
(5)运行,./webcamera
? ? ?运行效果: 00849039