C++ 将图片插入实时摄像机

C++ 将图片插入实时摄像机,c++,opencv,C++,Opencv,我有一个问题,我如何能插入一张照片到一个实时摄像头(特别是我有一个人脸检测代码,在检测到人脸后,我希望它在我的脸上画一个圆圈,并在脸上显示一张图像)。如有任何意见,将不胜感激 以下是我迄今为止编写的代码: #include <cv.h> #include <highgui.h> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <o

我有一个问题,我如何能插入一张照片到一个实时摄像头(特别是我有一个人脸检测代码,在检测到人脸后,我希望它在我的脸上画一个圆圈,并在脸上显示一张图像)。如有任何意见,将不胜感激

以下是我迄今为止编写的代码:

#include <cv.h>
#include <highgui.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/videoio/videoio.hpp>
#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;

// These are global variables
char face_cascade_name[] = "c:\\Program Files\\opencv\\build\\etc\\haarcascades\\haarcascade_frontalface_alt.xml";
char eyes_cascade_name[] = "c:\\Program Files\\opencv\\build\\etc\\haarcascades\\haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;


// This is function detectAndDisplay()
// The input, or parameter, is a Mat object.
// It does not return a value, thus the return type "void".

void detectAndDisplay( Mat frame ) {
    std::vector<Rect> faces;
    Mat frame_gray;

    cvtColor( frame, frame_gray, COLOR_BGR2GRAY );
    equalizeHist( frame_gray, frame_gray );  // increase the image contrast
    //-- Detect faces
    face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CASCADE_SCALE_IMAGE, Size(30, 30) );

    // For each face in the frame. faces[i] is a Rect, so it has a
    //     top-left corner (faces[i].x,faces[i].y)
    //     and dimensions faces[i].width x faces[i].height
    for( size_t i = 0; i < faces.size(); i++ ) {
        Point center( faces[i].x + faces[i].width/2, faces[i].y + faces[i].height/2 );

        ellipse( frame, center, Size( faces[i].width/2, faces[i].height/2), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 )

        Mat faceROI = frame_gray( faces[i] );  // image of the face



    }
}


