[EDIT] improve hand detection with mask
This commit is contained in:
@@ -15,6 +15,9 @@ namespace computervision
|
||||
|
||||
cv::Mat img, imgGray, img2, img2Gray, img3, img4;
|
||||
|
||||
int handMaskStartXPos, handMaskStartYPos, handMaskWidth, handMaskHeight;
|
||||
bool handMaskGenerated = false;
|
||||
|
||||
Mat frame, frameOut, handMask, foreground, fingerCountDebug;
|
||||
BackgroundRemover backgroundRemover;
|
||||
SkinDetector skinDetector;
|
||||
@@ -22,6 +25,8 @@ namespace computervision
|
||||
FingerCount fingerCount;
|
||||
|
||||
|
||||
|
||||
|
||||
ObjectDetection::ObjectDetection()
|
||||
{
|
||||
}
|
||||
@@ -67,6 +72,36 @@ namespace computervision
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ObjectDetection::detectHand(Mat inputFrame)
|
||||
{
|
||||
frameOut = inputFrame.clone();
|
||||
|
||||
skinDetector.drawSkinColorSampler(frameOut);
|
||||
|
||||
foreground = backgroundRemover.getForeground(inputFrame);
|
||||
|
||||
//faceDetector.removeFaces(inputFrame, foreground);
|
||||
handMask = skinDetector.getSkinMask(foreground);
|
||||
fingerCountDebug = fingerCount.findFingersCount(handMask, frameOut);
|
||||
|
||||
//backgroundRemover.calibrate(frame);
|
||||
|
||||
|
||||
imshow("output", frameOut);
|
||||
imshow("foreground", foreground);
|
||||
imshow("handMask", handMask);
|
||||
imshow("handDetection", fingerCountDebug);
|
||||
|
||||
int key = waitKey(1);
|
||||
|
||||
if (key == 98) // b
|
||||
backgroundRemover.calibrate(inputFrame);
|
||||
else if (key == 115) // s
|
||||
skinDetector.calibrate(inputFrame);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ObjectDetection::calculateDifference()
|
||||
{
|
||||
cap.read(img);
|
||||
@@ -84,22 +119,31 @@ namespace computervision
|
||||
|
||||
cv::Mat ObjectDetection::generateHandMaskSquare(cv::Mat img)
|
||||
{
|
||||
handMaskStartXPos = 20;
|
||||
handMaskStartYPos = img.rows / 5;
|
||||
handMaskWidth = img.cols / 3;
|
||||
handMaskHeight = img.cols / 3;
|
||||
|
||||
|
||||
cv::Mat mask = cv::Mat::zeros(img.size(), img.type());
|
||||
cv::Mat dstImg = cv::Mat::zeros(img.size(), img.type());
|
||||
|
||||
cv::rectangle(mask, Rect(0, img.rows * 0.2, img.cols / 3, img.cols / 3), Scalar(255, 255, 255), -1);
|
||||
//cv::circle(mask, cv::Point(mask.cols / 2, mask.rows / 2), 50, cv::Scalar(255, 0, 0), -1, 8, 0);
|
||||
|
||||
cv::rectangle(mask, Rect(handMaskStartXPos, handMaskStartYPos, handMaskWidth, handMaskHeight), Scalar(255, 255, 255), -1);
|
||||
|
||||
img.copyTo(dstImg, mask);
|
||||
|
||||
rectangle(img, Rect(0, img.rows * 0.2, img.cols / 3, img.cols / 3), Scalar(0, 255, 255, 255));
|
||||
|
||||
handMaskGenerated = true;
|
||||
return dstImg;
|
||||
|
||||
}
|
||||
|
||||
bool ObjectDetection::drawHandMaskRect(cv::Mat* input)
|
||||
{
|
||||
if (!handMaskGenerated) return false;
|
||||
rectangle(*input, Rect(handMaskStartXPos, handMaskStartYPos, handMaskWidth, handMaskHeight), Scalar(255, 255, 255));
|
||||
return true;
|
||||
}
|
||||
|
||||
void ObjectDetection::detect()
|
||||
{
|
||||
int key = waitKey(1);
|
||||
|
||||
@@ -61,6 +61,21 @@ namespace computervision
|
||||
*/
|
||||
cv::Mat readCamera();
|
||||
|
||||
/**
|
||||
* @brief detects a hand based on the given hand mask input frame.
|
||||
*
|
||||
* @param inputFrame the input frame with only the hand
|
||||
* @return true if the webcam is connected, false if not.
|
||||
*/
|
||||
bool detectHand(cv::Mat inputFrame);
|
||||
|
||||
/**
|
||||
* @brief draws the hand mask rectangle on the given input matrix.
|
||||
*
|
||||
* @param input the input matrix to draw the rectangle on
|
||||
*/
|
||||
bool drawHandMaskRect(cv::Mat *input);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ namespace computervision
|
||||
void SkinDetector::drawSkinColorSampler(Mat input) {
|
||||
int frameWidth = input.size().width, frameHeight = input.size().height;
|
||||
|
||||
int rectangleSize = 20;
|
||||
int rectangleSize = 25;
|
||||
Scalar rectangleColor = Scalar(255, 0, 255);
|
||||
|
||||
skinColorSamplerRectangle1 = Rect(frameWidth / 5, frameHeight / 2, rectangleSize, rectangleSize);
|
||||
|
||||
10
src/main.cpp
10
src/main.cpp
@@ -4,6 +4,7 @@
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "stb_image.h"
|
||||
#include <ostream>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <opencv2/core.hpp>
|
||||
#include <opencv2/videoio.hpp>
|
||||
@@ -67,7 +68,7 @@ int main(void)
|
||||
|
||||
// set up object detection
|
||||
//objDetect.setup();
|
||||
|
||||
cv::Mat cameraFrame;
|
||||
|
||||
// Main game loop
|
||||
while (!glfwWindowShouldClose(window))
|
||||
@@ -82,9 +83,14 @@ int main(void)
|
||||
shader.Start();
|
||||
shader.LoadViewMatrix(camera);
|
||||
|
||||
|
||||
render_engine::renderer::Render(entity, shader);
|
||||
|
||||
objDetect.generateHandMaskSquare(objDetect.readCamera());
|
||||
cameraFrame = objDetect.readCamera();
|
||||
objDetect.detectHand(objDetect.generateHandMaskSquare(cameraFrame));
|
||||
objDetect.drawHandMaskRect(&cameraFrame);
|
||||
cv::imshow("camera",cameraFrame);
|
||||
|
||||
|
||||
// Finish up
|
||||
shader.Stop();
|
||||
|
||||
Reference in New Issue
Block a user