Falha na segmentação ao usar MPI e OpenCV juntos

Estou tentando aprender MPI em C ++. Eu tenho algum conhecimento de OpenCV, então tentei escrever um programa usando o MPI e o OpenCV. Isso pode parecer estúpido, mas, com o objetivo de aprender, tentei capturar uma imagem da webcam no segmento 0 e passei a imagem para o segmento 1 para converter em escala de cinza e exibir a imagem em escala de cinza.

É assim que eu compilo o código:
mpic++ opencv.cpp `pkg-config opencv --libs`

O código é compilado com êxito, mas quando executo o executável, uma imagem aparece na tela por uma fração de segundo e é isso que vejo no 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).
--------------------------------------------------------------------------

aqui está o código

#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;
}

Alguém pode apontar onde estou errado

obrigado

EDITAR: (após a resposta do usuário0815)

Ao fazer as alterações sugeridas, o problemaDevice or resource busy foi resolvido, mas o programa ainda apresenta um 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).
--------------------------------------------------------------------------

questionAnswers(3)

yourAnswerToTheQuestion