Ask Your Question
0

video frame is different frome imgs?

asked 2013-03-17 00:07:51 -0600

wdxz6547 gravatar image

updated 2013-03-17 23:34:41 -0600

int main(int argc, char** argv)
{
    if (argc != 2) {
    help(argv);
    return 1;
    }
    std::string arg = argv[1];
    VideoCapture capture(arg); 
    if (!capture.isOpened()){ 
    capture.open(atoi(arg.c_str()));
    }
    if (!capture.isOpened()) {
    cerr << "Failed to open a video device or video file!\n" << endl;
    help(argv);
    return 1;
    }

    char filename[200];
    Mat frame1;
    Mat frame2;
    Mat frame3;
    Mat frame4;

    capture >> frame1;
    sprintf(filename,"frame%.3d.jpg",1);
    imwrite(filename,frame1);
    frame3 = imread(filename);

    capture.set(CV_CAP_PROP_POS_FRAMES, 100);

    capture >> frame2;
    sprintf(filename,"frame%.3d.jpg",2);
    imwrite(filename,frame2);
    frame4 = imread(filename);

    cout << (double)cmpHistOfHSV(frame1, frame2) << endl;
    cout << (double)partOfCmpHist(frame1, frame2) << endl;

    cout << cmpHistOfHSV(frame3, frame4) << endl;
    cout << partOfCmpHist(frame3, frame4) << endl;
    return 0;
}

the output:

0
0
4074.85
6790.25

why, the video frame is different the img?

Appendix:

double cmpHistOfHSV(Mat src_base1, Mat src_base2)
{
    const int HL_RANGE = 0;
    const int HR_RANGE = 256;
    const int SL_RANGE = 0;
    const int SR_RANGE = 180;

    const int H_BINS = 12;//50
    const int S_BINS = 6;//60

    Mat hsv_base1, hsv_base2;

    // Convert to HSV
    cvtColor(src_base1, hsv_base1, CV_BGR2HSV);
    cvtColor(src_base2, hsv_base2, CV_BGR2HSV);


    //30 bins for Hue, 32 for Saturation
    int h_bins = H_BINS;
    int s_bins = S_BINS;
    int histSize[] = {h_bins, s_bins};

    //hue varies from 0 to 256, saturation from 0 to 180
    float h_ranges[] = {HL_RANGE, HR_RANGE};
    float s_ranges[] = {SL_RANGE, SR_RANGE};
    const float *ranges[] = {h_ranges, s_ranges};

    //Use the 0th and 1th channels
    int channels[] = {0, 1};

    //Histogram
    MatND hist_base1;
    MatND hist_base2;

    //Calculate the histogram for HSV images
    calcHist(&hsv_base1, 1, channels, Mat(), hist_base1, 2, histSize, ranges, true, false);
    normalize(hist_base1, hist_base1, 0, 1, NORM_MINMAX, -1, Mat());

    calcHist(&hsv_base2, 1, channels, Mat(), hist_base2, 2, histSize, ranges, true, false);
    normalize(hist_base2, hist_base2, 0, 1, NORM_MINMAX, -1, Mat());

    double base_base = compareHist(hist_base1, hist_base2, 1);
    return base_base;
}
edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted
0

answered 2013-03-17 03:53:06 -0600

  1. I do not find cmpHistOfHSV and partOfCmpHist functions in OpenCV. What do they do?
  2. Jpeg is lossy image format. It means that frame1 differs from frame3.
edit flag offensive delete link more

Comments

yeah, it is written by myself which is used for compare two images or frames. what's the different between Jpeg and frame?

wdxz6547 gravatar imagewdxz6547 ( 2013-03-17 20:57:20 -0600 )edit
0

answered 2013-03-18 02:55:52 -0600

updated 2013-03-18 02:57:51 -0600

In OpenCV 1.X, it is not allowed to modify the returned image buffer from capture. According to the doc, it seems not relevant with OpenCV 2.X, but usually, I made deep copy of images returned by VideoCapture object, did you try to copy these images (frame 1&2)? Maybe they return 0 because, the pointers are pointing to the same (internal) image? Try it and let us know.

edit flag offensive delete link more

Comments

the docs are wrong there !

it does not matter, if you're using the 1.0 or the 2.0 api, a frame from a camera points to Driver memory, so you need a "deep copy" if

  1. you try to manipulate the image ( draw into it, or such).

  2. it goes out of scope

  3. you're trying to store it in a vector or such

berak gravatar imageberak ( 2013-03-18 14:15:50 -0600 )edit

Hey, I wanted to ask something regarding what you said. If I have a VideoCapture class capturing video frames, I can't store those frames in a vector directly? How come?

Uni gravatar imageUni ( 2013-08-01 15:18:25 -0600 )edit

Question Tools

Stats

Asked: 2013-03-17 00:07:51 -0600

Seen: 310 times

Last updated: Mar 18 '13