How to disable JPEG corruption message in OPENCV?

4.3k views Asked by At

I am working with images taken by a high speed IP camera which transmits them through Ethernet port. During transmission, a few bytes are lost or corrupted occasionally without any visual impact. When I load the images in a sequence using cvLoadImage, frequently a message like "Corrupt JPEG data: Premature end of data segment" or "Corrupt JPEG data: extraneous bytes before marker 0*d9" appears.

I don't want cvLoadImage to display such errors since they are common due to high speed transmission of images. Also printing to console is time-consuming since I might be processing upto 2000 images at a time.

How do I disable these messages. I have the Opencv 2.0 source and can dig into it if I get the right pointer.

P.S. I tried looking into grfmt_jpeg.cpp and similar source files but didn't find this error message anywhere.

2

There are 2 answers

0
kamjagin On

I don't think there is a nice and neat way of avoiding these error printouts - they originate from the core of libjpeg.

If you do want to dig in and see who is actually emitting the errors, take a look in 3rdparty/libjpeg/. Look at jerror.h/c.

PS. I think your particular error comes from the no_more_bytes-goto-flag in the jpeg_fill_bit_buffer() in jdhuff.c.

0
Lucas Walter On

I rebuilt opencv with BUILD_JPEG WITH_JPEG set to true so that opencv uses its own libjpeg rather than using the system one, and with the fprintf in 3rdparty/libjpeg/jerror.c commented out, which of course disables possibly more important messages than the minor annoyances that can flood stderr:

METHODDEF(void)
output_message (j_common_ptr cinfo)
{
  char buffer[JMSG_LENGTH_MAX];

  /* Create the message */
  (*cinfo->err->format_message) (cinfo, buffer);

#ifdef USE_WINDOWS_MESSAGEBOX
  /* Display it in a message dialog box */
  MessageBox(GetActiveWindow(), buffer, "JPEG Library Error",
             MB_OK | MB_ICONERROR);
#else
  /* Send it to stderr, adding a newline */
  // fprintf(stderr, "%s\n", buffer);
#endif

}

The much quicker option is redirecting the stderr to /dev/null e.g., and even more possibilities for legitimate errors are now unseen:

./cpp-example-bgfg_segm -m knn -c 2>/dev/null