转换网络摄像头程序以处理一张图像

时间:2018-07-20 15:21:46

标签: c++ opencv

我目前正在尝试修改一个以摄像头流为输入的程序。问题是,当我尝试更改程序以使用单个图像时,它不会显示我期望的输出,例如视频流(下面的代码)

#include "opencv2/opencv.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include <opencv2/highgui.hpp>
#include <opencv2/video.hpp>

#include "BackgroundRemover.h"
#include "SkinDetector.h"
#include "FaceDetector.h"
#include "FingerCount.h"

using namespace cv;
using namespace std;

int main(int, char**) {
    VideoCapture videoCapture(0);
    videoCapture.set(CV_CAP_PROP_SETTINGS, 1);

    if (!videoCapture.isOpened()) {
        cout << "Can't find camera!" << endl;
        return -1;
    }

    Mat frame, frameOut, handMask, foreground, fingerCountDebug;

    BackgroundRemover backgroundRemover;
    SkinDetector skinDetector;
    FaceDetector faceDetector;
    FingerCount fingerCount;

    for (int i = 0; i < 2; i++)
    {
        videoCapture >> frame;
        frameOut = frame.clone();

        skinDetector.drawSkinColorSampler(frameOut);

        foreground = backgroundRemover.getForeground(frame);

        faceDetector.removeFaces(frame, foreground);
        handMask = skinDetector.getSkinMask(foreground);
        fingerCountDebug = fingerCount.findFingersCount(handMask, frameOut);

        imshow("output", frameOut);
        imshow("foreground", foreground);
        imshow("handMask", handMask);
        imshow("handDetection", fingerCountDebug);

        if (i == 0)
        {
            backgroundRemover.calibrate(frame);
            skinDetector.calibrate(frame);
        }
    }
    waitKey(0);
}

输出显示一个检测。而如果我修改代码以使帧不会从视频流中读取,则输出完全不显示任何内容。有人可以帮助解决此问题吗?编辑:由于一些社区成员的困惑,修改后的代码在下面读取了一张图片:

#include "opencv2/opencv.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include <opencv2/highgui.hpp>
#include <opencv2/video.hpp>

#include "BackgroundRemover.h"
#include "SkinDetector.h"
#include "FaceDetector.h"
#include "FingerCount.h"

using namespace cv;
using namespace std;

int main(int, char**) {

    string imageName("C:/Users/whoever/Desktop/hand_test.jpg"); // by default
    Mat image;
    image = imread(imageName.c_str(), IMREAD_COLOR); // Read the file


    Mat frame, frameOut, handMask, foreground, fingerCountDebug;

    BackgroundRemover backgroundRemover;
    SkinDetector skinDetector;
    FaceDetector faceDetector;
    FingerCount fingerCount;

    for (int i = 0; i < 2; i++)
    {
        frame = image;
        frameOut = frame.clone();

        skinDetector.drawSkinColorSampler(frameOut);

        foreground = backgroundRemover.getForeground(frame);

        faceDetector.removeFaces(frame, foreground);
        handMask = skinDetector.getSkinMask(foreground);
        fingerCountDebug = fingerCount.findFingersCount(handMask, frameOut);

        imshow("output", frameOut);
        imshow("foreground", foreground);
        imshow("handMask", handMask);
        imshow("handDetection", fingerCountDebug);

        if (i == 0)
        {
            cout << "Calibrating...";
            backgroundRemover.calibrate(frame);
            skinDetector.calibrate(frame);
        }
    }
    waitKey(0);
}

1 个答案:

答案 0 :(得分:2)

每次绕过循环,原始代码都会处理从相机捕获的不同图像,并输出差异。由于您现在每次都使用相同的图像,因此不会有任何差异,因此输出完全为空白。 (请注意,它仍将作为视频播放输出,只是一个不断空白的内容)

for循环的第一行是它从相机获取新图像的地方:

   videoCapture >> frame;

正如您在更新的代码中看到的那样,您正在删除此代码,并再次使用同一张图片:

    frame = image;

请尝试保存2张不同的图像,并在每次循环时将程序加载到不同的图像中。

这是一种蛮力的方式,您可以改善每次循环时加载不同文件,使用数组等的方式:

string imageName1("C:/Users/whoever/Desktop/hand_test_1.jpg"); // by default
string imageName2("C:/Users/whoever/Desktop/hand_test_2.jpg"); // by default
Mat image1;
Mat image2;
image1 = imread(imageName1.c_str(), IMREAD_COLOR); // Read the file
image2 = imread(imageName2.c_str(), IMREAD_COLOR); // Read the file

Mat frame, frameOut, handMask, foreground, fingerCountDebug;

BackgroundRemover backgroundRemover;
SkinDetector skinDetector;
FaceDetector faceDetector;
FingerCount fingerCount;

for (int i = 0; i < 2; i++)
{
    if (i = 0) { frame = image1 } else { frame = image2 };
...