OpenCV displaying a 2-channel image (optical flow)

前端 未结 2 1310
小蘑菇
小蘑菇 2021-02-05 22:11

I have optical flow stored in a 2-channel 32F matrix. I want to visualize the contents, what\'s the easiest way to do this?

How do I convert a CV_32FC2 to R

2条回答
  •  旧时难觅i
    2021-02-05 22:36

    imshow can handle only 1-channel gray-scale and 3-4 channel BRG/BGRA images. So you need do a conversion yourself.

    I think you can do something similar to:

    //extraxt x and y channels
    cv::Mat xy[2]; //X,Y
    cv::split(flow, xy);
    
    //calculate angle and magnitude
    cv::Mat magnitude, angle;
    cv::cartToPolar(xy[0], xy[1], magnitude, angle, true);
    
    //translate magnitude to range [0;1]
    double mag_max;
    cv::minMaxLoc(magnitude, 0, &mag_max);
    magnitude.convertTo(magnitude, -1, 1.0 / mag_max);
    
    //build hsv image
    cv::Mat _hsv[3], hsv;
    _hsv[0] = angle;
    _hsv[1] = cv::Mat::ones(angle.size(), CV_32F);
    _hsv[2] = magnitude;
    cv::merge(_hsv, 3, hsv);
    
    //convert to BGR and show
    cv::Mat bgr;//CV_32FC3 matrix
    cv::cvtColor(hsv, bgr, cv::COLOR_HSV2BGR);
    cv::imshow("optical flow", bgr);
    
    cv::waitKey(0);
    

提交回复
热议问题