【发布时间】:2018-04-12 10:39:16
【问题描述】:
我正在尝试使用 OpenCV 通过单独的线程连续流式传输来自 2 个摄像头的视频。以下代码显示Segmentation fault (core dumped)
这是什么原因,我该如何解决这个问题?
main.cpp
#include <iostream>
#include <pthread.h>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/videoio.hpp>
using namespace std;
struct thread_data {
string path;
int thread_id;
};
void *capture(void *threadarg)
{
struct thread_data *data;
data = (struct thread_data *) threadarg;
cv::VideoCapture cap(data->path);
if( !cap.isOpened())
{
std::cout<<"Not good, open camera failed"<<std::endl;
}
std::cout<< "Opened IP camera successfully!"<<std::endl;
cv::Mat frame;
string ext = ".jpg";
string result;
while (true) {
cap >> frame;
cv::imshow("Frame",frame);
cv::waitKey(1);
}
pthread_exit(NULL);
}
int main(void) {
pthread_t threads[2];
struct thread_data td[2];
int rc=0;
for( int i = 0; i < 2; i++ ) {
cout <<"main() : creating thread, " << i << endl;
td[i].thread_id = i;
td[0].path = "rtsp://admin:opencv123@192.168.1.23:554/Streaming/Channels/101/";
td[1].path = "rtsp://admin:opencv123@192.168.1.24:554/Streaming/Channels/101/";
rc = pthread_create(&threads[i], NULL, capture, (void *)&td[i]);
if (rc) {
cout << "Error:unable to create thread," << rc << endl;
exit(-1);
}
}
pthread_exit(NULL);
return 0;
}
日志:
main() : creating thread, 0 main() : creating thread, 1
Segmentation fault (core dumped)
当我尝试多次运行它时,我只能打开一个摄像头,而且它也不能连续流式传输。它会在几秒钟内启动和停止。
有时我会收到一条错误消息,上面写着
OpenCV Error: Insufficient memory (Failed to allocate 140703464366800 bytes) in OutOfMemoryError
我在 StackOverflow 上浏览了各种问答,但没有任何帮助。
【问题讨论】:
-
如果你是新手,那么你需要学习如何使用调试器并获得回溯。
-
@underscore_d 你不认为我们是边干边学吗?我这样做并陷入了上述问题。无论如何,谢谢!
-
当然,学习如何调试是一项基本技能,它将显着提高您的学习质量和速度。相比之下,SO 上的读者必须为您调试代码并不会获得相同的好处。
-
首先,因为您没有等待线程启动 - 您的 thread_data 有时会在您开始使用之前被销毁。
-
@underscore_d 完全同意。我被卡住了,所以我不得不询问并尝试使用谷歌搜索以及我能做的一切但没有找到解决方案。
标签: c++ multithreading opencv segmentation-fault