int main( void ) {
    VideoCapture cap(0);
    Mat frame;
    int frameCount;
    int fr=1,i=1;
      char name[20],s[20];
      Mat image;
      while(fr<=751)
      {
      sprintf(name,"Hello.jpg",i);

      image = imread(name,CV_LOAD_IMAGE_COLOR);   // Read the file

      if(! image.data )                              // Check for invalid input
      {
      cout <<  "Could not open or find the image" << std::endl ;
      return -1;
      }
      sprintf(s,"pic/img%u.jpg",i);
      imwrite(s,image);
      fr++;
      i++;
      }
    namedWindow("Faces",CV_WINDOW_FULLSCREEN);

    // 1. Load the cascades
    if( !face_cascade.load( face_cascade_name ) ) {
        printf("--(!)Error loading face cascade\n");
        return -1;
    }
    if( !eyes_cascade.load( eyes_cascade_name ) ){
        printf("--(!)Error loading eyes cascade\n");
        return -1;
    }
    // 2. Read the video stream
    if (!cap.isOpened() ) {
        printf("--(!)Error opening video capture\n");
        return -1;
    }


    for (frameCount = 0; frameCount < 1000000000; frameCount++) {

        cap >> frame;
        detectAndDisplay( frame );
        image.copyTo( frame );
        namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
        imshow( "Display window", image );
        imshow( "Faces",frame );
        int c = waitKey(10);
        if ((char)c == 27 ) {   // if the "Escape" key is pressed
            break;
        }
    }
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括“opencv2/objdetect.hpp”
#包括“opencv2/highgui.hpp”
#包括“opencv2/imgproc.hpp”
#包括
#包括
使用名称空间std;
使用名称空间cv;
//这些是全局变量
char face_cascade_name[]=“c:\\Program Files\\opencv\\build\\etc\\haarcascade\\haarcascade\u frontalface\u alt.xml”;
char eyes\u cascade\u name[]=“c:\\Program Files\\opencv\\build\\etc\\haarcascade\\haarcascade\u eye\u tree\u eyegories.xml”;
层叠式分级机;
级联效应;
//这是函数detectAndDisplay()
//输入或参数是Mat对象。
//它不返回值,因此返回类型为“void”。
空隙检测和显示(垫架){
向量面;
垫子框架为灰色;
CVT颜色(框架、框架灰、颜色灰);
equalizeHist(frame_gray,frame_gray);//增加图像对比度
//--检测人脸
人脸层叠。检测多尺度(帧灰度,人脸,1.1,2,0;层叠图像,大小(30,30));
//对于frame.faces[i]中的每个面都是一个矩形,因此它有一个
//左上角(面[i].x,面[i].y)
//和尺寸面[i]。宽度x面[i]。高度
对于(size_t i=0;i虽然(fr您可以用循环图像的像素替换边界矩形内的像素,也可以使用ecopyTo函数复制帧内的图像。在此之前,请使用resize函数将图像调整到边界框。

以下代码片段演示如何在另一个im上绘制透明度为的图像年龄。对于此示例,我使用了两幅图像:

  • a(jpg格式)
  • 具有透明度的a(png格式)

#包括
#包括
使用名称空间cv;
使用名称空间std;
int main(int argc,字符**argv)
{
垫面;
face=imread(“face.jpg”,1);//我们要在其上应用图像的面部。这当然可以由相机的边框代替。
垫胡子;
mustache=imread(“CurlyMustache.png”,imread_未更改);//我们将应用于面部顶部的图像。imread_未更改允许加载带有透明度通道的图像。
尺寸=尺寸(118,50);
调整大小(小胡子,小胡子,大小,0.0,0.0,INTER_区域);//将小胡子调整为所需大小。INTER_区域插值可提供良好的结果。
点2i画_位置(160275);//画胡子的位置
//把胡子和脸混在一起
对于(int y=0;y
结果如下:


这个解决方案不是很有效(因为我们必须在胡子图像的所有像素上循环)但是,由于OpenCV与透明度的结合不太好,我认为这是一个不错的选择。希望它能有所帮助!

到目前为止你做了什么?发布一些代码。如果你已经有了人脸检测代码,那么在检测到的人脸上显示一张图像应该是相当容易的。这是我的代码,我已经复制了一张图像并粘贴到t中他是一个框架,但它只是覆盖了我所有的框架。请看我的答案。它应该可以让你开始。如果它没有,也许我可以进一步改进它?好的,我有你的ans,它很有用。你能帮我放文本(从文件扫描)把它放在SRCN上,这样我就想了。谢谢你在屏幕上添加文字,我用这个函数更新了我的代码片段。我想我回答了你的问题,所以请考虑接受它。谢谢你,但是我仍然不知道如何把它放在我的相机里的特定区域,它只是覆盖了所有的帧。到目前为止,这就是我所拥有的。
#include <stdio.h>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    Mat face;
    face = imread("face.jpg", 1); // the face we want to apply an image on. This can of course be replaced by a camera's frame.

    Mat mustache;
    mustache = imread("CurlyMustache.png", IMREAD_UNCHANGED); // the image we will apply on top of the face. IMREAD_UNCHANGED allows to load an image with its transparency channel.

    Size size = Size(118, 50);
    resize(mustache, mustache, size, 0.0,0.0, INTER_AREA); // resize the mustache to the wanted size. INTER_AREA interpolation gives good results.

    Point2i draw_position(160, 275); // the position where to draw the mustache

    // blend the mustache with the face
    for (int y = 0; y < mustache.rows; ++y)
    {
        for (int x = 0; x < mustache.cols; ++x)
        {
            cv::Vec4b & pixel = mustache.at<cv::Vec4b>(y, x); // mustache's pixel
            cv::Vec3b & pixel_dst = face.at<cv::Vec3b>(y + draw_position.y - size.height / 2, x + draw_position.x - size.width / 2); // destination image's (face) pixel

            pixel_dst = (pixel[3] / 255.0f) * Vec3b(pixel[0], pixel[1], pixel[2]) + (1 - pixel[3] / 255.0f) * pixel_dst; // we blend the two pixels according to the transparency of the mustache's pixel.
        }
    }

    putText(face, "your text here", Point(draw_position.x - 20, draw_position.y - 50), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0), 2); // adds text on screen

    namedWindow("Display Image", WINDOW_AUTOSIZE);
    imshow("Display Image", face);

    waitKey(0);

    return 0;
}