Error : CV_8U in function initialize

683 views Asked by At

I get an image from kinect using the ROS /camera/depth/image topic. This image encoding type is TYPE_32FC1. But I can't use this image. I want to do a background subtraction but I have an error as shown below:

     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

The code is shown below:

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

Thanks!

1

There are 1 answers

0
berak On

you will have to convert the image to single channel uchar before feeding it to BackgroundSubtractorMOG:

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