zoukankan      html  css  js  c++  java
  • opencv 摄像头 线程

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <pthread.h>
    #include <iostream>
    #include <unistd.h>
    #include <signal.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <semaphore.h>
    #include <sys/shm.h>
    #include <sys/ipc.h>
    #include <sys/sem.h>
    #include <sys/stat.h>
    #include "opencv.hpp"
    #include <fcntl.h>
    #include <errno.h>
    #include <ctype.h>

    #define OUTPUT_VIDEO_FILE "webcam.avi"
    #define OUTPUT_VIDEO_FILE1 "webcam1.avi"
    #define OUTPUT_VIDEO_FILE2 "a.k"

    using namespace std;
    using namespace cv;

    char message[32] = "Hello World";
    //子进程 先父进程
    void *thread_function(void *arg){

    string outputVideoFile =OUTPUT_VIDEO_FILE;
    VideoCapture videoCapture(0); //初始化摄像头
    if (!videoCapture.isOpened()){
    cout << "Failed to open the default camera" << endl;
    return NULL;
    }
    /* 获取视频帧的宽度和高度 */
    double frameWidth = videoCapture.get(CV_CAP_PROP_FRAME_WIDTH);
    double frameHeight = videoCapture.get(CV_CAP_PROP_FRAME_HEIGHT);
    cout << "Frame size is [" << frameWidth << "x" << frameHeight << "]" << endl;
    /* 创建一个VideoWriter的对象将视频流写入文件 */
    Size frameSize(static_cast<int>(frameWidth), static_cast<int>(frameHeight));
    /* videoWriter(输出的文件名,输出文件的编码,帧率,帧的尺寸,是否是彩色(尽支持win)) */
    //('D', 'I', 'V', 'X') mp4
    VideoWriter videoWriter(outputVideoFile.c_str(), CV_FOURCC('D', 'I', 'V', 'X'), 20, frameSize, true);
    if (!videoWriter.isOpened()){
    cout << "Failed to initialize the VideoWriter" << endl;
    return NULL;
    }
    while(true){ //图片
    Mat frame;
    if (!videoCapture.read(frame)){ // 抓住,解码并返回下一个视频帧
    cout << "Failed to read a video frame" << endl;
    break;
    }
    videoWriter.write(frame); //写入文件的框架
    if(27 == waitKey(30)){ //等待按下“ESC”键(注意:不是工作如果没有窗口被创建!)
    cout << "ESC key pressed, stop capturing video" << endl;
    break;
    }
    }
    return 0;
    printf("子进程 %s ", (char *)arg);
    //pthread_exit("Thank you for the cpu time");
    }

    int main(int argc, char *argv[]){
    pthread_t a_thread; //线程id
    void *thread_result;


    /*使用缺省属性创建线程*/
    if (pthread_create(&a_thread, NULL,thread_function, (void *)argv[1]) < 0){
    perror("fail to pthread_create");
    exit(-1);
    }
    printf("父进程 ");
    // 等待线程结束(资源回收)
    if (pthread_join(a_thread, &thread_result) < 0){
    perror("fail to pthread_join");
    exit(-1);
    }
    printf("MESSAGE is now %s ", message);
    return 0;
    }

  • 相关阅读:
    17._4Csharp中正则表达式的匹配
    15.13DataGridView单元格自动填充
    C#安装,启动,停止,卸载Windows服务
    C# 如何判断系统是32位还是64位
    [转]Excel关闭进程
    利用.Net中Process类调用netstat命令来判断计算端口的使用情况
    转:C# 获取指定目录下所有文件信息、移动目录、拷贝目录
    做安装包,安装后自动运行程序
    ...
    SharpZIP Lib
  • 原文地址:https://www.cnblogs.com/jikexianfeng/p/5725609.html
Copyright © 2011-2022 走看看