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:
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.
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:
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,…)
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.