我在 Linux 中调试这个小程序,编译代码:
gcc `pkg-config --cflags opencv` `pkg-config --libs opencv` -o videoHandler videoHandler.c
当我运行它时,我得到这个输出:
minscanline 1
minscanline 1
minscanline 1
minscanline 1
Video loaded succesfully
minscanline 1
Segmentation fault
我真正关心的是while循环,因为我需要获取电影文件的各个帧。有任何想法吗?
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#define HEIGHT 480
#define WIDTH 640
// Position the video at a specific frame number position
//cvSetCaptureProperty(video, CV_CAP_PROP_POS_FRAMES, next_frame);
// Convert the frame to a smaller size (WIDTH x HEIGHT)
//cvResize(img, thumb, CV_INTER_AREA);
int main(void){
CvCapture *video;
IplImage *image;
CvMat *thumb;
CvMat *encoded;
// Open the video file.
video = cvCaptureFromFile("sample.avi");
if (!video) {
// The file doesn't exist or can't be captured as a video file.
printf("Video could not load\n");
}else{
printf("Video loaded succesfully\n");
// Obtain the next frame from the video file
while ( image = cvQueryFrame(video) ) {
printf("Inside loop\n");
//If next frame doesn't exist, Video ended
thumb = cvCreateMat(HEIGHT, WIDTH, CV_8UC3);
// Encode the frame in JPEG format with JPEG quality 30%.
const static int encodeParams[] = { CV_IMWRITE_JPEG_QUALITY, 30 };
encoded = cvEncodeImage(".jpeg", thumb, encodeParams);
// After the call above, the encoded data is in encoded->data.ptr
// and has a length of encoded->cols bytes.
namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
imshow( "Display Image", encoded );
printf("Frame retrieved, length: %s\n", encoded->cols);
}
// Close the video file
cvReleaseCapture(&video);
}
return 0;
}