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
16 changes: 16 additions & 0 deletions include/ImageProcessorImpl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include "image_processing.hpp"


class ImageProcessorImpl : public ImageProcessor {
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 size);
virtual cv::Mat DetectEdges(const cv::Mat &src, const cv::Rect &roi,
const int filter_size, const int low_threshold,
const int ratio, const int kernel_size);
virtual cv::Mat Pixelize(const cv::Mat &src, const cv::Rect &roi,
const int divs);
};
1 change: 1 addition & 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/opencv.hpp>

class ImageProcessor {
public:
Expand Down
2 changes: 2 additions & 0 deletions include/workaround.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once



class MatrixProcessor {
public:
void Threshold(unsigned char* const data, const int width, const int height,
Expand Down
70 changes: 49 additions & 21 deletions samples/devtools_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"

#include "ImageProcessorImpl.hpp"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TheG1uy, ваше решение должно быть в отдельном файле, которое является копией devtools_demo.cpp.

#include "workaround.hpp"

using namespace std;
Expand All @@ -12,9 +14,33 @@ 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 }";
"{ @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 }";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TheG1uy, надо почистить форматирование, чтобы отступы были везде одинаковые, чтобы не было лишних пустых строк и закомментированного кода.




Rect_<int> roi;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TheG1uy, глобальных переменных не должно быть в реализации. Необходимые объекты следует передавать через параметры void* userdata.

void CallBackFunc(int event, int x, int y, int flags, void* userdata)
{

if (event == EVENT_LBUTTONDOWN)
{
static vector<Point2d> points;
points.push_back(Point_<int>(x,y));
if (points.size() > 1) {
roi = Rect2i(points[0], points[1]);
points.clear();
}
}

}




int main(int argc, const char** argv) {
// Parse command line arguments.
Expand All @@ -28,7 +54,7 @@ int main(int argc, const char** argv) {
}

// Read image.
Mat src = imread(parser.get<string>(0), CV_LOAD_IMAGE_GRAYSCALE);
Mat src = imread(parser.get<string>(0));
if (src.empty()) {
cout << "Failed to open image file '" + parser.get<string>(0) + "'."
<< endl;
Expand All @@ -40,25 +66,27 @@ int main(int argc, const char** argv) {
const int kWaitKeyDelay = 1;
namedWindow(kSrcWindowName, WINDOW_NORMAL);
resizeWindow(kSrcWindowName, 640, 480);


setMouseCallback(kSrcWindowName, CallBackFunc, NULL);
imshow(kSrcWindowName, src);
waitKey(kWaitKeyDelay);

// Threshold data.
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;


while (true) {
waitKey(1);
if (!roi.empty()) break;
}
const string newWindowName = "filter";
ImageProcessorImpl newFiltr;
//Mat img = newFiltr.CvtColor(src, roi);
//Mat img = newFiltr.Filter(src, roi, 7);
Mat img = newFiltr.DetectEdges(src, roi, 2, 75, 3 , 5);
//Mat img = newFiltr.Pixelize(src, roi, 15);
namedWindow(newWindowName, WINDOW_NORMAL);
resizeWindow(newWindowName, 640, 480);
imshow(newWindowName, img);

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


waitKey(0);
return 0;
}
68 changes: 68 additions & 0 deletions src/ImageProcessorImpl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "ImageProcessorImpl.hpp"

cv::Mat ImageProcessorImpl::CvtColor(const cv::Mat &src, const cv::Rect &roi) {

cv::Mat src_tmp = src.clone();
cv::Mat src_tmp_roi = src_tmp(roi);
cv::Mat dst_roi_green = src_tmp_roi.clone();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TheG1uy, чтобы не писать везде cv:: можно подключить после подключения библиотек using namespace cv;

std::vector<cv::Mat> channels;

cv::cvtColor(src_tmp_roi, dst_roi_green, cv::COLOR_BGR2GRAY);
channels.push_back(dst_roi_green);
channels.push_back(dst_roi_green);
channels.push_back(dst_roi_green);
cv::merge(channels, src_tmp_roi);

return src_tmp;
}

cv::Mat ImageProcessorImpl::DetectEdges(const cv::Mat &src, const cv::Rect &roi,
const int filter_size, const int low_threshold,
const int ratio, const int kernel_size) {


cv::Mat src_tmp = src.clone();
cv::Mat src_tmp_roi = src_tmp(roi);
cv::Mat src_roi_green = src_tmp_roi.clone();
cv::Mat gray_blurred;
cv::Mat detected_edges;


cv::cvtColor(src_tmp_roi, src_roi_green, cv::COLOR_BGR2GRAY);
cv::blur(src_roi_green, gray_blurred, cv::Size(kernel_size, kernel_size));
cv::Canny(gray_blurred, detected_edges, 0, 50);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TheG1uy, здесь надо использовать параметры const int low_threshold, const int ratio, а не фиксированные константы.


cv::Mat dst = src.clone();
cv::Mat dst_roi = dst(roi);
dst_roi = cv::Scalar::all(0);
src_tmp_roi.copyTo(dst_roi, detected_edges);

return dst;
}


cv::Mat ImageProcessorImpl::Filter(const cv::Mat &src, const cv::Rect &roi,
const int size) {

cv::Mat src_tmp = src.clone();
cv::Mat src_tmp_roi = src_tmp(roi);
cv::medianBlur(src_tmp_roi, src_tmp_roi, size);

return src_tmp;
}

cv::Mat ImageProcessorImpl::Pixelize(const cv::Mat &src, const cv::Rect &roi,
const int divs) {
cv::Mat src_tmp = src.clone();
cv::Mat src_tmp_roi = src_tmp(roi);
const int block_size_x = roi.width / divs, block_size_y = roi.height / divs;

for (int x = 0; x < src_tmp_roi.cols - block_size_x ; x += block_size_x)
for (int y = 0; y < src_tmp_roi.rows - block_size_y; y += block_size_y) {
cv::Mat src_roi_block = src_tmp_roi(cv::Rect(x, y, block_size_x, block_size_y));
cv::blur(src_roi_block, src_roi_block, cv::Size(block_size_x, block_size_y));
}

return src_tmp;

}