From a99e643f7ab4cddaf8cbac726fd3682a60f84423 Mon Sep 17 00:00:00 2001 From: luba Date: Thu, 19 Oct 2017 17:00:08 +0300 Subject: [PATCH 1/5] add grayscale filter --- include/image_processing.hpp | 37 ++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/include/image_processing.hpp b/include/image_processing.hpp index ca1f116..a2f8a8e 100644 --- a/include/image_processing.hpp +++ b/include/image_processing.hpp @@ -2,6 +2,7 @@ #include #include +#include #include "opencv2/core/core.hpp" @@ -15,4 +16,40 @@ 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 ImageProcessor +{ +public: + cv::Mat CvtColor(const cv::Mat &src, const cv::Rect &roi) override { + + cv::Mat src_copy; + src.copyTo(src_copy); + cv::Mat src_copy_roi = src_copy(roi); + cv::Mat dst_gray_roi; + cv::cvtColor(src_copy_roi, dst_gray_roi, CV_BGR2GRAY); + std::vector channels(3); + std::for_each(channels.begin(), channels.end(), [&dst_gray_roi](cv::Mat elem){ + elem = dst_gray_roi; + }); + cv::Mat dst_roi; + cv::merge(channels, dst_roi); + dst_roi.copyTo(src_copy_roi); + return src_copy; + } + + cv::Mat Filter(const cv::Mat &src, const cv::Rect &roi, const int kSize) override { + + return cv::Mat(); + } + + cv::Mat + DetectEdges(const cv::Mat &src, const cv::Rect &roi, const int filterSize, const int lowThreshold, const int ratio, + const int kernelSize) override { + return cv::Mat(); + } + + cv::Mat Pixelize(const cv::Mat &src, const cv::Rect &roi, const int kDivs) override { + return cv::Mat(); + } }; \ No newline at end of file From ab40bcdf2b03c06986def1f0abafe451f591568e Mon Sep 17 00:00:00 2001 From: luba Date: Thu, 19 Oct 2017 18:07:12 +0300 Subject: [PATCH 2/5] add main for only gray --- include/image_processing.hpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/include/image_processing.hpp b/include/image_processing.hpp index a2f8a8e..166ad6e 100644 --- a/include/image_processing.hpp +++ b/include/image_processing.hpp @@ -29,9 +29,8 @@ class ImageProcessorImpl : public ImageProcessor cv::Mat dst_gray_roi; cv::cvtColor(src_copy_roi, dst_gray_roi, CV_BGR2GRAY); std::vector channels(3); - std::for_each(channels.begin(), channels.end(), [&dst_gray_roi](cv::Mat elem){ - elem = dst_gray_roi; - }); + + std::fill(channels.begin(), channels.end(), dst_gray_roi); cv::Mat dst_roi; cv::merge(channels, dst_roi); dst_roi.copyTo(src_copy_roi); From 8abd2a4c846686398c5a2a4df2b6b213f9bf32be Mon Sep 17 00:00:00 2001 From: luba Date: Thu, 19 Oct 2017 18:07:47 +0300 Subject: [PATCH 3/5] add main for only gray --- samples/imgproc_demo.cpp | 94 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 samples/imgproc_demo.cpp diff --git a/samples/imgproc_demo.cpp b/samples/imgproc_demo.cpp new file mode 100644 index 0000000..e40d02b --- /dev/null +++ b/samples/imgproc_demo.cpp @@ -0,0 +1,94 @@ +#include +#include +#include +#include + +#include "opencv2/core.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 = + "{ @image | | 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 }"; +struct MouseCallbackState { + bool is_selection_started; + bool is_selection_finished; + Point point_first; + Point point_second; +} mouseCallbackState; +void onMouse(int event, int x, int y, int flag, void* param) +{ + if(event == cv::EVENT_LBUTTONDOWN) + { + mouseCallbackState.is_selection_started = true; + mouseCallbackState.is_selection_finished = false; + mouseCallbackState.point_first = cv::Point(x, y); + } + if(event == cv::EVENT_LBUTTONUP) + { + mouseCallbackState.is_selection_started = false; + mouseCallbackState.is_selection_finished = true; + mouseCallbackState.point_second = cv::Point(x, y); + } + if(event == cv::EVENT_MOUSEMOVE && !mouseCallbackState.is_selection_finished) + { + mouseCallbackState.point_second = cv::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("help")) { + parser.printMessage(); + return 0; + } + + + // Do something cool. + cv::namedWindow("input"); + cv::setMouseCallback("input", onMouse); + cv::Mat input = cv::imread("/home/luba/Downloads/Lenna.jpg"); + cv::Mat input_copy; + cv::Rect rect; + while(true) + { + input.copyTo(input_copy); + + if(mouseCallbackState.is_selection_started && !mouseCallbackState.is_selection_finished) { + rect = cv::Rect(mouseCallbackState.point_first.x, mouseCallbackState.point_first.y, + mouseCallbackState.point_second.x - mouseCallbackState.point_first.x, + mouseCallbackState.point_second.y - mouseCallbackState.point_first.y); + } + cv::rectangle(input_copy, rect, cv::Scalar(90, 108, 70)); + cv::imshow("input", input_copy); + char c = cv::waitKey(39); + if (c == 27) + break; + } + ImageProcessorImpl processor; + + if(parser.has("gray")){ + input = processor.CvtColor(input, rect); + } + else{ + cerr << "no flag" << endl; + } + cv::imshow("input", input); + cv::waitKey(0); + + return 0; +} From 678bab7dfb625b75ed3f2843435b549f9af81583 Mon Sep 17 00:00:00 2001 From: luba Date: Thu, 26 Oct 2017 16:46:21 +0300 Subject: [PATCH 4/5] implementation filters --- include/image_processing.hpp | 44 ++++++++++++++++++++++++++++++------ samples/imgproc_demo.cpp | 9 ++++++++ 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/include/image_processing.hpp b/include/image_processing.hpp index 166ad6e..690c2c5 100644 --- a/include/image_processing.hpp +++ b/include/image_processing.hpp @@ -18,8 +18,7 @@ class ImageProcessor { const int kDivs) = 0; }; -class ImageProcessorImpl : public ImageProcessor -{ +class ImageProcessorImpl : public ImageProcessor { public: cv::Mat CvtColor(const cv::Mat &src, const cv::Rect &roi) override { @@ -38,17 +37,48 @@ class ImageProcessorImpl : public ImageProcessor } cv::Mat Filter(const cv::Mat &src, const cv::Rect &roi, const int kSize) override { - - return cv::Mat(); + cv::Mat src_copy; + src.copyTo(src_copy); + cv::Mat src_copy_roi = src_copy(roi); + cv::medianBlur(src_copy_roi, src_copy_roi, kSize); + return src_copy; } cv::Mat DetectEdges(const cv::Mat &src, const cv::Rect &roi, const int filterSize, const int lowThreshold, const int ratio, const int kernelSize) override { - return cv::Mat(); + cv::Mat src_copy; + src.copyTo(src_copy); + cv::Mat src_roi = src_copy(roi); + cv::Mat src_gray_roi; + cv::cvtColor(src_roi, src_gray_roi, CV_BGR2GRAY); + cv::Mat detected_edges; + cv::blur(src_gray_roi, src_gray_roi, cv::Size(filterSize, filterSize)); + cv::Canny(src_gray_roi, detected_edges, lowThreshold, lowThreshold + ratio, kernelSize); + cv::Mat dst; + src.copyTo(dst); + cv::Mat dst_roi = dst(roi); + dst_roi = cv::Scalar::all(0); + src_roi.copyTo(dst_roi, detected_edges); + return dst; } cv::Mat Pixelize(const cv::Mat &src, const cv::Rect &roi, const int kDivs) override { - return cv::Mat(); + cv::Mat src_copy; + src.copyTo(src_copy); + cv::Mat src_copy_roi = src_copy(roi); + int block_size_x = roi.width / kDivs; + int block_size_y = roi.height / kDivs; + for (int i = 0; i < src_copy_roi.rows; i += block_size_y) + for (int j = 0; j < src_copy_roi.cols; j += block_size_x) { + cv::Mat src_roi_block = src_copy_roi(cv::Rect(j, i, + j + block_size_x <= src_copy_roi.cols ? + block_size_x : src_copy_roi.cols - j, + i + block_size_y <= src_copy_roi.rows ? + block_size_y : src_copy_roi.rows - i)); + + cv::blur(src_roi_block, src_roi_block, cv::Size(block_size_x, block_size_y)); + } + return src_copy; } -}; \ No newline at end of file +}; diff --git a/samples/imgproc_demo.cpp b/samples/imgproc_demo.cpp index e40d02b..c60a6af 100644 --- a/samples/imgproc_demo.cpp +++ b/samples/imgproc_demo.cpp @@ -84,6 +84,15 @@ int main(int argc, const char** argv) { if(parser.has("gray")){ input = processor.CvtColor(input, rect); } + else if(parser.has("pix")) { + input = processor.Pixelize(input, rect, 5); + } + else if(parser.has("edges")) { + input = processor.DetectEdges(input, rect, 5, 2, 3, 5); + } + else if(parser.has("median")) { + input = processor.Filter(input, rect, 5); + } else{ cerr << "no flag" << endl; } From 29f103b72087208ac0708faec45094206be9c6a7 Mon Sep 17 00:00:00 2001 From: luba Date: Thu, 2 Nov 2017 14:32:11 +0300 Subject: [PATCH 5/5] fix image_process --- include/detection.hpp | 22 ++++++++- samples/detection_demo.cpp | 96 ++++++++++++++++++++++++++++++++++++++ samples/imgproc_demo.cpp | 27 +++++------ src/detection.cpp | 27 +++++++++-- 4 files changed, 150 insertions(+), 22 deletions(-) create mode 100644 samples/detection_demo.cpp diff --git a/include/detection.hpp b/include/detection.hpp index 46930d3..cf66efe 100644 --- a/include/detection.hpp +++ b/include/detection.hpp @@ -1,14 +1,32 @@ #pragma once - +#include #include #include -#include "opencv2/core/core.hpp" +#include +#include +#include +#include + +using namespace std; +using namespace cv; class Detector { public: static std::shared_ptr CreateDetector(const std::string& name); + virtual bool Init(const std::string& model_file_path) = 0; virtual void Detect(const cv::Mat& frame, std::vector& objects, std::vector& 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& objects, + std::vector& scores); + +protected: + cv::CascadeClassifier detector; +}; + diff --git a/samples/detection_demo.cpp b/samples/detection_demo.cpp new file mode 100644 index 0000000..c538291 --- /dev/null +++ b/samples/detection_demo.cpp @@ -0,0 +1,96 @@ +#include +#include "detection.hpp" + + +using namespace std; +using namespace cv; + +const char* kOptions = + "{ i image | | image to process }" + "{ v video | | video to process }" + "{ c camera | | camera to get video from }" + "{ m model | | path to detector file }" + "{ h ? help usage | | print help message }"; + +int main(int argc, const char** argv) { + // Parse command line arguments. + CommandLineParser parser(argc, argv, kOptions); + + // If help option is given, print help message and exit. + if (parser.get("help")) { + parser.printMessage(); + return 0; + } + + CascadeDetector detector; + //("../test/test_data/detection/cascades/intel_logo_cascade.xml"); + detector.Init("/home/luba/github/itseez-ss-2016-practice/test/test_data/detection/cascades/unn_old_logo_cascade.xml"); + + cv::Mat frame1; + std::vector objects; + std::vector scores; + + /*if(parser.has("v")){ + std::string filePath; + filePath = parser.get("v"); + cv::VideoCapture video(filePath); + + while(true){ + video >> frame1; + detector.Detect(frame1, objects, scores); + } + } + else if(parser.has("i")){ + std::string filePath; + filePath = parser.get("i"); + cv::Mat input = cv::imread(filePath); + detector.Detect(input, objects, scores); + } + else if(parser.has("c")){ + + CvCapture* capture = cvCreateCameraCapture(CV_CAP_ANY); + assert( capture ); + IplImage* frame=0; + cvNamedWindow("capture", CV_WINDOW_AUTOSIZE); + printf("[i] press Esc for quit!\n\n"); + while(true) {// получаем кадр + frame = cvQueryFrame(capture); + detector.Detect(frame1, objects, scores); + cvShowImage("capture", frame); + char c = cvWaitKey(33); + if (c == 27) { // нажата ESC + break; + } + } + cvReleaseCapture( &capture ); + cvDestroyWindow("capture"); + } + else if(parser.has("m")){ + std::string filePathDetector; + filePathDetector = parser.has("m"); + detector.Init( filePathDetector); + }*/ + + CvCapture* capture = cvCreateCameraCapture(CV_CAP_ANY); + assert( capture ); + IplImage* frame=0; + cvNamedWindow("capture", CV_WINDOW_AUTOSIZE); + printf("[i] press Esc for quit!\n\n"); + while(true) {// получаем кадр + frame = cvQueryFrame(capture); + detector.Detect(frame1, objects, scores); + cvShowImage("capture", frame); + char c = cvWaitKey(33); + if (c == 27) { // нажата ESC + break; + } + } + cvReleaseCapture( &capture ); + cvDestroyWindow("capture"); + + + + return 0; +} + + diff --git a/samples/imgproc_demo.cpp b/samples/imgproc_demo.cpp index c60a6af..855ee5d 100644 --- a/samples/imgproc_demo.cpp +++ b/samples/imgproc_demo.cpp @@ -1,17 +1,12 @@ #include #include #include +#include #include -#include "opencv2/core.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 = "{ @image | | image to process }" "{ gray | | convert ROI to gray scale }" @@ -48,8 +43,6 @@ void onMouse(int event, int x, int y, int flag, void* param) 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("help")) { @@ -61,7 +54,11 @@ int main(int argc, const char** argv) { // Do something cool. cv::namedWindow("input"); cv::setMouseCallback("input", onMouse); - cv::Mat input = cv::imread("/home/luba/Downloads/Lenna.jpg"); + std::string filePath; + if (parser.has("@image")){ + filePath = parser.get("@image"); + } + cv::Mat input = cv::imread(filePath); cv::Mat input_copy; cv::Rect rect; while(true) @@ -70,14 +67,14 @@ int main(int argc, const char** argv) { if(mouseCallbackState.is_selection_started && !mouseCallbackState.is_selection_finished) { rect = cv::Rect(mouseCallbackState.point_first.x, mouseCallbackState.point_first.y, - mouseCallbackState.point_second.x - mouseCallbackState.point_first.x, - mouseCallbackState.point_second.y - mouseCallbackState.point_first.y); + mouseCallbackState.point_second.x - mouseCallbackState.point_first.x, + mouseCallbackState.point_second.y - mouseCallbackState.point_first.y); } - cv::rectangle(input_copy, rect, cv::Scalar(90, 108, 70)); + cv::rectangle(input_copy, rect, cv::Scalar(255, 0, 100)); cv::imshow("input", input_copy); - char c = cv::waitKey(39); - if (c == 27) - break; + char c = cv::waitKey(33); + if (c == 27) + break; } ImageProcessorImpl processor; diff --git a/src/detection.cpp b/src/detection.cpp index 15e7fd1..bd2e2da 100644 --- a/src/detection.cpp +++ b/src/detection.cpp @@ -1,13 +1,30 @@ -#include "detection.hpp" - #include +#include "detection.hpp" using std::string; using std::shared_ptr; using namespace cv; shared_ptr 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(); + } +} + + +bool CascadeDetector::Init(const std::string &model_file_path) { + detector.load(model_file_path); + return detector.empty(); +} + +void CascadeDetector::Detect(const cv::Mat &frame, std::vector &objects, std::vector &scores) { + if (!detector.empty() ) { + std::vector sc; + detector.detectMultiScale(frame, objects, sc); + std::copy(sc.begin(), sc.end(), scores.begin()); + } + else { + std::cerr << "Error"; + exit(-1); + } }