分段使用MPI和OpenCV时的故障 [英] Segmentation Fault while using MPI and OpenCV together

查看:1864
本文介绍了分段使用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

~/mpi$ mpirun -np 2 ./a.out
libv4l2: error setting pixformat: Device or resource busy
HIGHGUI ERROR: libv4l unable to ioctl S_FMT
libv4l2: error setting pixformat: Device or resource busy
libv4l1: error setting pixformat: Device or resource busy
HIGHGUI ERROR: libv4l unable to ioctl VIDIOCSPICT

[arch:09670] *** Process received signal *** 
[arch:09670] Signal: Segmentation fault (11)
[arch:09670] Signal code: Address not mapped (1) 
[arch:09670] Failing at address: 0x218ac50
[arch:09670] [ 0] /usr/lib/libpthread.so.0(+0x10740)[0x7f422fcac740]
[arch:09670] [ 1] /usr/lib/libopencv_core.so.2.4(_ZNK2cv11_InputArray6getMatEi+0x203)[0x7f4233c8c113]
[arch:09670] [ 2] /usr/lib/libopencv_imgproc.so.2.4(_ZN2cv8cvtColorERKNS_11_InputArrayERKNS_12_OutputArrayEii+0x50)[0x7f4232c25de0]
[arch:09670] [ 3] ./a.out[0x408f54]
[arch:09670] [ 4] /usr/lib/libc.so.6(__libc_start_main+0xf0)[0x7f422e9e9800]
[arch:09670] [ 5] ./a.out[0x408c19]
[arch:09670] *** End of error message *** 
--------------------------------------------------------------------------
mpirun noticed that process rank 1 with PID 9670 on node arch exited on signal 11 (Segmentation fault).
--------------------------------------------------------------------------

这是代码

#include <opencv2/opencv.hpp>
#include <mpi.h>

int main(int argc, char **argv) {
    cv::Mat_<uint> img(640,480);
    cv::Mat_<uint> gray(640,480);
    cv::VideoCapture cam(0);

    int rank, nproc, j=0;

    MPI_Status status;

    MPI_Init(&argc, &argv);

    // MPI datatype for 8UC3 image
    MPI_Datatype mat_8uc3;
    MPI_Type_contiguous(sizeof(img), MPI_BYTE, &mat_8uc3);
    MPI_Type_commit(&mat_8uc3);

    // MPI datatype for 8UC1 image
    MPI_Datatype mat_8uc1;
    MPI_Type_contiguous(sizeof(gray), MPI_BYTE, &mat_8uc1);
    MPI_Type_commit(&mat_8uc1);

    MPI_Comm_size(MPI_COMM_WORLD, &nproc); // number of processes
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);  // rank of the current process

    /*
     * Thread 0 captures the image from camera
     * and sends the image to process 1 for processing
     * thread 1 converts the image to grayscale and
     * displays the image
     */

    if (rank == 0) {
        // capture the image and send to thread 1
        while (1) { 
            cam >> img;
            cv::imshow("proc 0", img);
            MPI_Send(&img, 1, mat_8uc3, 1, j, MPI_COMM_WORLD);
            cv::waitKey(40);
            j++;
        }
    }
    else if (rank == 1) {
        // receive the image, convert to grayscale and display
        while (1) {
            MPI_Recv(&img, 1, mat_8uc3, 0, j, MPI_COMM_WORLD, &status);
            cv::cvtColor(img, gray, cv::COLOR_BGR2GRAY);
            cv::imshow("proc 1", gray);
            cv::waitKey(20);
            j++;
        }
    }

    MPI_Finalize();

    return 0;
}

有人可以指出我出错的地方

Can someone point out where am I going wrong

感谢

编辑(在user0815回答后)

(after user0815's answer)

在进行建议的更改时,问题设备或资源繁忙已解决,但程序仍然提供segfault。

On making the suggested changes the problem Device or resource busy is resolved but the program still gives a segfault.

[arch:01080] *** Process received signal *** 
[arch:01080] Signal: Segmentation fault (11)
[arch:01080] Signal code: Address not mapped (1) 
[arch:01080] Failing at address: 0x16bbf80
[arch:01080] [ 0] /usr/lib/libpthread.so.0(+0x10740)[0x7fea97322740]
[arch:01080] [ 1] /usr/lib/libopencv_core.so.2.4(_ZNK2cv11_InputArray6getMatEi+0x203)[0x7fea9b302113]
[arch:01080] [ 2] /usr/lib/libopencv_imgproc.so.2.4(_ZN2cv8cvtColorERKNS_11_InputArrayERKNS_12_OutputArrayEii+0x50)[0x7fea9a29bde0]
[arch:01080] [ 3] ./a.out[0x408fc3]
[arch:01080] [ 4] /usr/lib/libc.so.6(__libc_start_main+0xf0)[0x7fea9605f800]
[arch:01080] [ 5] ./a.out[0x408c79]
[arch:01080] *** End of error message *** 
--------------------------------------------------------------------------
mpirun noticed that process rank 1 with PID 1080 on node arch exited on signal 11 (Segmentation fault).
--------------------------------------------------------------------------


推荐答案

目前每个进程都尝试打开相机。这很可能导致问题。尝试将开头移动到根特定部分如下:

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:

int main(int argc, char **argv) {
  cv::Mat_<uint> img(640,480);
  cv::Mat_<uint> gray(640,480);
  cv::VideoCapture cam;

  /* ... */
  if (rank == 0) {
    cam.open(0);
    /* ... */
  }
  /* ... */
}

更新:

我认为你的代码的问题是,使用 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

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时的故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