一起使用 MPI 和 OpenCV 时出现分段错误
问题描述
我正在尝试学习 C++ 中的 MPI.我对 OpenCV 有一些了解,所以我尝试使用 MPI 和 OpenCV 编写程序.这听起来可能很愚蠢,但为了学习,我尝试在线程 0 上从网络摄像头捕获图像并将图像传递给线程 1 以转换为灰度并显示灰度图像.
I am trying to learn MPI in C++. I have some knowledge of OpenCV so I tried writing a program using both MPI and OpenCV. This may sound stupid but for the purpose of learning I tried capturing an image from webcam on thread 0 and passed the image to thread 1 for converting to grayscale and displaying the grayscale image.
这就是我编译代码的方式:mpic++ opencv.cpp `pkg-config opencv --libs`
This is how I compile the code:
mpic++ opencv.cpp `pkg-config opencv --libs`
代码编译成功,但是当我运行可执行文件时,屏幕上会出现一小段时间的图像,这就是我在终端上看到的
The code compiles sucessfully but when I run the executable, an image shows up on the screen for a fraction of a second and this is what I see on the terminal
这是代码
谁能指出我哪里出错了
谢谢
(在 user0815 的回答之后)
(after user0815's answer)
在进行建议的更改时,设备或资源繁忙
问题已解决,但程序仍会出现段错误.
On making the suggested changes the problem Device or resource busy
is resolved but the program still gives a segfault.
推荐答案
目前每个进程都在尝试打开摄像头.这很可能会导致问题.尝试将开口移动到根特定部分,如下所示:
Currently each process tries to open the camera. That is very likely to cause problems. Try to move the opening into the root specific section like so:
更新:
我认为您的代码的问题在于,您不能简单地使用 MPI_Send
传输对象.sizeof
运算符通常对对象无效.如果要传输对象,则需要传输底层数据.
I think the problem with your code is, that you can't simply transfer objects with MPI_Send
. Also is the sizeof
operator not valid on objects in general. If you want to transfer an object, you need to transfer the underlying data.
您可以通过发送 img.data
大小为 img.rows * img.cols * sizeof(uint)
来实现这一点.然后你也可以使用 MPI_BYTE 作为数据类型,不需要自定义类型.
You could achieve this in your case by sending img.data
with a size of img.rows * img.cols * sizeof(uint)
. Then you can also use MPI_BYTE as data type and no custom types are required.
cv::Mat_
内部结构的一些细节可以参考这里.
Some details about the internal structure of cv::Mat_
can be found here.
这篇关于一起使用 MPI 和 OpenCV 时出现分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!