Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
3408ff2
written binarisation implementation
ChumankinYuriy Jul 4, 2016
0734721
Created test application for image processing. Written implementation…
ChumankinYuriy Jul 5, 2016
6533db9
written CvtColor implementation
ChumankinYuriy Jul 5, 2016
8b83dc1
written Filter and DetectEdges implementation
ChumankinYuriy Jul 5, 2016
769c668
written Pixelize implementation
ChumankinYuriy Jul 5, 2016
caf6d41
Parced command line options
ChumankinYuriy Jul 5, 2016
a804100
Added callback
ChumankinYuriy Jul 5, 2016
21e2848
commit before pull
ChumankinYuriy Jul 6, 2016
02e0682
Merge branch 'master' of https://github.com/itseez-academy/itseez-ss-…
ChumankinYuriy Jul 6, 2016
cdbb6a1
draw rectangle
ChumankinYuriy Jul 6, 2016
d536737
written stubs for CascadeDetector
ChumankinYuriy Jul 6, 2016
7e7686e
Merge branch 'master' of https://github.com/itseez-academy/itseez-ss-…
ChumankinYuriy Jul 6, 2016
f5ec00a
Merge branch 'master' into practice-3
ChumankinYuriy Jul 6, 2016
f9c8db1
Written implementaton for CascadeDetector. Written implementation of …
ChumankinYuriy Jul 6, 2016
1a1d763
detection demo added to control
ChumankinYuriy Jul 6, 2016
c052826
Merge branch 'practice-2'
ChumankinYuriy Jul 6, 2016
324c994
Merge branch 'practice-3'
ChumankinYuriy Jul 6, 2016
235b655
simple median flow tracking realisation
ChumankinYuriy Jul 7, 2016
f0f1ec7
simple median flow tracking realisation
ChumankinYuriy Jul 7, 2016
bcf6def
written forward backward filtration
ChumankinYuriy Jul 7, 2016
6b186d7
written scaling
ChumankinYuriy Jul 7, 2016
1b7db11
bug fix in scale calculation, Detect refactoring
ChumankinYuriy Jul 7, 2016
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
11 changes: 11 additions & 0 deletions include/detection.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/objdetect.hpp"

class Detector {
public:
Expand All @@ -12,3 +13,13 @@ class Detector {
virtual void Detect(const cv::Mat& frame, std::vector<cv::Rect>& objects,
std::vector<double>& scores) = 0;
};

class CascadeDetector : public Detector {
public:
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;
};
16 changes: 15 additions & 1 deletion include/image_processing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#include <memory>
#include <string>

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

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

class ImageProcessorImpl {
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);
};
11 changes: 11 additions & 0 deletions include/tracking.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,14 @@ 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 : public Tracker {
public:
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_;
};

80 changes: 80 additions & 0 deletions samples/detection_demo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <iostream>
#include <string>

#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.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> | detector model }"
"{ h ? help usage | | print help message }";

void processImage(string & windowName, Mat & frame, CascadeDetector & detector) {
vector<Rect> objects;
vector<double> scores;
detector.Detect(frame, objects, scores);
Mat frame_copy = frame.clone();
for each (Rect object in objects)
{
rectangle(frame_copy, object, Scalar(255, 0, 0));
}
imshow(windowName, frame_copy);
waitKey(1);
}

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.get<bool>("help")) {
parser.printMessage();
return 0;
}

if (!parser.has("model")) {
cerr << "Specify detector model" << endl;
return -1;
}

string mfilepath = parser.get<string>("model");

CascadeDetector detector;
detector.CreateDetector("cascade");
if (!detector.Init(mfilepath)) {
cerr << "Can't load cascade!" << endl;
return -1;
}

string windowName("Detected objects");

if (parser.has("video")) {
string vfilepath = parser.get<string>("video");
VideoCapture cap(vfilepath);
Mat frame;
if(!cap.isOpened()) {
cerr << "Can't open video capture!" << parser.get<string>("video") << endl;
return -1;
}
cap >> frame;
while (frame.cols * frame.rows)
{
processImage(windowName, frame, detector);
cap >> frame;
}
}

return 0;
}
112 changes: 112 additions & 0 deletions samples/imgproc_demo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include <iostream>
#include <string>

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

using namespace std;
using namespace cv;

#include "image_processing.hpp"


struct MouseCallbackState {
bool is_selection_started;
bool is_selection_finished;
Point point_first;
Point point_second;

MouseCallbackState() : is_selection_started(false), is_selection_finished(false) {}
};

void CallBackFunc(int event, int x, int y, int flags, void* userdata)
{
MouseCallbackState * p_mc_state = (MouseCallbackState *)userdata;
if (event == EVENT_LBUTTONDOWN)
{
p_mc_state->point_first = Point(x, y);
p_mc_state->is_selection_started = true;
}
else if (event == EVENT_LBUTTONUP)
{
p_mc_state->is_selection_finished = true;
}
p_mc_state->point_second = Point(x, y);
}

const char* kAbout =
"This is image processing test application";

const char* kOptions =
"{ @image | <none> | image to process }"
"{ gray | | convert ROI to gray scale }"
"{ median | | apply median filter for ROI }"
"{ edges | | detect edges in ROI }"
"{ pix | | pixelize ROI }"
"{ 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));
if (src.empty()) {
cout << "Failed to open image file '" + parser.get<string>(0) + "'."
<< endl;
return 0;
}

//set the callback function for any mouse event
// Show source image.
const string kSrcWindowName = "Source image";
imshow(kSrcWindowName, src);
MouseCallbackState mc_state;
setMouseCallback(kSrcWindowName, CallBackFunc, (void *)&mc_state);

Rect roi;

while (!mc_state.is_selection_finished) {
if (mc_state.is_selection_started) {
Mat src_copy = src.clone();
roi = Rect(mc_state.point_first, mc_state.point_second);
rectangle(src_copy, roi, Scalar(255, 0, 0));
imshow(kSrcWindowName, src_copy);
}
waitKey(30);
}

ImageProcessorImpl proc;
Mat res = src.clone();
if (parser.has("gray")) {
res = proc.CvtColor(src, roi);
}
else if (parser.has("median")) {
res = proc.Filter(src, roi, 11);
}
else if (parser.has("edges")) {
int filterSize = 5;
int lowThreshold = 50;
int ratio = 3;
int kernelSize = 5;
res = proc.DetectEdges(src, roi,
filterSize, lowThreshold, ratio, kernelSize);
}
else if (parser.has("pix")) {
res = proc.Pixelize(src, roi, 5);
}

const string kResWindowName = "Processed image";
imshow(kResWindowName, res);
const int kWaitKeyDelay = 0;
waitKey(kWaitKeyDelay);

return 0;
}
99 changes: 99 additions & 0 deletions samples/tracking_demo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include <iostream>
#include <string>

#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "tracking.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 =
"{ v video | | video to process }"
"{ h ? help usage | | print help message }";

struct MouseCallbackState {
bool is_selection_started;
bool is_selection_finished;
Point point_first;
Point point_second;

MouseCallbackState() : is_selection_started(false), is_selection_finished(false) {}
};

void CallBackFunc(int event, int x, int y, int flags, void* userdata)
{
MouseCallbackState * p_mc_state = (MouseCallbackState *)userdata;
if (event == EVENT_LBUTTONDOWN)
{
p_mc_state->point_first = Point(x, y);
p_mc_state->is_selection_started = true;
}
else if (event == EVENT_LBUTTONUP)
{
p_mc_state->is_selection_finished = true;
}
p_mc_state->point_second = Point(x, y);
}

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.get<bool>("help")) {
parser.printMessage();
return 0;
}

if (parser.has("video")) {
string vfilepath = parser.get<string>("video");
VideoCapture cap(vfilepath);
Mat frame;
if (!cap.isOpened()) {
cerr << "Can't open video capture!" << parser.get<string>("video") << endl;
return -1;
}
cap >> frame;

const string kSrcWindowName = "Tracking object";
imshow(kSrcWindowName, frame);
MouseCallbackState mc_state;
setMouseCallback(kSrcWindowName, CallBackFunc, (void *)&mc_state);
Rect roi;

while (!mc_state.is_selection_finished) {
if (mc_state.is_selection_started) {
Mat frame_copy = frame.clone();
roi = Rect(mc_state.point_first, mc_state.point_second);
rectangle(frame_copy, roi, Scalar(255, 0, 0));
imshow(kSrcWindowName, frame_copy);
}
waitKey(30);
}

MedianFlowTracker tracker;
tracker.Init(frame, roi);
while (frame.cols * frame.rows)
{
Rect obj = tracker.Track(frame);
Mat tracking_frame = frame.clone();
if (! (obj.width * obj.height)) {
cout << "Tracking lost!" << endl;
break;
}
rectangle(tracking_frame, obj, Scalar(255, 0, 0));
imshow("Tracking object", tracking_frame);
waitKey(30);
cap >> frame;
}
}

return 0;
}
28 changes: 25 additions & 3 deletions src/detection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,32 @@

using std::string;
using std::shared_ptr;
using std::vector;
using namespace cv;

shared_ptr<Detector> Detector::CreateDetector(const string& name) {
std::cerr << "Failed to create detector with name '" << name << "'"
<< std::endl;
return nullptr;
if (name == "cascade") {
return std::make_shared<CascadeDetector>();
}
else {
std::cerr << "Failed to create detector with name '" << name << "'"
<< std::endl;
return nullptr;
}
}

bool CascadeDetector::Init(const std::string& model_file_path) {
return detector.load(model_file_path);
}

void CascadeDetector::Detect(const cv::Mat& frame, std::vector<cv::Rect>& objects,
std::vector<double>& scores) {
if (!detector.empty()) {
vector<int> dscores;
detector.detectMultiScale(frame, objects, dscores);
scores.resize(dscores.size());
for (int i = 0; i < dscores.size(); i++) {
scores[i] = (double)dscores[i];
}
}
}
Loading