2

I'm trying to do edge detection with a p270 camera using OpenCV, and when I can get it to open up my webcam, all the data it has been received is coming back as corrupt. My code is very simple at the moment, just trying to get started but this gave me a headache. This is my code:

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
//#include <opencv\cv.h>
//#include <opencv\highgui.h>
#include <iostream>
using namespace cv;

//Mat src, src_gray;
//Mat dst, detected_edges;

int main() {
       VideoCapture capture(1);
       if(!capture.isOpened()) {
               printf("Camera failed to initialize\n");
               return 1;
       };
       Mat src, src_gray;
       Mat detected_edges;
       while(1) {
               capture >> src;
               //cvtColor(src, src_gray, CV_BGR2GRAY);
               //Canny(src_gray, detected_edges, 20, 60, 3);
               imshow("Source", src);
               //imshow("Gray Edges", detected_edges);

               //if(waitKey(10) >= 0) break;
       };

       return 0;
};

Also, the last line before the return should make it so that pressing the escape key closes the program, but it causes the program to close automatically for some reason. Any advice there?

This is an example of the errors I'm getting

Corrupt JPEG data: 10 extraneous bytes before marker 0xd6

Corrupt JPEG data: 6 extraneous bytes before marker 0xd3

Corrupt JPEG data: 2 extraneous bytes before marker 0xd0

Corrupt JPEG data: 6 extraneous bytes before marker 0xd3

Corrupt JPEG data: 2 extraneous bytes before marker 0xd3

Corrupt JPEG data: 4 extraneous bytes before marker 0xd6

This is the compilation command I use, in case it's a compile-time issue but I highly doubt that. Best to cover all my bases though

g++ -o edge edge_detection.cpp $(pkg-config opencv --cflags --libs)

If anyone can help I would greatly appreciate it.

I've tried running this with my standard laptop webcam, and while I don't get the corrupt jpeg data errors, I just get the following two lines of output and then the window that's supposed to hold the data coming from the webcam times out.

init done
opengl support available

By just getting it to get the first image frame from the webcam and then going to sleep I've determined that the code IS actually getting valid image data for at least the first image. Maybe there's a way I can space out how often gets the image so that it's not sending constant information. I don't see why it wouldn't work with constant info coming in, though. This is extraordinarily frustrating.

Things I tried

As you can see I have removed almost everything from the code that isn't needed but nothing has worked.

I have tried all three answers in this question but nothing helped: Opencv Error on Ubuntu Webcam (Logitech C270) Capture -> HIGHGUI ERROR: V4L/V4L2: VIDIOC_S_CROP

Thank you for any help!

Community
  • 1
  • 1
Harbinger
  • 55
  • 2
  • 9
  • try replacing `1` in your `while` condition with [`cv::waitKey(1) != 27`](http://docs.opencv.org/2.4/modules/highgui/doc/user_interface.html#waitkey) – slawekwin Feb 24 '17 at 07:52
  • @slawekwin This worked, thank you! Could you add this as an answer so you can get some credit (maybe an explanation)? – Harbinger Feb 26 '17 at 21:23

1 Answers1

1

When you wrote if(waitKey(10) >= 0) break you made your while loop always break after the first iteration because waitKey returns key pressed by the user or 0 if none was pressed during specified time (10ms in your case). To display your input you need to loop as long as waitKey does not return the key you expect (you said escape = 27 ASCII), so try:

   while (waitKey(10) != 27) {
           capture >> src;
           //cvtColor(src, src_gray, CV_BGR2GRAY);
           //Canny(src_gray, detected_edges, 20, 60, 3);
           imshow("Source", src);
           //imshow("Gray Edges", detected_edges);               
   };

This should display your images as long as the input stream can be decoded.

slawekwin
  • 6,270
  • 1
  • 44
  • 57