[EDIT] added evrything to namespace, also fixed includes

This commit is contained in:
Jasper
2021-05-21 12:12:42 +02:00
parent e39cb1a761
commit 27a09aeca4
12 changed files with 493 additions and 454 deletions

View File

@@ -1,21 +1,21 @@
#include "BackgroundRemover.h"
#include"opencv2\opencv.hpp"
/*
Author: Pierfrancesco Soffritti https://github.com/PierfrancescoSoffritti
*/
BackgroundRemover::BackgroundRemover(void) {
namespace computervision
{
BackgroundRemover::BackgroundRemover(void) {
background;
calibrated = false;
}
}
void BackgroundRemover::calibrate(Mat input) {
void BackgroundRemover::calibrate(Mat input) {
cvtColor(input, background, CV_BGR2GRAY);
calibrated = true;
}
}
Mat BackgroundRemover::getForeground(Mat input) {
Mat BackgroundRemover::getForeground(Mat input) {
Mat foregroundMask = getForegroundMask(input);
//imshow("foregroundMask", foregroundMask);
@@ -24,9 +24,9 @@ Mat BackgroundRemover::getForeground(Mat input) {
input.copyTo(foreground, foregroundMask);
return foreground;
}
}
Mat BackgroundRemover::getForegroundMask(Mat input) {
Mat BackgroundRemover::getForegroundMask(Mat input) {
Mat foregroundMask;
if (!calibrated) {
@@ -39,9 +39,9 @@ Mat BackgroundRemover::getForegroundMask(Mat input) {
removeBackground(foregroundMask, background);
return foregroundMask;
}
}
void BackgroundRemover::removeBackground(Mat input, Mat background) {
void BackgroundRemover::removeBackground(Mat input, Mat background) {
int thresholdOffset = 10;
for (int i = 0; i < input.rows; i++) {
@@ -55,4 +55,5 @@ void BackgroundRemover::removeBackground(Mat input, Mat background) {
input.at<uchar>(i, j) = 255;
}
}
}
}

View File

@@ -1,24 +1,25 @@
#pragma once
#include<opencv\cv.h>
#include"opencv2\opencv.hpp"
#include <opencv2/imgproc\types_c.h>
/*
Author: Pierfrancesco Soffritti https://github.com/PierfrancescoSoffritti
*/
namespace computervision
{
using namespace cv;
using namespace std;
class BackgroundRemover {
public:
class BackgroundRemover {
public:
BackgroundRemover(void);
void calibrate(Mat input);
Mat BackgroundRemover::getForeground(Mat input);
Mat getForeground(Mat input);
private:
private:
Mat background;
bool calibrated = false;
Mat getForegroundMask(Mat input);
void BackgroundRemover::removeBackground(Mat input, Mat background);
};
void removeBackground(Mat input, Mat background);
};
}

View File

@@ -1,21 +1,22 @@
#include "FaceDetector.h"
#include"opencv2\opencv.hpp"
/*
Author: Pierfrancesco Soffritti https://github.com/PierfrancescoSoffritti
*/
namespace computervision
{
Rect getFaceRect(Mat input);
Rect getFaceRect(Mat input);
String faceClassifierFileName = "../res/haarcascade_frontalface_alt.xml";
CascadeClassifier faceCascadeClassifier;
String faceClassifierFileName = "../res/haarcascade_frontalface_alt.xml";
CascadeClassifier faceCascadeClassifier;
FaceDetector::FaceDetector(void) {
FaceDetector::FaceDetector(void) {
if (!faceCascadeClassifier.load(faceClassifierFileName))
throw runtime_error("can't load file " + faceClassifierFileName);
}
}
void FaceDetector::removeFaces(Mat input, Mat output) {
void FaceDetector::removeFaces(Mat input, Mat output) {
vector<Rect> faces;
Mat frameGray;
@@ -33,9 +34,9 @@ void FaceDetector::removeFaces(Mat input, Mat output) {
-1
);
}
}
}
Rect getFaceRect(Mat input) {
Rect getFaceRect(Mat input) {
vector<Rect> faceRectangles;
Mat inputGray;
@@ -48,4 +49,5 @@ Rect getFaceRect(Mat input) {
return faceRectangles[0];
else
return Rect(0, 0, 1, 1);
}
}

View File

@@ -1,7 +1,7 @@
#pragma once
#include<opencv\cv.h>
#include"opencv2\opencv.hpp"
#include <opencv2\imgproc\types_c.h>
#include <opencv2/objdetect/objdetect_c.h>
/*
Author: Pierfrancesco Soffritti https://github.com/PierfrancescoSoffritti
*/
@@ -9,8 +9,11 @@
using namespace cv;
using namespace std;
class FaceDetector {
public:
namespace computervision
{
class FaceDetector {
public:
FaceDetector(void);
void removeFaces(Mat input, Mat output);
};
};
}

View File

@@ -12,7 +12,9 @@
#define BOUNDING_RECT_FINGER_SIZE_SCALING 0.3
#define BOUNDING_RECT_NEIGHBOR_DISTANCE_SCALING 0.05
FingerCount::FingerCount(void) {
namespace computervision
{
FingerCount::FingerCount(void) {
color_blue = Scalar(255, 0, 0);
color_green = Scalar(0, 255, 0);
color_red = Scalar(0, 0, 255);
@@ -20,9 +22,9 @@ FingerCount::FingerCount(void) {
color_white = Scalar(255, 255, 255);
color_yellow = Scalar(0, 255, 255);
color_purple = Scalar(255, 0, 255);
}
}
Mat FingerCount::findFingersCount(Mat input_image, Mat frame) {
Mat FingerCount::findFingersCount(Mat input_image, Mat frame) {
Mat contours_image = Mat::zeros(input_image.size(), CV_8UC3);
// check if the source image is good
@@ -150,14 +152,14 @@ Mat FingerCount::findFingersCount(Mat input_image, Mat frame) {
putText(frame, to_string(filtered_finger_points.size()), center_bounding_rect, FONT_HERSHEY_PLAIN, 3, color_purple);
return contours_image;
}
}
double FingerCount::findPointsDistance(Point a, Point b) {
double FingerCount::findPointsDistance(Point a, Point b) {
Point difference = a - b;
return sqrt(difference.ddot(difference));
}
}
vector<Point> FingerCount::compactOnNeighborhoodMedian(vector<Point> points, double max_neighbor_distance) {
vector<Point> FingerCount::compactOnNeighborhoodMedian(vector<Point> points, double max_neighbor_distance) {
vector<Point> median_points;
if (points.size() == 0)
@@ -188,16 +190,16 @@ vector<Point> FingerCount::compactOnNeighborhoodMedian(vector<Point> points, dou
median_points.push_back(median);
return median_points;
}
}
double FingerCount::findAngle(Point a, Point b, Point c) {
double FingerCount::findAngle(Point a, Point b, Point c) {
double ab = findPointsDistance(a, b);
double bc = findPointsDistance(b, c);
double ac = findPointsDistance(a, c);
return acos((ab * ab + bc * bc - ac * ac) / (2 * ab * bc)) * 180 / CV_PI;
}
}
bool FingerCount::isFinger(Point a, Point b, Point c, double limit_angle_inf, double limit_angle_sup, Point palm_center, double min_distance_from_palm) {
bool FingerCount::isFinger(Point a, Point b, Point c, double limit_angle_inf, double limit_angle_sup, Point palm_center, double min_distance_from_palm) {
double angle = findAngle(a, b, c);
if (angle > limit_angle_sup || angle < limit_angle_inf)
return false;
@@ -225,9 +227,9 @@ bool FingerCount::isFinger(Point a, Point b, Point c, double limit_angle_inf, do
return false;
return true;
}
}
vector<Point> FingerCount::findClosestOnX(vector<Point> points, Point pivot) {
vector<Point> FingerCount::findClosestOnX(vector<Point> points, Point pivot) {
vector<Point> to_return(2);
if (points.size() == 0)
@@ -266,9 +268,9 @@ vector<Point> FingerCount::findClosestOnX(vector<Point> points, Point pivot) {
to_return[1] = points[index_found];
return to_return;
}
}
double FingerCount::findPointsDistanceOnX(Point a, Point b) {
double FingerCount::findPointsDistanceOnX(Point a, Point b) {
double to_return = 0.0;
if (a.x > b.x)
@@ -277,12 +279,13 @@ double FingerCount::findPointsDistanceOnX(Point a, Point b) {
to_return = b.x - a.x;
return to_return;
}
}
void FingerCount::drawVectorPoints(Mat image, vector<Point> points, Scalar color, bool with_numbers) {
void FingerCount::drawVectorPoints(Mat image, vector<Point> points, Scalar color, bool with_numbers) {
for (int i = 0; i < points.size(); i++) {
circle(image, points[i], 5, color, 2, 8);
if (with_numbers)
putText(image, to_string(i), points[i], FONT_HERSHEY_PLAIN, 3, color);
}
}
}

View File

@@ -1,20 +1,23 @@
#pragma once
#include "opencv/cv.h"
#include "opencv2/core.hpp"
#include <opencv2/imgproc/types_c.h>
/*
Author: Nicol<6F> Castellazzi https://github.com/nicast
*/
using namespace cv;
using namespace std;
namespace computervision
{
using namespace cv;
using namespace std;
class FingerCount {
public:
class FingerCount {
public:
FingerCount(void);
Mat findFingersCount(Mat input_image, Mat frame);
private:
private:
Scalar color_blue;
Scalar color_green;
Scalar color_red;
@@ -29,4 +32,5 @@ private:
vector<Point> findClosestOnX(vector<Point> points, Point pivot);
double findPointsDistanceOnX(Point a, Point b);
void drawVectorPoints(Mat image, vector<Point> points, Scalar color, bool with_numbers);
};
};
}

View File

@@ -1,6 +1,13 @@
#include "opencv2/opencv.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include <opencv2/highgui.hpp>
#include <opencv2/video.hpp>
#include "ObjectDetection.h"
#include "ObjectDetection.h"
//#include "BackgroundRemover.h"
#include "BackgroundRemover.h"
#include "SkinDetector.h"
#include "FaceDetector.h"
#include "FingerCount.h"
@@ -8,10 +15,11 @@
namespace computervision
{
cv::VideoCapture cap(0);
cv::Mat img, imgGray, img2, img2Gray, img3, img4;
Mat frame, frameOut, handMask, foreground, fingerCountDebug;
//BackgroundRemover backgroundRemover;
BackgroundRemover backgroundRemover;
SkinDetector skinDetector;
FaceDetector faceDetector;
FingerCount fingerCount;
@@ -21,15 +29,24 @@ namespace computervision
{
}
void ObjectDetection::Init()
bool ObjectDetection::Init()
{
if (!cap.isOpened()) {
cout << "Can't find camera!" << endl;
return false;
}
cap.read(frame);
frameOut = frame.clone();
skinDetector.drawSkinColorSampler(frameOut);
foreground = backgroundRemover.getForeground(frame);
faceDetector.removeFaces(frame, foreground);
handMask = skinDetector.getSkinMask(foreground);
return true;
}
void ObjectDetection::readWebcam()

View File

@@ -17,6 +17,7 @@ namespace computervision
public:
ObjectDetection();
bool Init();
void readWebcam();
void showWebcam();
void calculateDifference();

View File

@@ -1,11 +1,12 @@
#include "SkinDetector.h"
#include"opencv2\opencv.hpp"
/*
Author: Pierfrancesco Soffritti https://github.com/PierfrancescoSoffritti
*/
SkinDetector::SkinDetector(void) {
namespace computervision
{
SkinDetector::SkinDetector(void) {
hLowThreshold = 0;
hHighThreshold = 0;
sLowThreshold = 0;
@@ -16,9 +17,9 @@ SkinDetector::SkinDetector(void) {
calibrated = false;
skinColorSamplerRectangle1, skinColorSamplerRectangle2;
}
}
void SkinDetector::drawSkinColorSampler(Mat input) {
void SkinDetector::drawSkinColorSampler(Mat input) {
int frameWidth = input.size().width, frameHeight = input.size().height;
int rectangleSize = 20;
@@ -38,9 +39,9 @@ void SkinDetector::drawSkinColorSampler(Mat input) {
skinColorSamplerRectangle2,
rectangleColor
);
}
}
void SkinDetector::calibrate(Mat input) {
void SkinDetector::calibrate(Mat input) {
Mat hsvInput;
cvtColor(input, hsvInput, CV_BGR2HSV);
@@ -51,9 +52,9 @@ void SkinDetector::calibrate(Mat input) {
calculateThresholds(sample1, sample2);
calibrated = true;
}
}
void SkinDetector::calculateThresholds(Mat sample1, Mat sample2) {
void SkinDetector::calculateThresholds(Mat sample1, Mat sample2) {
int offsetLowThreshold = 80;
int offsetHighThreshold = 30;
@@ -72,9 +73,9 @@ void SkinDetector::calculateThresholds(Mat sample1, Mat sample2) {
vHighThreshold = max(hsvMeansSample1[2], hsvMeansSample2[2]) + offsetHighThreshold;
//vLowThreshold = 0;
//vHighThreshold = 255;
}
}
Mat SkinDetector::getSkinMask(Mat input) {
Mat SkinDetector::getSkinMask(Mat input) {
Mat skinMask;
if (!calibrated) {
@@ -95,9 +96,10 @@ Mat SkinDetector::getSkinMask(Mat input) {
dilate(skinMask, skinMask, Mat(), Point(-1, -1), 3);
return skinMask;
}
}
void SkinDetector::performOpening(Mat binaryImage, int kernelShape, Point kernelSize) {
void SkinDetector::performOpening(Mat binaryImage, int kernelShape, Point kernelSize) {
Mat structuringElement = getStructuringElement(kernelShape, kernelSize);
morphologyEx(binaryImage, binaryImage, MORPH_OPEN, structuringElement);
}
}

View File

@@ -1,23 +1,27 @@
#pragma once
#include<opencv\cv.h>
#include<opencv2\core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgproc/types_c.h>
/*
Author: Pierfrancesco Soffritti https://github.com/PierfrancescoSoffritti
*/
using namespace cv;
using namespace std;
namespace computervision
{
using namespace cv;
using namespace std;
class SkinDetector {
public:
class SkinDetector {
public:
SkinDetector(void);
void drawSkinColorSampler(Mat input);
void calibrate(Mat input);
Mat getSkinMask(Mat input);
private:
private:
int hLowThreshold = 0;
int hHighThreshold = 0;
int sLowThreshold = 0;
@@ -30,5 +34,6 @@ private:
Rect skinColorSamplerRectangle1, skinColorSamplerRectangle2;
void calculateThresholds(Mat sample1, Mat sample2);
void SkinDetector::performOpening(Mat binaryImage, int structuralElementShapde, Point structuralElementSize);
};
void performOpening(Mat binaryImage, int structuralElementShapde, Point structuralElementSize);
};
}

View File

@@ -37,7 +37,7 @@
<ItemGroup>
<ClInclude Include="src\computervision\FaceDetector.h" />
<ClInclude Include="src\computervision\FingerCount.h" />
<ClInclude Include="src\computervision\Header.h" />
<ClInclude Include="src\computervision\BackgroundRemover.h" />
<ClInclude Include="src\computervision\SkinDetector.h" />
<ClInclude Include="src\computervision\ObjectDetection.h" />
<ClInclude Include="src\entities\camera.h" />

View File

@@ -101,7 +101,7 @@
<ClInclude Include="src\computervision\FaceDetector.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\computervision\Header.h">
<ClInclude Include="src\computervision\BackgroundRemover.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>