Ask Your Question
0

How do I store the captured image in OpenCV (saving the picture to computer)

asked 2017-07-26 01:23:47 -0500

blink gravatar image

I am still quite new to OpenCV and c++ programming in general. I am doing on a project that stores image from my webcam. I was able to display camera image and detect faces somehow but I don't know how to save the images.

What should i do in order for the faces that my webcam detects gets captured and store into my computer?

Using my code below, how can i edit it to:

  1. capture an image from the live cam 5 seconds after detecting face
  2. save the images to a folder in jpg format

Thanks so much for any help you can provide!

my code:

#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;
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-07-27 06:48:12 -0500

Raki gravatar image

updated 2017-07-27 06:49:24 -0500

I suggest you take a look at the tutorial here regarding writing images on disk. The line which you probably need is the following:

 imwrite( "../where/you/want/it/to/be/saved/", img);

instead of the function that shows the image in a window via:

 imshow("wooohooo", img);

which you currently have.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-07-26 01:23:47 -0500

Seen: 4,022 times

Last updated: Jul 27 '17