为什么我不能让3个摄像头与pthread并行运行?

问题描述 投票:0回答:1

我需要有关使用C ++进行线程的OpenCV的帮助。我正在使用树莓派3B。这是四核。有4个USB 2.0设备,3个USB 2.0网络摄像头和USB 2.0 Arduino。网络摄像头的电缆已被剪断以自行提供电压,因此只有数据线进入pi。 Arduino由pi驱动。现在到问题。我有4个线程,其中3个用于网络摄像头,另外1个用于Arduino。网络摄像头等待Arduino将信号并行发送到网络摄像头。问题是,我无法让3台摄像机同时工作。我可以使用2台摄像机的任意组合,但不能使用3台。当我尝试使用3台网络摄像头时,出现空框错误。

Link To Webcam Used

我尝试没有成功:

sudo modprobe uvcvideo nodrop=1 timeout=5000 quirks=0x80
[ WARN:2] global /home/pi/opencv/modules/videoio/src/cap_v4l.cpp (1004) tryIoctl VIDEOIO(V4L2:/dev/video0): select() timeout.
Empty Frame
terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(4.3.0) /home/pi/opencv/modules/imgcodecs/src/loadsave.cpp:738: error: (-215:Assertion failed) !_img.empty() in function 'imwrite'

Aborted
    #include<stdio.h>
    #include<stdlib.h>
    #include<thread>
    #include<iostream>
    #include<fstream>

    #include<opencv2/highgui/highgui.hpp>
    #include<opencv2/imgproc/imgproc.hpp>
    #include<opencv2/core/core.hpp>
    #include"pthread.h"
    #include<stdlib.h>
    #include<string>
    #include<boost/date_time/posix_time/posix_time.hpp>

    #include<mutex>
    #include<condition_variable>
    #include<atomic>


    std::mutex m;
    std::condition_variable suspend_cv;
    std::atomic<bool> enabled (false);

    struct args {
        int camNum;
        char* camName;
        int x;
        int y;
        };


    void* camSetup(void *inputs){
    cv::VideoCapture stream(((struct args*)inputs)->camNum);
    cv::Mat Frame;
    //cv::Mat resizeFrame;
    if(!stream.isOpened()){
        std::cout << "Cannot Open Camera: " + ((struct args*) inputs)->camNum + '\n';
    }
    else{
        std::cout << "Camera Open: " << ((struct args*) inputs)->camNum + '\n';
    }
    std::unique_lock<std::mutex> lock(m);
    while (true){
        stream >> Frame;
        //cv::resize(Frame, resizeFrame, cv::Size(25, 25));
        while (enabled){
            // Get current time from the clock, using microseconds resolution
            const boost::posix_time::ptime now = 
                boost::posix_time::microsec_clock::local_time();

            // Get the time offset in current day
            const boost::posix_time::time_duration td = now.time_of_day();
            const long year         = now.date().year();
            const long month        = now.date().month();
            const long day          = now.date().day();
            const long hours        = td.hours();
            const long minutes      = td.minutes();
            const long seconds      = td.seconds();
            const long milliseconds = td.total_milliseconds() -
                                      ((hours * 3600 + minutes * 60 + seconds) * 1000);

            char buf[80];
            sprintf(buf, "%02ld%02ld%02ld_%02ld%02ld%02ld__%03ld", 
                year, month, day, hours, minutes, seconds, milliseconds);
                std::string sBuf = buf;
                std::string PATH = std::string("/home/pi/Desktop/") +
                                ((struct args*)inputs)->camName + 
                                    '/' +
                                    std::string(((struct args*)inputs)->camName) +
                                    '_' +
                                    sBuf +
                                    ".jpeg";
            if (Frame.empty())
            {
                std::cout << "Empty Frame" <<std::endl;
                stream >> Frame;
                //cv::resize(Frame, resizeFrame, cv::Size(25, 25));

            }
            cv::imwrite(PATH, Frame);
            if (cv::waitKey(30) >= 0){
                break;
            }
            suspend_cv.wait(lock);
        }
    }
    return NULL;
    }


    void* ardReader(void*){
    ARDUINO STUFF HAPPENS…
    }

    int main(){
    struct args *cameraA = (struct args *)malloc(sizeof(struct args));
    struct args *cameraB = (struct args *)malloc(sizeof(struct args));
    struct args *cameraC = (struct args *)malloc(sizeof(struct args));

    cameraA->camNum = 0;
    char camA[] = "camA";
    cameraA->camName = camA;
    cameraA->x = 100;
    cameraA->y =100;

    cameraB->camNum = 2;
    char camB[] = "camB";
    cameraB->camName = camB;
    cameraB->x = 100;
    cameraB->y = 300;

    cameraC->camNum = 4;
    char camC[] = "camC";
    cameraC->camName = camC;
    cameraC->x = 100;
    cameraC->y = 500;

    pthread_t t1, t2, t3, t4;

    pthread_create(&t1, NULL, camSetup, (void *) cameraA);
    pthread_create(&t2, NULL, camSetup, (void *) cameraB);
    pthread_create(&t3, NULL, camSetup, (void *) cameraC);
    pthread_create(&t4, NULL, ardReader, NULL);

    pthread_join(t1, NULL);
    pthread_join(t2, NULL);
    pthread_join(t3, NULL);
    pthread_join(t4, NULL);
    return 0;
    }
c++ multithreading opencv pthreads
1个回答
0
投票

似乎20mS的差异对线程/ opencv产生了足够大的差异。如果有人再次遇到此问题,要回答我自己的问题,请将waitkey时间更改为以下...

在这种情况下,线程不正确

        if (cv::waitKey(30) >= 0){
            break;
        }

在这种情况下更适合穿线

        if (cv::waitKey(10) == 27){
            break;
        }

现在,我能够从所有三个网络摄像头捕获图像。

© www.soinside.com 2019 - 2024. All rights reserved.