1.数梅派和opencv的图片截取
条件: 树梅派安装opencv和免驱动的摄像头
c源码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include "opencv.hpp" 4 5 6 int main(int argc, char **argv) { 7 if (argc < 2) { 8 fprintf(stderr, "Usage: ./webcam output_image_file_name\n"); 9 return 1; 10 } 11 12 /* init camera */ 13 CvCapture* pCapture = cvCreateCameraCapture(0); //初始化摄像头 14 if (NULL == pCapture) { 15 fprintf(stderr, "Can't initialize webcam!\n"); 16 return 1; 17 } 18 cvSetCaptureProperty(pCapture, CV_CAP_PROP_FRAME_WIDTH, 640); //设置图片宽度 19 cvSetCaptureProperty(pCapture, CV_CAP_PROP_FRAME_HEIGHT, 480); //设置图片高度 20 cvSetCaptureProperty(pCapture, CV_CAP_PROP_BRIGHTNESS, 20); //设置图片亮度 21 cvSetCaptureProperty(pCapture, CV_CAP_PROP_CONTRAST, 10); //设置图片对比度 22 IplImage *pFrame = cvQueryFrame(pCapture); //抓取一张图片 23 if(NULL == pFrame) { 24 fprintf(stderr, "Can't get a frame!\n" ); 25 return 1; 26 } 27 cvSaveImage(argv[1], pFrame); //保存到指定位置 28 cvReleaseCapture(&pCapture); //释放空间 29 return 0; 30 }
脚本
capture-image-from-camera-using-opencv.sh
1 #!/bin/bash 2 # A script to invoke a C program to capture an image from USB camera using OpenCV. 3 #CURRENT_DIR 目录 4 #dirname "$0" 指定当前 5 6 CURRENT_DIR=`dirname "$0" 7 WORKING_HOME=`cd "$CURRENT_DIR"; pwd` 8 9 WEBCAM_SOURCE=$WORKING_HOME/webcam.c 10 WEBCAM_BIN=$WORKING_HOME/webcam 11 OUTPUT_IMAGE_FILE=$WORKING_HOME/webcam.jpg 12 13 rm -f $OUTPUT_IMAGE_FILE 14 15 # compile the source code if the executable bin not exists 16 17 if [ ! -f $WEBCAM_BIN ]; then 18 echo "Compiling $WEBCAM_SOURCE ..." 19 g++ -I/usr/include/opencv2/ `pkg-config --cflags opencv --libs opencv` $WEBCAM_SOURCE -o $WEBCAM_BIN 20 if [ $? -ne 0 ]; then 21 echo "Failed to compile $WEBCAM_SOURCE" 22 exit 1 23 fi 24 fi 25 26 # run the program 27 $WEBCAM_BIN $OUTPUT_IMAGE_FILE 28 if [ $? -ne 0 ]; then 29 echo "Failed to capture image and output to file $OUTPUT_IMAGE_FILE" 30 exit 1 31 fi