-
Notifications
You must be signed in to change notification settings - Fork 39
Improc #86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Improc #86
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
#include <memory> | ||
#include <string> | ||
#include <opencv/cv.hpp> | ||
|
||
#include "opencv2/core/core.hpp" | ||
|
||
|
@@ -15,4 +16,69 @@ 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<cv::Mat> channels(3); | ||
|
||
std::fill(channels.begin(), channels.end(), dst_gray_roi); | ||
cv::Mat dst_roi; | ||
cv::merge(channels, dst_roi); | ||
dst_roi.copyTo(src_copy_roi); | ||
return src_copy; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Реализацию методов необходимо вынести из заголовочного файла в файл реализации. |
||
|
||
cv::Mat Filter(const cv::Mat &src, const cv::Rect &roi, const int kSize) override { | ||
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 { | ||
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 { | ||
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; | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
#include <iostream> | ||
#include <string> | ||
#include <opencv/cv.hpp> | ||
#include <image_processing.hpp> | ||
|
||
#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 | <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 }"; | ||
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<bool>("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; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Что-то форматирование поплыло. |
||
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; | ||
} | ||
cv::imshow("input", input); | ||
cv::waitKey(0); | ||
|
||
return 0; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Мне кажется, что можно было просто написать
merge(dst_gray_roi, dst_gray_roi, dst_gray_roi, dst_roi)
, операция слияния перегружена.