Imdecode function returns empty array

Hello Community,

I am trying to compress image datas and send them using udp protocol but in final step, imdecode funtion returns empty array even though my vector is not empty. How can I fix this?

Tools: C++, Ros2 Foxy
OS: Ubuntu 20.04

Code:

std::string result = lzw.decode(numbers);//LZW DECODE
std::string decoded_string = base64.base64_decode(result);
vector<uchar> data(decoded_string.begin(), decoded_string.end());
for(const char &c: data)
   std::cout << c;
cv::Mat img = imdecode(data, IMREAD_UNCHANGED);
std::cout << "IMAGEEEEEEEEEEEEEEEEEE" << img << std::endl;
cv::imshow("Img", img);

Results:


IMAGEEEEEEEEEEEEEEEEEE[]
Exception: OpenCV(4.5.5-dev) /home/kursatkomurcu/opencv_build/opencv/modules/highgui/src/window.cpp:967: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'

Best Regards

  • are you sure, you got all the data from your udp socket ?
    can you show, how you do this ?
    (are you trying to read it as a single packet ?)
  • get rid of the lzw encoding (your image is already encoded),
    and the silly base64 (which will only blow up your data by a factor of 3/2)
  • try to save the encoded binary data to a file on the server, and read it back in from disk in your program, then try imdecode() again (if that works, blame your udp code)
  • I controlled again and I get same string in each node.

  • My method is, compressing image using jpeg then convert it to string format and compress again this string using lzw because I need to much compression. Then I am decoding it.

  • Yes, i am trying to read one image now.

The code that publish data to udp node:

//Publisher
publisher_ = this->create_publisher<std_msgs::msg::String>("lzw_publisher", 10);
timer_ = this->create_wall_timer(500ms, std::bind(&image_compressor::timer_callback, this));

void timer_callback(){
            auto message = std_msgs::msg::String();
            message.data = n;
            RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str());
            publisher_->publish(message);
        }

Udp Subscriber:

subscription_ = this->create_subscription<std_msgs::msg::String>("lzw_publisher", 10, std::bind(&receiver::topic_callback, this, _1));

void topic_callback(const std_msgs::msg::String::SharedPtr msg) const
    {
        std::string s = msg->data;
        len = s.length();
        char *msgg = new char[len + 1];
        strcpy(msgg, s.c_str());

        udpSocket mUDP(ip, port);

        if (mUDP.sendRecv(msgg) > 0)
        {
            mUDP.printMsg();
            std::cout << "[" << mUDP.getRecvBytes() << "]" << mUDP.getRecvMsg() << std::endl;
        }
    }

okay.

not okay. that is silly. compression doesn’t work like that. with these antics you’ll end up with even more bytes. don’t do any of this.

debug your code. that means take it apart. all that UDP and network stuff is separate from the imencode/imdecode stuff. don’t make all of it work at once. take one aspect, verify that it works completely. don’t just look at code and variables, make the program check itself.

related question: