Imdecode crashing

Hello everyone, I didn’t know exactly where to post this question as it is related both with Arduino and with openCV. I have an esp32-cam that is sending image bytes (JPEG format) through sockets to a ROS2 c++ node. This c++ node receives those bytes and then tries to create a cv::Mat image using the imdecode method the following way:

cv::Mat image = cv::imdecode(cv::Mat(header.width, header.height, CV_8UC3, bufferBytes), cv::IMREAD_COLOR);

where header.width and header.height are the width and height of the image (sent from esp32-cam as well via TCP), and bufferBytes contains the actual image bytes. It works most of the times, however, sometimes it crashes,. I believe it is due to a bad capture taking from the esp32 and that some of the JPEG bytes are corrupted, but how could I handle that? I tried to catch an exception but it’s not throwing any.

Thnaks for any help provided!

IF bufferBytes contains encoded bytes (like jpeg)
you’re supposed to give it a (1d) vector with the encoded bytes, not a 2d image

else, if if it contains uncompressed pixels, there should be no imdecode at all
(but you have width & height swapped above)

do you know, how many bytes to expect, and if all were read from tcp ?

I assume the image contains encoded bytes as the esp32-cam is encoding the image to JPEG before sending it. However, the esp32-cam also informs about the width and height of the image, which cannot be known beforehand as it depends for each capture. That’s why I was giving a 2d image to imdecode, however, I tried the following given your approach, but I am not sure if that’s what you meant:

cv::Mat image = cv::imdecode(cv::Mat(1, header.width * header.height, CV_8UC1, compressedBuffer), cv::IMREAD_COLOR);

Does this mean that if the JPEG is corrupted, there’s no way that imdecode can work at all, no matter the format in which I give the image vector (1d, 2d,…)

Many thanks!

no, still wrong, it needs the size of the compressed buffer, how many bytes you read from tcp

it might still return an incomplete image (idk), however, you cant control any of it.

1 Like

Thank you very much @berak, the problem was that I was giving imdecode a wrong image format. Right now, I’ve been able to receive 2000+ images without crashing when the largest amount of images previously didn’t reach 200.

I liked your last reply as I don’t know if there’s another way of marking a reply as the answer.

Again, many thanks for your help!