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!