user2192424
user2192424

Reputation: 113

Opencv Error on Ubuntu Webcam (Logitech C270) Capture -> HIGHGUI ERROR: V4L/V4L2: VIDIOC_S_CROP

this erorr message appears on running simple camera capture on Ubuntu with logitech C270 (OpenCV 2.4.2/C++):

HIGHGUI ERROR: V4L/V4L2: VIDIOC_S_CROP

and further:

Corrupt JPEG data: 2 extraneous bytes before marker 0xd1 Corrupt JPEG data: 1 extraneous bytes before marker 0xd6 Corrupt JPEG data: 1 extraneous bytes before marker 0xd0 Corrupt JPEG data: 1 extraneous bytes before marker 0xd0

I get frames but the values of frame width and height swapped when writing to a Mat object see below:

Mat frame;
videoCapture = new VideoCapture(camId);
if(!videoCapture->isOpened()) throw Exception();

cout << "Frame width: " << videoCapture->get(CV_CAP_PROP_FRAME_WIDTH) << endl;
cout << "Frame height: " << videoCapture->get(CV_CAP_PROP_FRAME_HEIGHT) << endl;

(*videoCapture) >> frame;

cout << "Mat width: " << frame.rows << endl;
cout << "Mat height: " << frame.cols << endl;

Output:

Frame width: 640
Frame height: 480
Mat width: 480
Mat height: 640

Upvotes: 11

Views: 18534

Answers (5)

Kanaris007
Kanaris007

Reputation: 314

About issue:

Corrupt JPEG data: 2 extraneous bytes before marker 0xd1 Corrupt JPEG data: 1 extraneous bytes before marker 0xd6 Corrupt JPEG data: 1 extraneous bytes before marker 0xd0 Corrupt JPEG data: 1 extraneous bytes before marker 0xd0

Looks like, the issue is in libjpeg library. For some unknown reason it works incorrect under OpenCV library. I tried to compile without support of JPEG and it solved this issue.

cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_SHARED_LIBS=OFF -D BUILD_EXAMPLES=OFF -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D WITH_JPEG=OFF -D WITH_IPP=OFF ..

You can find all details in my blog:

http://privateblog.by/linux/opencv-i-corrupt-jpeg-data-na-linux/

Upvotes: 6

Ricardo Rosado
Ricardo Rosado

Reputation: 23

I would post this as a comment (not enough reputation), still, I got stuck here and the solution I found, though not elegant, was:

python my_app.py 2<&1 | grep -v "Corrupt JPEG data"

Note: To replicate normal python print statements behaviour I'm using os.system(f'echo {my_string}')

Upvotes: 1

jns
jns

Reputation: 1352

If you just want to get rid of the output quickly and grep -v Corrupt does not work for somehow - like for me - you could also redirect stderr to nothing, e.g.

./my_app 2> /dev/null

python my_app.py 2> /dev/null

This will of course hide other error messages, too.

Upvotes: 0

mpenkov
mpenkov

Reputation: 21896

If you don't feel like debugging the problem, and the frames from your webcam are being displayed without any issues, your option is to just shoot the messenger. The instructions below work if you have built OpenCV from source, as opposed to installing pre-built binaries.

Start with grep -R "Corrupt JPEG data" ~/src/opencv-2.4.4/ and go deeper into the rabbit hole until you find what you want. In my case the culprit is at opencv-2.4.4/thirdparty/libjpeg/jdmarker.c:908:

  if (cinfo->marker->discarded_bytes != 0) {
    WARNMS2(cinfo, JWRN_EXTRANEOUS_DATA, cinfo->marker->discarded_bytes, c);
    cinfo->marker->discarded_bytes = 0;
  }

The WARNMS2 macro is what's causing the error messages about extraneous data to be printed. Just comment it out, rebuild OpenCV and carry on with your work. I also have a C270, run Ubuntu 12.04, and experienced the same nagging error message until I did what I described above.

Upvotes: 7

carlodef
carlodef

Reputation: 2472

The width of an image is given by its number of columns. Your code should be

cout << "Mat width: " << frame.cols << endl;
cout << "Mat height: " << frame.rows << endl;

So there is no swap between width and height.

Upvotes: 2

Related Questions