opencv播放视频速度慢

时间:2015-11-17 16:18:31

标签: c++ opencv

我的视频速度有问题,当我在opencv中运行视频文件时,它会以正常速度播放。

当我将面部和眼睛检测应用于播放速度非常慢的视频时,问题就开始了。我试图更改waitKey()值,但问题仍然存在。

为什么会这样?

#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

// Function Headers
void detectAndDisplay(Mat frame);

// Global variables
// Copy this file from opencv/data/haarscascades to target folder
string face_cascade_name = "haarcascade_frontalface_alt.xml";
string eye_cascade_name = "haarcascade_mcs_eyepair_big.xml";
CascadeClassifier face_cascade;
CascadeClassifier eye_cascade;
string window_name = "Capture - Face detection";
int filenumber; // Number of file to be saved
string filename;


int main(void)
{
    VideoCapture capture("m.mp4");

    if (!capture.isOpened())  // check if we succeeded
    return -1;

    // Load the cascade
    if (!face_cascade.load(face_cascade_name))
    {
        printf("--(!)Error loading\n");
        return (-1);
    }

    if (!eye_cascade.load(eye_cascade_name))
    {
        printf("--(!)Error loading\n eye  ");
        return (-1);
    }

    // Read the video stream
    Mat frame;

    for (;;)
    {
        capture >> frame;

        // Apply the classifier to the frame
        if (!frame.empty())
        {
            detectAndDisplay(frame);
        }
        else
        {
            printf(" --(!) No captured frame -- Break!");
            break;
        }

        waitKey(40);
        return 0;
    }
}

void detectAndDisplay(Mat frame)
{
    std::vector<Rect> faces;
    std::vector<Rect> eyes;
    Mat frame_gray;
    Mat crop;
    Mat res;
    Mat gray;
    string text;
    stringstream sstm;

    cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
    equalizeHist(frame_gray, frame_gray);

    // Detect faces
    face_cascade.detectMultiScale(frame_gray, faces, 1.1, 4, CASCADE_SCALE_IMAGE, Size(20, 20));

    // Set Region of Interest
    size_t i = 0; // ic is index of current element

    for (i = 0; i < faces.size(); i++) // Iterate through all current elements (detected faces)
    {
        Point pt1(faces[i].x, faces[i].y); // Display detected faces on main window - live stream from camera
        Point pt2((faces[i].x + faces[i].height), (faces[i].y + faces[i].width));

        rectangle(frame, pt1, pt2, Scalar(0, 255, 0), 2, 8, 0);

        // set ROI for the eyes
        Rect Roi = faces[i];

        Roi.height = Roi.height / 4;
        Roi.y = Roi.y + Roi.height;

        cv::Mat crop = frame(Roi);

        cvtColor(crop, gray, CV_BGR2GRAY);
        imshow("ROI", gray);

        eye_cascade.detectMultiScale(gray, eyes, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(20, 20));

        for (size_t j = 0; j < eyes.size(); j++)
        {
            Rect r(Roi.x + eyes[j].x, Roi.y + eyes[j].y, eyes[j].width, eyes[j].height);

            rectangle(frame, r, Scalar(255, 0, 0), 3, 1, 0);

            // convert roi to ychannel
            Mat eye_region = frame(eyes[j]).clone();
            Mat eye_region_YCbCr;

            cvtColor(eye_region, eye_region_YCbCr, CV_BGR2YCrCb);

            vector<Mat> channels;

            split(eye_region_YCbCr, channels);

            Mat eye_region_Ychannel = channels[0].clone();

            imshow("Y", eye_region_Ychannel);
            // end of convert
        }
    }

    // Show image
    imshow("original", frame);

    if (!crop.empty())
    {
        imshow("detected", crop);
    }
    else
    {
        destroyWindow("detected");
    }
}

0 个答案:

没有答案