错误:CV_8U在函数初始化中



我使用ROS/相机/深度/图像主题从kinect获得图像。此图像编码类型为type_32FC1。但是我不能用这个图片。我想做一个背景减法,但我有一个错误,如下所示:

     terminate called after throwing an instance of 'cv::Exception'     what():/build/buildd/opencv-.4.8+dfsg1/modules/video/src/bgfg_gaussmix.cpp:117 :
     error: (-215) CV_MAT_DEPTH(frameType) == CV_8U in function initialize

代码如下所示:

class ImageConverter
{
   //convert image pointer
   cv_bridge::CvImagePtr cv_ptr;
   //to get ros node
   ros::NodeHandle nh_;
   image_transport::ImageTransport it_;
   image_transport::Subscriber image_sub_;
    //for back_sub
    //global variables
    Mat frame; //current frame
    Mat fgMaskMOG; //fg mask generated by MOG method
   Ptr<BackgroundSubtractor> pMOG; //MOG Background subtractor
   public:
  ImageConverter(): it_(nh_)
     {
       //using ros subscribe node to get dept image 
       image_sub_ = it_.subscribe("camera/depth/image", 1, &ImageConverter::imageCb, this);
    //create MOG object
    pMOG= new BackgroundSubtractorMOG(); //MOG approach
     }
     void imageCb(const sensor_msgs::ImageConstPtr& msg)
        {
     //convert depth data to opencv
     cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::TYPE_32FC1);
     processVideo();
     cv::waitKey(3);
        }

     void processVideo()
     {
        //showing depth image
        imshow("Image", cv_ptr->image);
        //using MOG function to subtract image
        pMOG->operator()(cv_ptr->image, fgMaskMOG);
        //showing background suntraction image
        imshow("Image2", fgMaskMOG);
     }
     };
       //test 
      int main(int argc, char** argv)
       {
           ros::init(argc, argv, "subtract_node");
           ImageConverter ic;
           ros::spin();
           return 0;
       }

谢谢!

在将图像馈送到BackgroundSubtractactorMOG:之前,您必须将图像转换为单通道uchar

Mat gray;
cv_ptr->image.convertTo( gray, CV_8U, 255 ); // unsure about the scale.
pMOG->operator()(gray, fgMaskMOG);

相关内容

  • 没有找到相关文章

最新更新