# Imdecode function returns empty array

**URL:** <https://forum.opencv.org/t/imdecode-function-returns-empty-array/8656>\
**Category:** C++\
**Tags:** imgcodecs, ros, programming\
**Created:** [May 6, 2022, 8:05am UTC](https://forum.opencv.org/t/imdecode-function-returns-empty-array/8656 "2022-05-06T08:05:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kursatkomurcu](https://avatars.discourse-cdn.com/v4/letter/k/ecccb3/32.png) [@kursatkomurcu](https://forum.opencv.org/u/kursatkomurcu)\
**Post date:** [May 6, 2022, 8:05am UTC](https://forum.opencv.org/t/imdecode-function-returns-empty-array/8656/1 "2022-05-06T08:05:29Z")

</div>

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:

```auto
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:

```auto

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

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [May 6, 2022, 8:37am UTC](https://forum.opencv.org/t/imdecode-function-returns-empty-array/8656/3 "2022-05-06T08:37:39Z")

</div>

- 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)

---

<div class="post-metadata">

**Author:** ![kursatkomurcu](https://avatars.discourse-cdn.com/v4/letter/k/ecccb3/32.png) [@kursatkomurcu](https://forum.opencv.org/u/kursatkomurcu)\
**Post date:** [May 6, 2022, 9:03am UTC](https://forum.opencv.org/t/imdecode-function-returns-empty-array/8656/4 "2022-05-06T09:03:11Z")

</div>

- 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:

```auto
//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:

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

```

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [May 6, 2022, 9:25am UTC](https://forum.opencv.org/t/imdecode-function-returns-empty-array/8656/7 "2022-05-06T09:25:17Z")

</div>

> [@kursatkomurcu](#):
>
> My method is, compressing image using jpeg

okay.

> [@kursatkomurcu](#):
>
> then convert it to string format and compress again this string using lzw because I need to much compression.

**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:

[https://stackoverflow.com/questions/72138217/imdecode-function-returns-none](https://stackoverflow.com/questions/72138217/imdecode-function-returns-none)
