0

一般来说,我对 OpenCV 和 c++ 编程还是很陌生。我正在做一个从我的网络摄像头存储图像的项目。我能够以某种方式显示相机图像并检测人脸,但我不知道如何保存图像。

我应该怎么做才能捕获我的网络摄像头检测到的面孔并将其存储到我的计算机中?

使用下面的代码,我如何将其编辑为:

  1. 在检测到面部 5 秒后从实时摄像头捕获图像
  2. 将图像保存到 jpg 格式的文件夹中

非常感谢您提供的任何帮助!

我的代码:

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/objdetect.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    // capture from web camera init

    VideoCapture cap(0);
    cap.open(0);

    Mat img;

    // Initialize the inbuilt Harr Cascade frontal face detection
    // Below mention the path of where your haarcascade_frontalface_alt2.xml     file is located

    CascadeClassifier face_cascade;
    face_cascade.load("C:/OpenCV/sources/data/haarcascades/haarcascade_frontalface_alt2.xml");
    // i tried changing this line to match my folder in C Drive

    for (;;)
    {

        // Image from camera to Mat

        cap >> img;

        // Just resize input image if you want
        resize(img, img, Size(1000, 640));

        // Container of faces
        vector<Rect> faces;

        // Detect faces
        face_cascade.detectMultiScale(img, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(140, 140));
        // error message appears here

        //Show the results
        // Draw circles on the detected faces
        for (int i = 0; i < faces.size(); i++)
        {
            Point center(faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5);
            ellipse(img, center, Size(faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);
        }

        // To draw rectangles around detected faces 
        /* for (unsigned i = 0; i<faces.size(); i++)
        rectangle(img,faces[i], Scalar(255, 0, 0), 2, 1);*/

        imshow("wooohooo", img);
        int key2 = waitKey(20);

    }

    return 0;
}
4

1 回答 1

0

回答这个问题,我们可以结束这个问题

用于在 C++ opencv 中保存图像的函数是cv::imwrite('image_name.jpg', img). cv::imshow()如果您想在每次检测后保存图像,请在您之前或之后添加它。

于 2019-02-12T16:57:08.983 回答