-
Notifications
You must be signed in to change notification settings - Fork 39
Вихрев Иван. Lab 2 #98
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?
Changes from 6 commits
5a0dac6
3e37da1
97567dc
5817e52
cce0d4e
464b7f8
af5f56c
c995898
911f66c
60c1b81
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 |
---|---|---|
@@ -0,0 +1,141 @@ | ||
#include <iostream> | ||
#include <string> | ||
#include <image_processing.hpp> | ||
#include "opencv2/core.hpp" | ||
#include "opencv2/opencv.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; | ||
}; | ||
|
||
void OnMouse(int event, int x, int y, int flags, void* param) | ||
{ | ||
MouseCallbackState* mouse = (MouseCallbackState*)param; | ||
|
||
switch (event) | ||
{ | ||
case EVENT_LBUTTONDOWN: | ||
|
||
|
||
mouse->is_selection_started = true; | ||
mouse->is_selection_finished = false; | ||
mouse->point_first.x = x; | ||
mouse->point_first.y = y; | ||
break; | ||
|
||
case EVENT_LBUTTONUP: | ||
|
||
mouse->is_selection_started = false; | ||
mouse->is_selection_finished = true; | ||
if (mouse->point_first.x != x && mouse->point_first.y != y) | ||
{ | ||
mouse->point_second.x = x; | ||
mouse->point_second.y = y; | ||
} | ||
break; | ||
|
||
case EVENT_MOUSEMOVE: | ||
|
||
if (mouse->is_selection_finished == false) | ||
{ | ||
mouse->point_second.x = x; | ||
mouse->point_second.y = y; | ||
} | ||
break; | ||
} | ||
|
||
} | ||
|
||
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; | ||
} | ||
|
||
Mat img,src,dst; | ||
MouseCallbackState p; | ||
ImageProcessorImpl obj; | ||
Rect roi; | ||
char c; | ||
|
||
string imageName(parser.get<string>(0)); | ||
img = imread(imageName.c_str(), 1); | ||
if (img.empty()) | ||
{ | ||
cout << "Could not open or find the image" << std::endl; | ||
return -1; | ||
} | ||
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. @ivanvikhrev, поплыли отступы. 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. @valentina-kustikova, попробовал поправить отступы форматированием в VS, ничего не изменилось |
||
|
||
imshow("Original", img); | ||
setMouseCallback("Original", OnMouse, &p); | ||
while (1) | ||
{ | ||
ivanvikhrev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
while (1) | ||
{ | ||
c = waitKey(100); | ||
roi = Rect(p.point_first, p.point_second); | ||
if ((p.point_first.x < p.point_second.x) && (p.point_first.y < p.point_second.y) && !roi.empty()) break; | ||
} | ||
|
||
if (!roi.empty() && (( roi & Rect(0,0,img.cols,img.rows)) == roi)) | ||
{ | ||
if (parser.has("gray")) | ||
{ | ||
dst = obj.CvtColor(img, roi); | ||
} | ||
else | ||
if (parser.has("median")) | ||
{ | ||
int _size = 11;// size must be odd | ||
dst = obj.Filter(img, roi, 11); | ||
} | ||
else | ||
if (parser.has("edges")) | ||
{ | ||
int _filter_size = 1, | ||
_low_threshold = 50, | ||
_ratio = 4, | ||
_ksize = 1; | ||
dst = obj.DetectEdges(img, roi, _filter_size, _low_threshold, _ratio, _ksize); | ||
} | ||
else | ||
if (parser.has("pix")) | ||
{ | ||
int _divs = 11; | ||
|
||
dst = obj.Pixelize(img, roi, _divs); | ||
} | ||
imshow("Result", dst); | ||
} | ||
if (c == 27) break; | ||
} | ||
|
||
|
||
return 0; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,32 +2,61 @@ | |
#include <string> | ||
|
||
#include "opencv2/core.hpp" | ||
|
||
#include "opencv2/opencv.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 }"; | ||
|
||
|
||
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. | ||
cout << "This is empty template sample." << endl; | ||
//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 }"; | ||
// | ||
// | ||
//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. | ||
// cout << "This is empty template sample." << endl; | ||
// | ||
// return 0; | ||
//} | ||
//int main(int argc, char** argv) | ||
//{ | ||
// Mat img, gray, edges; | ||
// // relative path?? | ||
// string imageName("C:/MyProjects/itseez-ss-2016-practice/test/test_data/imgproc/lena.png"); | ||
// | ||
// if (argc > 1) | ||
// { | ||
// imageName = argv[1]; | ||
// } | ||
// img = imread(imageName.c_str(), 1); | ||
// | ||
// if (img.empty()) // Check for invalid input | ||
// { | ||
// cout << "Could not open or find the image" << std::endl; | ||
// return -1; | ||
// } | ||
// //namedWindow("Display window", WINDOW_AUTOSIZE); | ||
// imshow("original", img); | ||
// cvtColor(img, gray, COLOR_BGR2GRAY); | ||
// GaussianBlur(gray, gray, Size(7, 7), 1.5); | ||
// Canny(gray, edges, 0, 50); | ||
// //namedWindow("Display window", WINDOW_AUTOSIZE); | ||
// imshow("result", edges); | ||
// imwrite("result.png", edges); | ||
// waitKey(0); | ||
// return 0; | ||
//} | ||
|
||
|
||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include "image_processing.hpp" | ||
#include "opencv2/opencv.hpp" | ||
#include <cstddef> | ||
using namespace cv; | ||
|
||
Mat ImageProcessorImpl::CvtColor(const Mat &src, const Rect &roi) | ||
{ | ||
Mat src_copy = src.clone(); | ||
Mat src_copy_roi = src_copy(roi); | ||
Mat dst_roi = src_copy_roi.clone(), dst_gray_roi; | ||
std::vector<Mat> channels; | ||
|
||
cvtColor(src_copy_roi, dst_gray_roi, COLOR_BGR2GRAY); | ||
|
||
channels.push_back(dst_gray_roi); | ||
channels.push_back(dst_gray_roi); | ||
channels.push_back(dst_gray_roi); | ||
|
||
merge(channels, dst_roi); | ||
dst_roi.copyTo(src_copy_roi); | ||
return src_copy; | ||
} | ||
|
||
cv::Mat ImageProcessorImpl::Filter(const Mat &src, const Rect &roi, const int size) | ||
{ | ||
Mat src_copy = src.clone(); | ||
Mat src_copy_roi = src_copy(roi); | ||
|
||
medianBlur(src_copy_roi,src_copy_roi,size); | ||
|
||
return src_copy; | ||
|
||
} | ||
|
||
Mat ImageProcessorImpl::DetectEdges(const Mat &src, const Rect &roi, | ||
const int filterSize, const int lowThreshold, const int ratio, | ||
const int kernelSize) | ||
{ | ||
Mat src_copy = src.clone(); | ||
Mat src_copy_roi = src_copy(roi); | ||
Mat src_gray_roi, gray_blurred, detected_edges, dst, dst_roi; | ||
Size a(kernelSize, kernelSize); | ||
|
||
cvtColor(src_copy_roi, src_gray_roi, COLOR_BGR2GRAY); | ||
blur(src_gray_roi, gray_blurred, a); | ||
Canny(gray_blurred, detected_edges, lowThreshold, lowThreshold*ratio); | ||
|
||
dst = src.clone(); | ||
dst_roi = dst(roi); | ||
dst_roi = Scalar::all(0); | ||
src_copy_roi.copyTo(dst_roi, detected_edges); | ||
return dst; | ||
} | ||
|
||
cv::Mat ImageProcessorImpl::Pixelize(const cv::Mat & src, const cv::Rect & roi, const int divs) | ||
{ | ||
Mat src_copy = src.clone(); | ||
Mat src_copy_roi = src_copy(roi); | ||
int block_size_x = (int)(roi.width / divs), block_size_y = (int)(roi.height / divs); | ||
Size kernSize(block_size_x, block_size_y); | ||
|
||
for(int x = 0; x < src_copy_roi.cols - block_size_x; x += block_size_x) | ||
for (int y = 0; y < src_copy_roi.rows - block_size_y ; y += block_size_y) | ||
{ | ||
Mat src_roi_block = src_copy_roi(Rect(x,y,block_size_x,block_size_y)); | ||
blur(src_roi_block, src_roi_block, kernSize); | ||
} | ||
|
||
return src_copy; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
#include "workaround.hpp" | ||
|
||
#include <cstddef> | ||
|
||
using namespace std; | ||
|
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.
@ivanvikhrev, почистите код от неоправданных пустых строк.