我一直在研究一个通过背景减去来检测手的程序。 我试图将相机的第一帧保存为背景并减去当前帧,但它们似乎以某种方式具有不同的亮度。 我已经试了好几次了,但我没有换灯,可能是什么问题? image1 image2
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include "Camera.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/background_segm.hpp>
using namespace cv;
using namespace std;
int main() {
const int LAPTOP_CAM = 0;
const int LIFECAM = 1;
const int MAX_FPS = 25;
Camera cam(LIFECAM);
Mat backGround;
cam.TakeShot();
cam.MirrorImage();
cam.getFrame().copyTo(backGround); //Deep Copy
imshow("Background", backGround);
Mat diff;
while (true) {
cam.TakeShot();
cam.MirrorImage();
absdiff(cam.getFrame(),backGround , diff);
imshow("Result", cam.getFrame());
imshow("Diff", diff);
cam.Set_FPS(MAX_FPS);
if (waitKey(1) == 27)
break;
}
}
这里是“Camera.cpp”:
#include "Camera.h"
Camera::Camera()
{
}
Camera::~Camera()
{
}
Camera::Camera(int camNum)
{
VideoCapture cap(camNum);
_capture = cap;
}
void Camera::TakeShot()
{
_capture >> _frame;
}
void Camera::Set_FPS(int fps)
{
if (fps > 25)
fps = 25;
else if (fps < 1)
fps = 1;
_capture.set(CV_CAP_PROP_FPS, fps);
}
Mat Camera::getFrame()
{
return _frame;
}
void Camera::MirrorImage()
{
flip(_frame, _frame, 1);
}
最佳答案
应该始终有细微的光照变化,绝对减法几乎行不通。如果您必须使用绝对不同,您可能需要阈值 diff
。
但是,您应该研究一些背景减法算法。 OpenCV有一些内置方法:MOG2、KNN。
此外,您还可以使用皮肤(颜色)检测来检测手。
关于c++ - 使用 Absdiff 的 OpenCV 背景减法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35061734/