- using namespace cv;
- using namespace std;
- mutex mtxQueueInput_1,mtxQueueInput_2; // 两个全局变量,1对应第一个摄像头。2对应第二个摄像头。
- queue<Mat> queueInput_1,queueInput_2; // 同样,1显示第一个摄像头数据,2同理。
- int get_video_1()
- {
- char video[] = "/dev/video10" ;
- int cpuid = 0; // 4个线程 4个函数 分别绑定到4个CPU上
- cpu_set_t mask;
- CPU_ZERO(&mask);
- CPU_SET(cpuid, &mask);
- if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0)
- {
- printf("set thread affinity failed");
- return(FALSE);
- }
- printf("Bind CameraCapture process to CPU %d\n", cpuid);
- cv::Mat img( IMAGE_HEIGHT, IMAGE_WIDTH, CV_8UC(3),cv::Scalar(0,0,0));
- struct v4l2_buffer dequeue;
- dequeue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE ;
- dequeue.memory = V4L2_MEMORY_MMAP ;
- BUF *buffers = (struct buffer *)malloc(COUNT*(sizeof (*buffers)));
- unsigned char *srcBuffer = (unsigned char*)malloc(sizeof(unsigned char) * BUFFER_SIZE_src); // yuyv
- unsigned char *dstBuffer = (unsigned char*)malloc(sizeof(unsigned char) * BUFFER_SIZE_det); // RGB
- /********************* function start *********************/
- buffers = v4l2(video,buffers) ;
- while(1)
- {
- srcBuffer = get_img(dequeue,buffers,srcBuffer);
- dstBuffer = yuyv2rgb24(srcBuffer,dstBuffer);
- img = unchar_to_Mat(dstBuffer,img);
- mtxQueueInput_1.lock();
- queueInput_1.push(img);
- if (queueInput_1.size() >= 30)
- {
- mtxQueueInput_1.unlock();
- printf("Img in queueInput is too much !");
- sleep(1);
- if (queueInput_1.size() >= 60)
- {
- printf("queue is full !\n");
- return(FALSE);
- }
- }else{
- mtxQueueInput_1.unlock();
- }
- }
- free(buffers);
- free(srcBuffer);
- free(dstBuffer);
- close_v4l2(buffers);
- return(TRUE);
- }
- int get_video_2() // get_video_1 和 get_video_2 除了 摄像头、需要绑定的CPU 不一样外,其余都一样。displayImage_1 和 displayImage_2 的情况类似。
复制代码
每次执行这个文件,都要开个好几次才会显示出画面,即使有画面也坚持不了 1 min 。每次卡住都是因为队列里数据太多,所以休眠卡住,但图像还是没能继续显示,使得程序一直卡住,然后崩溃退出。- int get_video_2()
- {
- char video[] = "/dev/video12" ;
- int cpuid = 1;
- cpu_set_t mask;
- CPU_ZERO(&mask);
- CPU_SET(cpuid, &mask);
- if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0)
- {
- printf("set thread affinity failed");
- return(FALSE);
- }
- printf("Bind CameraCapture process to CPU %d\n", cpuid);
- cv::Mat img( IMAGE_HEIGHT, IMAGE_WIDTH, CV_8UC(3),cv::Scalar(0,0,0));
- struct v4l2_buffer dequeue;
- dequeue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE ;
- dequeue.memory = V4L2_MEMORY_MMAP ;
- BUF *buffers = (struct buffer *)malloc(COUNT*(sizeof (*buffers)));
- unsigned char *srcBuffer = (unsigned char*)malloc(sizeof(unsigned char) * BUFFER_SIZE_src); // yuyv
- unsigned char *dstBuffer = (unsigned char*)malloc(sizeof(unsigned char) * BUFFER_SIZE_det); // RGB
- /********************* function start *********************/
- buffers = v4l2(video,buffers) ;
- while(1)
- {
- srcBuffer = get_img(dequeue,buffers,srcBuffer);
- dstBuffer = yuyv2rgb24(srcBuffer,dstBuffer);
- img = unchar_to_Mat(dstBuffer,img);
- mtxQueueInput_2.lock();
- queueInput_2.push(img);
- if (queueInput_2.size() >= 30)
- {
- mtxQueueInput_2.unlock();
- printf("Img in queueInput is too much !");
- sleep(1);
- if (queueInput_2.size() >= 60)
- {
- printf("queue is full !\n");
- return(FALSE);
- }
- }else{
- mtxQueueInput_2.unlock();
- }
- }
- free(buffers);
- free(srcBuffer);
- free(dstBuffer);
- close_v4l2(buffers);
- return(TRUE);
- }
- int displayImage_1()
- {
- int cpuid = 2;
- Mat img;
- cpu_set_t mask;
- CPU_ZERO(&mask);
- CPU_SET(cpuid, &mask);
- if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0){
- cerr << "set thread affinity failed" << endl;
- }
- printf("Bind Display process to CPU %d\n", cpuid);
- while (1)
- {
- mtxQueueInput_1.lock();
- if (queueInput_1.empty())
- {
- mtxQueueInput_1.unlock();
- usleep(10);
- }
- else
- {
- img = queueInput_1.front();
- cv::imshow("image_10", img); // display image
- queueInput_1.pop();
- mtxQueueInput_1.unlock();
- }
- if (waitKey(25) == 'Q') {
- break;
- }
- }
- return(TRUE);
- }
- int displayImage_2()
- {
- int cpuid = 3;
- Mat img;
- cpu_set_t mask;
- CPU_ZERO(&mask);
- CPU_SET(cpuid, &mask);
- if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0){
- cerr << "set thread affinity failed" << endl;
- }
- printf("Bind Display process to CPU %d\n", cpuid);
- while (1)
- {
- mtxQueueInput_2.lock();
- if (queueInput_2.empty())
- {
- mtxQueueInput_2.unlock();
- usleep(10);
- }
- else
- {
- img = queueInput_2.front();
- cv::imshow("image_12", img); // display image
- queueInput_2.pop();
- mtxQueueInput_2.unlock();
- }
- if (waitKey(25) == 'Q') {
- break;
- }
- }
- return(TRUE);
- }
- int main()
- {
- array<thread, 4> threads;
- threads = {thread(get_video_1),
- thread(displayImage_1),
- thread(get_video_2),
- thread(displayImage_2)};
- for (int i = 0; i < 4; i++){
- threads[i].join();
- }
- return(TRUE);
- }
复制代码
欢迎光临 Toybrick (https://t.rock-chips.com/) | Powered by Discuz! X3.3 |