获得OpenCV的工作(段错误)
问题描述:
我在Linux调试这个小程序与编译代码:获得OpenCV的工作(段错误)
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;
}
答
我没有立即看到的问题是什么,但这里是你可以做什么来帮助自己(或更新的问题有帮助您更好的机会在这里):
编译并启用警告:
gcc -Wall -Wextra `pkg-config --cflags opencv` `pkg-config --libs opencv` -o videoHandler videoHandler.c
然后修复您得到的任何警告(或编辑问题以添加它们,如果无法找到它们)。
2nd在调试器下运行你的程序,看看哪一行触发段错误。除此之外,如果你仍然无法弄清楚这个问题,那么也可以将相关的变量值(通过添加调试打印或通过调试器检查)添加到该问题中。
如果还是解决不了,在你的应用程序运行valgrind(如果你使用的是Windows,然后安装Linux VM并在其下运行它,我一般用的VirtualBox +最新的Lubuntu虚拟磁盘映像)。
其实,你应该尝试valgrind即使你解决这个问题,看看它带给你的警告,而如果这些实际上都是错误,你应该修复(也可能产生假阳性,甚至他们中的很多一些库)。
谢谢你,我会考虑它的。更新:部分工作。 – 2013-04-04 20:01:16
@PatricioJerí还有一件事要检查:验证视频是否与视频播放器应用程序一起播放,还要用另一个视频测试您的应用程序,最好使用不同的编码。有无效的视频文件引发的错误或者有效的文件上有效但经过严格测试的视频编解码器并不是不可能的,这会导致Segfault。 – hyde 2013-04-04 20:24:37
用%s表示整数的最后一个printf()是错误的。这就是为什么你会遇到段错误。用%d改变%s。问候 – 2013-10-25 09:53:40