OpenCV 2.3: Convert Mat to RGBA pixel array

前端 未结 3 1549
一向
一向 2020-12-10 07:23

I am attempting to use OpenCV to grab frames from a webcam and display them in a window using SFML.

VideoCapture returns frames in OpenCV\'s Mat format. To display t

相关标签:
3条回答
  • 2020-12-10 07:42

    I like the accepted answer better but this snippet helps you understand what's going on.

     for (int i=0; i<srcMat.rows; i++) {
                for (int j=0; j<srcMat.cols; j++) {
                    int index = (i*srcMat.cols+j)*4;
                    // copy while converting to RGBA order
                    dstRBBA[index + 0] = srcMat[index + 2 ];
                    dstRBBA[index + 1] = srcMat[index + 1 ];
                    dstRBBA[index + 2] = srcMat[index + 0 ];
                    dstRBBA[index + 3] = srcMat[index + 3 ];
                }
            }
    
    0 讨论(0)
  • 2020-12-10 07:45

    For me worked following code:

    VideoCapture capture(0);
    Mat mat_frame;
    capture >> mat_frame; // get a new frame from camera            
    
    // Be sure that we are dealing with RGB colorspace...
    Mat rgbFrame(width, height, CV_8UC3);
    cvtColor(mat_frame, rgbFrame, CV_BGR2RGB);
    
    // ...now let it convert it to RGBA
    Mat newSrc = Mat(rgbFrame.rows, rgbFrame.cols, CV_8UC4);
    int from_to[] = { 0,0, 1,1, 2,2, 3,3 };
    mixChannels(&rgbFrame, 2, &newSrc, 1, from_to, 4);
    

    The result (newSrc) is a premultiply image!

    0 讨论(0)
  • 2020-12-10 07:55

    OpenCV can do all job for you:

    VideoCapture cap(0);
    Mat frame;
    cap >> frame;
    
    uchar* camData = new uchar[frame.total()*4];
    Mat continuousRGBA(frame.size(), CV_8UC4, camData);
    cv::cvtColor(frame, continuousRGBA, CV_BGR2RGBA, 4);
    img.LoadFromPixels(frame.cols, frame.rows, camData);
    
    0 讨论(0)
提交回复
热议问题