Create a one-channel cv::mat object from 2d cv::mat object

    const cv::Mat depth_img =
        cv_bridge::toCvCopy(msg->depth_image,
                            sensor_msgs::image_encodings::TYPE_16UC1)
            ->image;
    cv::Mat dep(depth_img.rows, depth_img.cols, CV_32FC1, depth_img.data);

now dep has size [1408, 1024], but I want it to have a channel in the last dimension, i.e. size [1408, 1024, 1]. how should I do this?

I’ve read many stackoverflow answers and they talk about merge, but I don’t really need it since I only need it to be one channel.

that’s ROS, isn’t it? we don’t know about ROS here.

anyway, if the rows and cols are right, you should then check channels(). if that is 1, then you have achieved your goal.

opencv does not distinguish between those two. they’re the same.

why do you think they’re different? how did you determine those shapes?

they are later feed to libtorch for machine learning usage, which requires a 3D data. but I find I can directly reshape the tensor