我在尝试运行程序时收到分段错误。我只是在修改程序后从单独的线程上的两个摄像头捕获帧时才收到分段错误。我在两个线程上使用相同的功能,我想知道这是否是问题。
MAIN.C
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <opencv2/opencv.hpp>
#include "ThreadedWebcamCapture.h"
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
int main(){
ThreadedCapture Capture1;
Capture1.STOP = 0;
Capture1.CAP_NUMBER = 1;
Capture1.X_RESOLUTION = 640;
Capture1.Y_RESOLUTION = 480;
ThreadedCapture Capture2;
Capture2.STOP = 0;
Capture2.CAP_NUMBER = 0;
Capture2.X_RESOLUTION = 640;
Capture2.Y_RESOLUTION = 480;
pthread_t Capture1Thread;
int Capture1ThreadRetCode;
pthread_t Capture2Thread;
int Capture2ThreadRetCode;
printf("[INFO]Creating Threads\n");
Capture1ThreadRetCode = pthread_create(&Capture1Thread, NULL, &Capture_Thread, (void *)&Capture1);
printf("[INFO]Created Thread 1");
Capture2ThreadRetCode = pthread_create(&Capture2Thread, NULL, &Capture_Thread, (void *)&Capture2); //I think the error is occurring here
if(Capture1ThreadRetCode || Capture2ThreadRetCode != 0){
printf("[ERROR]Thread Creation Failed!\n");
exit(EXIT_FAILURE);
}
//cv::namedWindow("Window", cv::WINDOW_AUTOSIZE);
int NumberOfFrames;
time_t Seconds1;
Seconds1 = time(NULL);
cv::Mat Output;
while(1 == 1){
pthread_mutex_lock(&mutex1);
if(!Capture1.FRAME.empty() || !Capture2.FRAME.empty()){
cv::hconcat(Capture1.FRAME, Capture2.FRAME, Output);
}
pthread_mutex_unlock(&mutex1);
if(!Output.empty()){
NumberOfFrames = NumberOfFrames + 1;
cv::imshow("Window", Output);
}
if(cv::waitKeyEx(1) >= 0){
Capture1.STOP = 1;
Capture2.STOP = 1;
break;
}
}
time_t Seconds2;
Seconds2 = time(NULL);
int Seconds = Seconds2 - Seconds1;
int FPS = NumberOfFrames / Seconds;
printf("[INFO]FPS %d\n", FPS);
pthread_join(Capture1Thread, NULL);
pthread_join(Capture2Thread, NULL);
printf("[INFO]Done\n");
return(0);
}
cvthreads.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <opencv2/opencv.hpp>
#include "ThreadedWebcamCapture.h"
void *Capture_Thread(void* Test){
ThreadedCapture *Testing = (ThreadedCapture *) Test;
cv::VideoCapture CAP ((*Testing).CAP_NUMBER);
CAP.set(CV_CAP_PROP_FRAME_WIDTH, (*Testing).X_RESOLUTION);
CAP.set(CV_CAP_PROP_FRAME_HEIGHT, (*Testing).Y_RESOLUTION);
while((*Testing).STOP != 1){
CAP.read((*Testing).FRAME);
}
return(NULL);
}
ThreadedWebcamCapture.h
#ifndef _THREADEDWEBCAMCAPTURE_H_
#define _THREADEDWEBCAMCAPTURE_H_
void *Capture_Thread(void* Test);
typedef struct ThreadedCaptures {
int STOP;
int CAP_NUMBER;
int X_RESOLUTION;
int Y_RESOLUTION;
cv::Mat FRAME;
} ThreadedCapture;
#endif
答案 0 :(得分:1)
您的问题在于cv::hconcat
函数,特别是传递两个行数不同的cv::Mat
个对象。如果查看cv::hconcat
cv::hconcat
函数,它会说给定的两个矩阵必须具有相同的行数。您还可以看到在strace结果的最后几行中,有一个断言错误,指出发送到
"OpenCV Error: Assertion failed ("..., 205OpenCV Error: Assertion failed (src[i].dims <= 2 && src[i].rows == src[0].rows && src[i].type() == src[0].type()) in hconcat, file /builddir/build/BUILD/opencv-3.2.0/modules/core/src/matrix.cpp, line 2865
的行数不相等。
if(!Capture1.FRAME.empty() || !Capture2.FRAME.empty()){
cv::hconcat(Capture1.FRAME, Capture2.FRAME, Output);
}
现在,出现此错误的原因如下:
cv::hconcat
如果任何矩阵不为空,则执行水平并置操作。然而,这意味着其中一个矩阵可能具有640x480的大小而另一个可能具有0x0。在这种情况下,if(!Capture1.FRAME.empty() && !Capture2.FRAME.empty()){
cv::hconcat(Capture1.FRAME, Capture2.FRAME, Output);
}
会抛出一个断言错误,程序崩溃。要解决此问题,请尝试将条件更改为
ThreadedCapture
这样只有当两个矩阵都不为空时才执行操作。自从您构建具有相同行数的{{1}}对象以来,它们应具有相同的行数。