简体   繁体   中英

Transparent Image Overlay over Real-time OpenCV Camera

I already have my codes for body detection and it works fine. I want to finish it with a transparent image (a shirt for example) overlays on a real-time opencv camera when a body is detected. I used haar cascade classifier for detection, by the way.

Here are my c++ codes for the human body detection:

nerds_thesis_clartips_OpencvClass.cpp

#include "nerds_thesis_clartips_OpencvClass.h"

JNIEXPORT void JNICALL Java_nerds_thesis_clartips_OpencvClass_humanDetection
  (JNIEnv *, jclass, jlong addrRgba){
    Mat& frame = *(Mat*)addrRgba;

    detectHuman(frame);
    }

  void detectHuman(Mat& frame){
    String human_cascade_name = "/storage/emulated/0/data/haarcascade_upperbody.xml";
    CascadeClassifier human_cascade;

    if(!human_cascade.load( human_cascade_name ) ) { printf("--(!)Error loading\n"); return; };

    std::vector<Rect> humans;
    Mat frame_gray;

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

    //-- Detect Human
    human_cascade.detectMultiScale( frame_gray, humans, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );

    for (int i=0; i<humans.size(); i++)
        rectangle(frame, Point(humans[i].x, humans[i].y), Point(humans[i].x+humans[i].width, humans[i].y+humans[i].height), Scalar(0,255,0));

   }

And here are my codes in my h file:

nerds_thesis_clartips_OpencvClass.h

#include <jni.h>
#include <opencv2/opencv.hpp>
/* Header for class nerds_thesis_clartips_OpencvClass */

using namespace cv;

#ifndef _Included_nerds_thesis_clartips_OpencvClass
#define _Included_nerds_thesis_clartips_OpencvClass
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     nerds_thesis_clartips_OpencvClass
 * Method:    humanDetection
 * Signature: (J)V
 */

 void detectHuman(Mat& frame);

JNIEXPORT void JNICALL Java_nerds_thesis_clartips_OpencvClass_humanDetection
  (JNIEnv *, jclass, jlong);

#ifdef __cplusplus
}
#endif
#endif

I'm still noob in this field and this will serves as my final output in college.

You have to use alpha bending for this purpose. In order to construct a transparent overlay, you need two images:

  • Your original image.
  • An image containing what you want to “overlay” on top of the first using some level of alpha transparency.

Here is an example for transparent overlay image but it is in python
https://www.pyimagesearch.com/2016/03/07/transparent-overlays-with-opencv/

https://pytech-solution.blogspot.in/2017/07/alphablending.html

Below is a implementation for aplha bending in c++ but to make the overlay image transparent you have to see the logic in first link above. https://www.learnopencv.com/alpha-blending-using-opencv-cpp-python/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM