Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions include/detection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,27 @@
#include <string>

#include "opencv2/core/core.hpp"
#include "opencv2/opencv.hpp"

class Detector {
public:
static std::shared_ptr<Detector> CreateDetector(const std::string& name);
virtual bool Init(const std::string& model_file_path) = 0;
virtual void Detect(const cv::Mat& frame, std::vector<cv::Rect>& objects,
std::vector<double>& scores) = 0;
public:
static std::shared_ptr<Detector> CreateDetector(const std::string& name);
virtual bool Init(const std::string& model_file_path) = 0;
virtual void Detect(const cv::Mat& frame, std::vector<cv::Rect>& objects,
std::vector<double>& scores) = 0;
};

class CascadeDetector : Detector {
public:
static std::shared_ptr<CascadeDetector> CreateDetector(const std::string& name)
{
if (name == "cascade") {
return std::make_shared<CascadeDetector>();
}
};
virtual bool Init(const std::string& model_file_path);
virtual void Detect(const cv::Mat& frame, std::vector<cv::Rect>& objects,
std::vector<double>& scores);
protected:
cv::CascadeClassifier detector;
};
13 changes: 13 additions & 0 deletions include/image_processing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <string>

#include "opencv2/core/core.hpp"
#include "opencv2/imgproc.hpp"

class ImageProcessor {
public:
Expand All @@ -15,4 +16,16 @@ class ImageProcessor {
const int kernelSize) = 0;
virtual cv::Mat Pixelize(const cv::Mat &src, const cv::Rect &roi,
const int kDivs) = 0;
};

class MyImageProcessor {
public:
virtual cv::Mat CvtColor(const cv::Mat &src, const cv::Rect &roi);
virtual cv::Mat Filter(const cv::Mat &src, const cv::Rect &roi,
const int kSize);
virtual cv::Mat DetectEdges(const cv::Mat &src, const cv::Rect &roi,
const int filterSize, const int lowThreshold, const int ratio,
const int kernelSize);
virtual cv::Mat Pixelize(const cv::Mat &src, const cv::Rect &roi,
const int kDivs);
};
17 changes: 17 additions & 0 deletions include/tracking.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <memory>
#include <string>

#include "opencv2/opencv.hpp"
#include "opencv2/core/core.hpp"

class Tracker {
Expand All @@ -11,3 +12,19 @@ class Tracker {
virtual bool Init(const cv::Mat &frame, const cv::Rect &roi) = 0;
virtual cv::Rect Track(const cv::Mat &frame) = 0;
};

class MedianFlowTracker : Tracker {
public:
static std::shared_ptr<MedianFlowTracker> CreateTracker(const std::string &name)
{
if (name == "median_flow") {
return std::make_shared<MedianFlowTracker>();
}
};
virtual bool Init(const cv::Mat &frame, const cv::Rect &roi);
virtual cv::Rect Track(const cv::Mat &frame);

protected:
cv::Rect position_;
cv::Mat frame_;
};
1 change: 1 addition & 0 deletions include/workaround.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ class MatrixProcessor {
public:
void Threshold(unsigned char* const data, const int width, const int height,
const int threshold);
void Average(unsigned char* const data, const int width, const int height);
};
125 changes: 125 additions & 0 deletions samples/detection_demo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#include <iostream>
#include <string>

#include "opencv2/core.hpp"
#include "detection.hpp"

using namespace std;
using namespace cv;

const char* kAbout =
"This is an empty application that can be treated as a template for your "
"own doing-something-cool applications.";

const char* kOptions =
"{ i image | <none> | image to process }"
"{ v video | <none> | video to process }"
"{ c camera | <none> | camera to get video from }"
"{ m model | <none> | }"
"{ h ? help usage | | print help message }";


int getMode(CommandLineParser& parser)
{
if (parser.has("i"))
return 0;
if (parser.has("v"))
return 1;
if (parser.has("c"))
return 3;
}

void ImageProc(shared_ptr<CascadeDetector>& detector, Mat& src)
{
vector<Rect> objects;
vector<double> scores;

detector->Detect(src, objects, scores);

for each (Rect itm in objects)
{
rectangle(src, itm, Scalar(0, 255, 255));
}
}

int imageDetect(CommandLineParser& parser, shared_ptr<CascadeDetector>& detector)
{
Mat src = imread(parser.get<string>("i"), CV_LOAD_IMAGE_COLOR);
if (src.empty()) {
cout << "Failed to open image file '" + parser.get<string>(0) + "'."
<< endl;
return 0;
}

ImageProc(detector, src);

const string kSrcWindowName = "Detection image";
const int kWaitKeyDelay = 1;
namedWindow(kSrcWindowName);
imshow(kSrcWindowName, src);
waitKey(0);
}

int videoDetect(VideoCapture& cap, CommandLineParser& parser, shared_ptr<CascadeDetector>& detector)
{
if (!cap.isOpened()) {
cout << "Fail." << endl;
return -1;
}

for (;;)
{
Mat img;
cap >> img; if (img.empty()) break;

ImageProc(detector, img);

imshow("Detection video", img);
waitKey(25);
}
waitKey(0);
}


int main(int argc, const char** argv) {
// Parse command line arguments.
CommandLineParser parser(argc, argv, kOptions);
parser.about(kAbout);

// If help option is given, print help message and exit.
if (parser.has("help")) {
parser.printMessage();
return 0;
}

string pathToDetector = parser.get<string>("model");
shared_ptr<CascadeDetector> detector = CascadeDetector::CreateDetector("cascade");
bool flag = detector->Init(pathToDetector);

if (flag == false) {
cout << "Failfail." << endl;
return -1;
}

int mode = getMode(parser);

if (mode == 0)
imageDetect(parser, detector);

if (mode == 1)
{
string src = parser.get<string>("v");
VideoCapture cap;
cap.open(src);
videoDetect(cap, parser, detector);
}

if (mode == 3)
{
VideoCapture cap(0);
videoDetect(cap, parser, detector);
}


return 0;
}
9 changes: 5 additions & 4 deletions samples/devtools_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ int main(int argc, const char** argv) {
MatrixProcessor processor;
const int threshold = parser.get<int>("t");
try {
processor.Threshold(src.data, src.cols, src.rows, threshold);
} catch (const std::exception& ex) {
cout << ex.what() << endl;
return 0;
processor.Threshold(src.data, src.cols, src.rows, threshold);
}
catch (const std::exception& ex) {
cout << ex.what() << endl;
return 0;
}

// Show destination image.
Expand Down
65 changes: 65 additions & 0 deletions samples/devtools_demo_kud.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <iostream>
#include <string>

#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"

#include "workaround.hpp"

using namespace std;
using namespace cv;

const char* kAbout = "Application for practice #1.";

const char* kOptions =
"{ @image | | image to process }"
"{ t | 128 | threshold }"
"{ h ? help usage | | print help message }";

int main(int argc, const char** argv) {
// Parse command line arguments.
CommandLineParser parser(argc, argv, kOptions);
parser.about(kAbout);

// If help option is given, print help message and exit.
if (parser.has("help")) {
parser.printMessage();
return 0;
}

// Read image.
Mat src = imread(parser.get<string>(0), CV_LOAD_IMAGE_GRAYSCALE);
if (src.empty()) {
cout << "Failed to open image file '" + parser.get<string>(0) + "'."
<< endl;
return 0;
}

// Show source image.
const string kSrcWindowName = "Source image";
const int kWaitKeyDelay = 1;
namedWindow(kSrcWindowName, WINDOW_NORMAL);
resizeWindow(kSrcWindowName, 640, 480);
imshow(kSrcWindowName, src);
waitKey(kWaitKeyDelay);

// Average data.
MatrixProcessor processor;
const int threshold = parser.get<int>("t");
try {
processor.Average(src.data, src.cols, src.rows);
}
catch (const std::exception& ex) {
cout << ex.what() << endl;
return 0;
}

// Show destination image.
const string kDstWindowName = "Destination image";
namedWindow(kDstWindowName, WINDOW_NORMAL);
resizeWindow(kDstWindowName, 640, 480);
imshow(kDstWindowName, src);
waitKey();

return 0;
}
Loading