Skip to content

Commit

Permalink
More sane filters + cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
zaak committed Dec 29, 2017
1 parent 248062a commit ac7161f
Show file tree
Hide file tree
Showing 16 changed files with 290 additions and 242 deletions.
6 changes: 4 additions & 2 deletions CamGrab.pro
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ SOURCES += \
glvideosurface.cpp \
glvideowidget.cpp \
filters/abstractfilter.cpp \
filters/facedetectfilter.cpp
filters/facedetectfilter.cpp \
filters/grayscalefilter.cpp

HEADERS += \
mainwindow.h \
Expand All @@ -34,7 +35,8 @@ HEADERS += \
glvideosurface.h \
glvideowidget.h \
filters/abstractfilter.h \
filters/facedetectfilter.h
filters/facedetectfilter.h \
filters/grayscalefilter.h

FORMS += \
mainwindow.ui
Expand Down
20 changes: 19 additions & 1 deletion filters/abstractfilter.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
#include "abstractfilter.h"

AbstractFilter::AbstractFilter(QObject *parent) : QObject(parent) {}
AbstractFilter::AbstractFilter(QObject *parent):
QObject(parent),
enabled(false)
{}

bool AbstractFilter::isEnabled()
{
return enabled;
}

void AbstractFilter::setEnabled(bool enable)
{
enabled = enable;
}

const QString &AbstractFilter::getName()
{
return name;
}
7 changes: 6 additions & 1 deletion filters/abstractfilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ class AbstractFilter : public QObject
public:
explicit AbstractFilter(QObject *parent = nullptr);
virtual void apply(cv::Mat &mat) = 0;
bool isEnabled();
const QString &getName();

signals:
protected:
QString name;
bool enabled;

public slots:
void setEnabled(bool enable);
};

#endif // ABSTRACTFILTER_H
85 changes: 23 additions & 62 deletions filters/facedetectfilter.cpp
Original file line number Diff line number Diff line change
@@ -1,71 +1,32 @@
#include "facedetectfilter.h"

using namespace std;
using namespace cv;

FaceDetectFilter::FaceDetectFilter(QObject *parent): AbstractFilter(parent)
{
face_cascade_gpu = cv::cuda::CascadeClassifier::create("/media/zaak/Data/dev/CamGrab/opencv/data/haarcascades_cuda/haarcascade_frontalface_alt2.xml");
name = "Face detect";
faceCascadeFilterGPU = cv::cuda::CascadeClassifier::create("/media/zaak/Data/dev/CamGrab/opencv/data/haarcascades_cuda/haarcascade_frontalface_alt2.xml");
}

void FaceDetectFilter::apply(cv::Mat &mat)
{
std::vector<Rect> faces;
Mat frame_gray;
Mat crop;
Mat res;
Mat gray;
string text;
stringstream sstm;

cvtColor(mat, frame_gray, COLOR_BGR2GRAY);
equalizeHist(frame_gray, frame_gray);

cv::cuda::GpuMat frame_gpu(frame_gray);
cv::cuda::GpuMat objbuf_gpu;

// Detect faces
face_cascade_gpu->detectMultiScale(frame_gpu, objbuf_gpu);
face_cascade_gpu->convert(objbuf_gpu, faces);

// Set Region of Interest
cv::Rect roi_b;
cv::Rect roi_c;

size_t ic = 0; // ic is index of current element
int ac = 0; // ac is area of current element

size_t ib = 0; // ib is index of biggest element
int ab = 0; // ab is area of biggest element

for (ic = 0; ic < faces.size(); ic++) // Iterate through all current elements (detected faces)

{
roi_c.x = faces[ic].x;
roi_c.y = faces[ic].y;
roi_c.width = (faces[ic].width);
roi_c.height = (faces[ic].height);

ac = roi_c.width * roi_c.height; // Get the area of current element (detected face)

roi_b.x = faces[ib].x;
roi_b.y = faces[ib].y;
roi_b.width = (faces[ib].width);
roi_b.height = (faces[ib].height);

ab = roi_b.width * roi_b.height; // Get the area of biggest element, at beginning it is same as "current" element

if (ac > ab)
{
ib = ic;
roi_b.x = faces[ib].x;
roi_b.y = faces[ib].y;
roi_b.width = (faces[ib].width);
roi_b.height = (faces[ib].height);
}

Point pt1(faces[ic].x, faces[ic].y); // Display detected faces on main window - live stream from camera
Point pt2((faces[ic].x + faces[ic].height), (faces[ic].y + faces[ic].width));
cv::rectangle(mat, pt1, pt2, Scalar(0, 255, 0), 2, 8, 0);
}
std::vector<cv::Rect> faces;
cv::Mat grayedFrame;
cv::Mat crop;
cv::Mat res;
cv::Mat gray;

cv::cvtColor(mat, grayedFrame, cv::COLOR_BGR2GRAY);
cv::equalizeHist(grayedFrame, grayedFrame);

cv::cuda::GpuMat frameGPU(grayedFrame);
cv::cuda::GpuMat objectBufferGPU;

faceCascadeFilterGPU->detectMultiScale(frameGPU, objectBufferGPU);
faceCascadeFilterGPU->convert(objectBufferGPU, faces);

for (cv::Rect &face : faces)
{
cv::Point pt1(face.x, face.y);
cv::Point pt2(face.x + face.height, face.y + face.width);
cv::rectangle(mat, pt1, pt2, cv::Scalar(0, 255, 0), 2, 8, 0);
}
}
6 changes: 1 addition & 5 deletions filters/facedetectfilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@
#define FACEDETECTFILTER_H

