我已经有了用于 body 检测的代码,并且工作正常。当检测到 body 时,我想用实时 opencv 相机上覆盖的透明图像(例如一件衬衫)来完成它。顺便说一句,我使用 haar 级联分类器进行检测。
这是我用于人体检测的 C++ 代码:
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));
}
这是我的 h 文件中的代码:
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
我在这个领域还是菜鸟,这将作为我在大学的最终成果。
最佳答案
为此目的,您必须使用 alpha 弯曲。 为了构建透明叠加层,您需要两个图像:
- 您的原始图像。
- 包含您想要“叠加”在第一个图像之上的图像 使用一定程度的 Alpha 透明度。
这是一个透明覆盖图像的示例,但它是在 python 中
https://www.pyimagesearch.com/2016/03/07/transparent-overlays-with-opencv/
https://pytech-solution.blogspot.in/2017/07/alphablending.html
下面是 C++ 中 aplha 弯曲的实现,但要使覆盖图像透明,您必须查看上面第一个链接中的逻辑。 https://www.learnopencv.com/alpha-blending-using-opencv-cpp-python/
关于java - 实时 OpenCV 相机上的透明图像叠加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48841196/