2

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;
}
4

1 に答える 1

1

問題が何であるかはすぐにはわかりませんが、自分でできることは次のとおりです(または、質問を更新して、ここで助けが得られる可能性を高めます)。

警告を有効にした最初のコンパイル:

gcc -Wall -Wextra `pkg-config --cflags opencv` `pkg-config --libs opencv` -o videoHandler videoHandler.c

次に、表示された警告を修正します(または、質問を編集して、わからない場合は質問を追加します)。

2番目にデバッガーでプログラムを実行し、どの行がsegfaultをトリガーするかを確認します。それでもわからない場合は、それと関連する変数の値を (デバッグ出力を追加するか、デバッガーで調べることによって) 質問に追加してください。

3.それでも解決しない場合は、アプリケーションで valgrind を実行ます (Windows を使用している場合は、Linux VM をインストールしてその下で実行します。私は通常、VirtualBox + 利用可能な最新の Lubuntu 仮想ディスク イメージを使用します)。

実際には、この問題を解決したとしてもvalgrindを試して、どのような警告が表示されるかを確認し、それらのいずれかが実際にバグである場合は修正する必要があります (いくつかのライブラリで多くの場合でも、誤検知が発生する可能性があります)。

于 2013-04-04T19:30:41.307 に答える