#include "abstractfilter.h"
#include <opencv2/objdetect.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/cudaobjdetect.hpp>
#include <opencv2/cudaimgproc.hpp>
#include <opencv2/cudawarping.hpp>

class FaceDetectFilter : public AbstractFilter
{
Expand All @@ -16,7 +12,7 @@ class FaceDetectFilter : public AbstractFilter
void apply(cv::Mat &mat);
private:

cv::Ptr<cv::cuda::CascadeClassifier> face_cascade_gpu;
cv::Ptr<cv::cuda::CascadeClassifier> faceCascadeFilterGPU;
};

#endif // FACEDETECTFILTER_H
14 changes: 14 additions & 0 deletions filters/grayscalefilter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "grayscalefilter.h"

GrayscaleFilter::GrayscaleFilter(QObject *parent): AbstractFilter(parent)
{
name = "Grayscale";
}

void GrayscaleFilter::apply(cv::Mat &mat)
{
cv::Mat grayed;

cv::cvtColor(mat, grayed, cv::COLOR_BGR2GRAY);
cv::cvtColor(grayed, mat, cv::COLOR_GRAY2BGR);
}
14 changes: 14 additions & 0 deletions filters/grayscalefilter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef GRAYSCALEFILTER_H
#define GRAYSCALEFILTER_H

#include <opencv2/imgproc.hpp>
#include "abstractfilter.h"

class GrayscaleFilter : public AbstractFilter
{
public:
explicit GrayscaleFilter(QObject *parent = nullptr);
void apply(cv::Mat &mat);
};

#endif // GRAYSCALEFILTER_H
4 changes: 4 additions & 0 deletions glvideosurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ bool GLVideoSurface::present(const QVideoFrame &frame)
QVideoFrame bufferFrame(frame);
bufferFrame.map(QAbstractVideoBuffer::ReadOnly);

if (!bufferFrame.isValid()) {
return false;
}

cv::Mat mat = cv::Mat(bufferFrame.height(), bufferFrame.width(), CV_8UC3, bufferFrame.bits());

emit frameReceived(mat);
Expand Down
26 changes: 21 additions & 5 deletions glvideowidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ GLVideoWidget::GLVideoWidget(QWidget *parent):
QOpenGLWidget(parent),
videoSurface(new GLVideoSurface(this))
{

connect(videoSurface, SIGNAL(frameReceived(cv::Mat&)), this, SLOT(renderFrame(cv::Mat&)));
connect(videoSurface, SIGNAL(presentationStopped()), this, SLOT(cleanup()));

//qDebug() << "Haar loaded: " << face_cascade_gpu;
filters.append(QSharedPointer<AbstractFilter>(new FaceDetectFilter()));
// registerFilter(QSharedPointer<AbstractFilter>(new FaceDetectFilter()));
}

GLVideoSurface *GLVideoWidget::getVideoSurface()
Expand Down Expand Up @@ -89,8 +87,11 @@ void GLVideoWidget::showImage(const QImage& image)

void GLVideoWidget::renderFrame(cv::Mat &mat)
{
foreach (const QSharedPointer<AbstractFilter> &filter, filters) {
filter.data()->apply(mat);
foreach (const QSharedPointer<AbstractFilter> &filterPtr, filters) {
AbstractFilter *filter = filterPtr.data();
if (filter->isEnabled()) {
filter->apply(mat);
}
}

cv::cvtColor(mat, opencvFrame, CV_BGR2RGBA);
Expand All @@ -107,3 +108,18 @@ QImage &GLVideoWidget::getRenderedImage()
{
return renderedImage;
}

void GLVideoWidget::registerFilter(QSharedPointer<AbstractFilter> filter)
{
filters.append(filter);
}

void GLVideoWidget::disableFilters()
{
foreach (const QSharedPointer<AbstractFilter> &filterPtr, filters) {
AbstractFilter *filter = filterPtr.data();
if (filter->isEnabled()) {
filter->setEnabled(false);
}
}
}
108 changes: 0 additions & 108 deletions glvideowidget.cpp.autosave

This file was deleted.

3 changes: 3 additions & 0 deletions glvideowidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <QOpenGLTexture>
#include "glvideosurface.h"
#include "filters/facedetectfilter.h"
#include "filters/grayscalefilter.h"

class GLVideoWidget : public QOpenGLWidget
{
Expand All @@ -19,6 +20,7 @@ class GLVideoWidget : public QOpenGLWidget
explicit GLVideoWidget(QWidget *parent = nullptr);
GLVideoSurface *getVideoSurface();
QImage &getRenderedImage();
void registerFilter(QSharedPointer<AbstractFilter> filter);

protected:
void initializeGL();
Expand All @@ -42,6 +44,7 @@ private slots:
public slots:
void showImage(const QImage& image);
void renderFrame(cv::Mat &frame);
void disableFilters();
};

#endif // GLVIDEOWIDGET_H
Loading

0 comments on commit ac7161f

Please sign in to comment.