Reputation: 425
I am receiving a segmentation fault when trying to run my program. I am only receiving segmentation faults after modifying my program to capture frames from two cameras on separate threads. I am using the same function on both threads and I am wondering if this is the problem.
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
Upvotes: 0
Views: 721
Reputation: 1193
Your problem is with cv::hconcat
function, specifically with passing two cv::Mat
objects with different number of rows. If you look at the documentation of cv::hconcat
function, it says the given two matrices must have the same number of rows. You can also see that in the last lines of your strace result, there is an assertion error stating that number of rows sent to cv::hconcat
weren't equal.
"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
Now, the reason for this error is in these lines:
if(!Capture1.FRAME.empty() || !Capture2.FRAME.empty()){
cv::hconcat(Capture1.FRAME, Capture2.FRAME, Output);
}
You perform the horizontal concatenation operation if any of the matrices is not empty. However, this implies that one of the matrices may have size 640x480 and the other may have 0x0. In that case, cv::hconcat
throws an assertion error and your program crashes. To fix it, try changing the condition to
if(!Capture1.FRAME.empty() && !Capture2.FRAME.empty()){
cv::hconcat(Capture1.FRAME, Capture2.FRAME, Output);
}
so that you perform the operation only when both of the matrices are not empty. They should have the same number of rows since you build your ThreadedCapture
objects with the same number of rows.
Upvotes: 1