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

View File

@@ -1,24 +1,25 @@
#pragma once #pragma once
#include"opencv2\opencv.hpp"
#include<opencv\cv.h> #include <opencv2/imgproc\types_c.h>
/* /*
Author: Pierfrancesco Soffritti https://github.com/PierfrancescoSoffritti Author: Pierfrancesco Soffritti https://github.com/PierfrancescoSoffritti
*/ */
namespace computervision
{
using namespace cv; using namespace cv;
using namespace std; using namespace std;
class BackgroundRemover { class BackgroundRemover {
public: public:
BackgroundRemover(void); BackgroundRemover(void);
void calibrate(Mat input); void calibrate(Mat input);
Mat BackgroundRemover::getForeground(Mat input); Mat getForeground(Mat input);
private: private:
Mat background; Mat background;
bool calibrated = false; bool calibrated = false;
Mat getForegroundMask(Mat input); 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 "FaceDetector.h"
#include"opencv2\opencv.hpp"
/* /*
Author: Pierfrancesco Soffritti https://github.com/PierfrancescoSoffritti 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"; FaceDetector::FaceDetector(void) {
CascadeClassifier faceCascadeClassifier;
FaceDetector::FaceDetector(void) {
if (!faceCascadeClassifier.load(faceClassifierFileName)) if (!faceCascadeClassifier.load(faceClassifierFileName))
throw runtime_error("can't load file " + 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; vector<Rect> faces;
Mat frameGray; Mat frameGray;
@@ -33,9 +34,9 @@ void FaceDetector::removeFaces(Mat input, Mat output) {
-1 -1
); );
} }
} }
Rect getFaceRect(Mat input) { Rect getFaceRect(Mat input) {
vector<Rect> faceRectangles; vector<Rect> faceRectangles;
Mat inputGray; Mat inputGray;
@@ -48,4 +49,5 @@ Rect getFaceRect(Mat input) {
return faceRectangles[0]; return faceRectangles[0];
else else
return Rect(0, 0, 1, 1); return Rect(0, 0, 1, 1);
}
} }

View File

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

View File

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

View File

@@ -1,20 +1,23 @@
#pragma once #pragma once
#include "opencv/cv.h" #include "opencv2/core.hpp"
#include <opencv2/imgproc/types_c.h>
/* /*
Author: Nicol<6F> Castellazzi https://github.com/nicast Author: Nicol<6F> Castellazzi https://github.com/nicast
*/ */
using namespace cv; namespace computervision
using namespace std; {
using namespace cv;
using namespace std;
class FingerCount { class FingerCount {
public: public:
FingerCount(void); FingerCount(void);
Mat findFingersCount(Mat input_image, Mat frame); Mat findFingersCount(Mat input_image, Mat frame);
private: private:
Scalar color_blue; Scalar color_blue;
Scalar color_green; Scalar color_green;
Scalar color_red; Scalar color_red;
@@ -29,4 +32,5 @@ private:
vector<Point> findClosestOnX(vector<Point> points, Point pivot); vector<Point> findClosestOnX(vector<Point> points, Point pivot);
double findPointsDistanceOnX(Point a, Point b); double findPointsDistanceOnX(Point a, Point b);
void drawVectorPoints(Mat image, vector<Point> points, Scalar color, bool with_numbers); 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 "ObjectDetection.h" #include "ObjectDetection.h"
//#include "BackgroundRemover.h" #include "BackgroundRemover.h"
#include "SkinDetector.h" #include "SkinDetector.h"
#include "FaceDetector.h" #include "FaceDetector.h"
#include "FingerCount.h" #include "FingerCount.h"
@@ -8,10 +15,11 @@
namespace computervision namespace computervision
{ {
cv::VideoCapture cap(0); cv::VideoCapture cap(0);
cv::Mat img, imgGray, img2, img2Gray, img3, img4; cv::Mat img, imgGray, img2, img2Gray, img3, img4;
Mat frame, frameOut, handMask, foreground, fingerCountDebug; Mat frame, frameOut, handMask, foreground, fingerCountDebug;
//BackgroundRemover backgroundRemover; BackgroundRemover backgroundRemover;
SkinDetector skinDetector; SkinDetector skinDetector;
FaceDetector faceDetector; FaceDetector faceDetector;
FingerCount fingerCount; 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); cap.read(frame);
frameOut = frame.clone();
skinDetector.drawSkinColorSampler(frameOut); skinDetector.drawSkinColorSampler(frameOut);
foreground = backgroundRemover.getForeground(frame); foreground = backgroundRemover.getForeground(frame);
faceDetector.removeFaces(frame, foreground); faceDetector.removeFaces(frame, foreground);
handMask = skinDetector.getSkinMask(foreground); handMask = skinDetector.getSkinMask(foreground);
return true;
} }
void ObjectDetection::readWebcam() void ObjectDetection::readWebcam()

View File

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

View File

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

View File

@@ -1,23 +1,27 @@
#pragma once #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 Author: Pierfrancesco Soffritti https://github.com/PierfrancescoSoffritti
*/ */
using namespace cv; namespace computervision
using namespace std; {
using namespace cv;
using namespace std;
class SkinDetector { class SkinDetector {
public: public:
SkinDetector(void); SkinDetector(void);
void drawSkinColorSampler(Mat input); void drawSkinColorSampler(Mat input);
void calibrate(Mat input); void calibrate(Mat input);
Mat getSkinMask(Mat input); Mat getSkinMask(Mat input);
private: private:
int hLowThreshold = 0; int hLowThreshold = 0;
int hHighThreshold = 0; int hHighThreshold = 0;
int sLowThreshold = 0; int sLowThreshold = 0;
@@ -30,5 +34,6 @@ private:
Rect skinColorSamplerRectangle1, skinColorSamplerRectangle2; Rect skinColorSamplerRectangle1, skinColorSamplerRectangle2;
void calculateThresholds(Mat sample1, Mat sample2); 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> <ItemGroup>
<ClInclude Include="src\computervision\FaceDetector.h" /> <ClInclude Include="src\computervision\FaceDetector.h" />
<ClInclude Include="src\computervision\FingerCount.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\SkinDetector.h" />
<ClInclude Include="src\computervision\ObjectDetection.h" /> <ClInclude Include="src\computervision\ObjectDetection.h" />
<ClInclude Include="src\entities\camera.h" /> <ClInclude Include="src\entities\camera.h" />

View File

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