-
Notifications
You must be signed in to change notification settings - Fork 28
Prototype for second practice (Zhiltsov). #17
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
Open
zhiltsov-max
wants to merge
16
commits into
itseez-academy:master
Choose a base branch
from
zhiltsov-max:new-sample
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
a606c55
Prototype for second practice.
ae08598
7th task completed.
6ee1dfa
8th task completed.
f7ab402
Update processing.cpp
zhiltsov-max e0d97c0
Update processing.cpp
zhiltsov-max 9b47a90
Update application.cpp
zhiltsov-max 07fc3b9
9th task completed.
8fabd96
Merge remote-tracking branch 'origin/new-sample' into new-sample
640184a
Compilation fix.
f1a63c3
Compilation fix...
82c25d6
Code style fixes
zhiltsov-max 3ba9ed1
C++11 is again disabled =(. Fixes.
zhiltsov-max f00e459
Error fixes
zhiltsov-max 7faf827
Fixes
zhiltsov-max 58a30ba
Fixes
zhiltsov-max 029fddf
Fixes
zhiltsov-max File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
set(target "sample_zhiltsov") | ||
|
||
file(GLOB hdrs "*.hpp") | ||
file(GLOB srcs "*.cpp") | ||
|
||
add_executable(${target} ${srcs} ${hdrs}) | ||
target_link_libraries(${target} ${LIBRARY_DEPS}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
#include "application.hpp" | ||
#include "processing.hpp" | ||
|
||
#include <opencv2/highgui/highgui.hpp> | ||
#include <time.h> | ||
#include <string> | ||
#include <iostream> | ||
|
||
using namespace cv; | ||
|
||
int Application::parseArguments(int argc, const char **argv, | ||
Application::Parameters ¶ms) | ||
{ | ||
if (argc < 2) | ||
{ | ||
return 1; | ||
} | ||
params.imgFileName = std::string(argv[1]); | ||
return 0; | ||
} | ||
|
||
int Application::getFrame(const std::string &fileName, Mat& src) | ||
{ | ||
src = imread(fileName); | ||
if (src.empty()) | ||
{ | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
|
||
int Application::processFrame(const Mat& src, Mat& dst) | ||
{ | ||
processor.processFrame(src, dst); | ||
|
||
if (dst.empty()) | ||
{ | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
int Application::drawButtons(Mat &display) | ||
{ | ||
guiState.onButtonPlace = Rect(20, display.rows - 60, 120, 40); | ||
guiState.offButtonPlace = Rect(160, display.rows - 60, 120, 40); | ||
guiState.saveButtonPlace = Rect(300, display.rows - 60, 120, 40); | ||
|
||
guiState.filterBlurButtonPlace = Rect(20, display.rows - 120, 120, 40); | ||
guiState.filterCannyButtonPlace = Rect(160, display.rows - 120, 120, 40); | ||
guiState.filterPixelizeButtonPlace = Rect(300, display.rows - 120, 120, 40); | ||
guiState.filterGrayscaleButtonPlace = Rect(440, display.rows - 120, 120, 40); | ||
|
||
rectangle(display, guiState.onButtonPlace, Scalar(128, 128, 128), CV_FILLED); | ||
rectangle(display, guiState.offButtonPlace, Scalar(128, 128, 128), CV_FILLED); | ||
rectangle(display, guiState.saveButtonPlace, Scalar(128, 128, 128), CV_FILLED); | ||
rectangle(display, guiState.filterBlurButtonPlace, Scalar(128, 128, 128), CV_FILLED); | ||
rectangle(display, guiState.filterCannyButtonPlace, Scalar(128, 128, 128), CV_FILLED); | ||
rectangle(display, guiState.filterPixelizeButtonPlace, Scalar(128, 128, 128), CV_FILLED); | ||
rectangle(display, guiState.filterGrayscaleButtonPlace, Scalar(128, 128, 128), CV_FILLED); | ||
|
||
putText(display, "on", | ||
Point(guiState.onButtonPlace.x + guiState.onButtonPlace.width / 2 - 15, | ||
guiState.onButtonPlace.y + guiState.onButtonPlace.height / 2 + 10), | ||
FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); | ||
putText(display, "off", | ||
Point(guiState.offButtonPlace.x + guiState.offButtonPlace.width / 2 - 20, | ||
guiState.offButtonPlace.y + guiState.offButtonPlace.height / 2 + 10), | ||
FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); | ||
putText(display, "save", | ||
Point(guiState.saveButtonPlace.x + guiState.saveButtonPlace.width / 2 - 20, | ||
guiState.saveButtonPlace.y + guiState.saveButtonPlace.height / 2 + 10), | ||
FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); | ||
putText(display, "Canny", | ||
Point(guiState.filterCannyButtonPlace.x + guiState.filterCannyButtonPlace.width / 2 - 20, | ||
guiState.filterCannyButtonPlace.y + guiState.filterCannyButtonPlace.height / 2 + 10), | ||
FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); | ||
putText(display, "Blur", | ||
Point(guiState.filterBlurButtonPlace.x + guiState.filterBlurButtonPlace.width / 2 - 20, | ||
guiState.filterBlurButtonPlace.y + guiState.filterBlurButtonPlace.height / 2 + 10), | ||
FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); | ||
putText(display, "Pixelize", | ||
Point(guiState.filterPixelizeButtonPlace.x + guiState.filterPixelizeButtonPlace.width / 2 - 20, | ||
guiState.filterPixelizeButtonPlace.y + guiState.filterPixelizeButtonPlace.height / 2 + 10), | ||
FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); | ||
putText(display, "Grayscale", | ||
Point(guiState.filterGrayscaleButtonPlace.x + guiState.filterGrayscaleButtonPlace.width / 2 - 20, | ||
guiState.filterGrayscaleButtonPlace.y + guiState.filterGrayscaleButtonPlace.height / 2 + 10), | ||
FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); | ||
|
||
return 0; | ||
} | ||
|
||
int Application::showFrame(const std::string &caption, const Mat& src, Mat& dst) | ||
{ | ||
if (guiState.state == OffFilter) | ||
{ | ||
src.copyTo(dst); | ||
} | ||
else if (guiState.state == OnFilter) | ||
{ | ||
processFrame(src, dst); | ||
} | ||
else | ||
{ | ||
return 1; | ||
} | ||
|
||
Mat display(src.rows, src.cols + dst.cols, src.type()); | ||
Mat srcRoi = display(Rect(0, 0, src.cols, src.rows)); | ||
src.copyTo(srcRoi); | ||
Mat dstRoi = display(Rect(src.cols, 0, dst.cols, dst.rows)); | ||
dst.copyTo(dstRoi); | ||
|
||
drawButtons(display); | ||
|
||
namedWindow(caption); | ||
imshow(caption, display); | ||
|
||
butonClickHandleArgs.dstBuf = &dst; | ||
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. button, а не buton :) |
||
setMouseCallback(caption, onButtonClick, this); | ||
|
||
char key = waitKey(1); | ||
|
||
return key; | ||
} | ||
|
||
void Application::onButtonClick(int eventId, int x, int y, int flsgs, void *userData) | ||
{ | ||
if (eventId != EVENT_LBUTTONDOWN) | ||
{ | ||
return; | ||
} | ||
Application* app = static_cast<Application*>(userData); | ||
|
||
if (isButtonClicked(app->guiState.onButtonPlace, x, y)) | ||
{ | ||
app->guiState.state = Application::OnFilter; | ||
return; | ||
} | ||
if (isButtonClicked(app->guiState.offButtonPlace, x, y)) | ||
{ | ||
app->guiState.state = Application::OffFilter; | ||
return; | ||
} | ||
if (isButtonClicked(app->guiState.saveButtonPlace, x, y)) | ||
{ | ||
std::stringstream ss; | ||
ss << clock() << ".png"; | ||
imwrite(ss.str(), *app->butonClickHandleArgs.dstBuf); | ||
return; | ||
} | ||
if (isButtonClicked(app->guiState.filterBlurButtonPlace, x, y)) | ||
{ | ||
app->processor.setFilterType(FilterType::Blur); | ||
return; | ||
} | ||
if (isButtonClicked(app->guiState.filterCannyButtonPlace, x, y)) | ||
{ | ||
app->processor.setFilterType(FilterType::Canny); | ||
return; | ||
} | ||
if (isButtonClicked(app->guiState.filterGrayscaleButtonPlace, x, y)) | ||
{ | ||
app->processor.setFilterType(FilterType::Grayscale); | ||
return; | ||
} | ||
if (isButtonClicked(app->guiState.filterPixelizeButtonPlace, x, y)) | ||
{ | ||
app->processor.setFilterType(FilterType::Pixelize); | ||
return; | ||
} | ||
} | ||
|
||
bool Application::isButtonClicked(cv::Rect buttonPlace, int x, int y) | ||
{ | ||
if (x < buttonPlace.x || x > buttonPlace.x + buttonPlace.width || | ||
y < buttonPlace.y || y > buttonPlace.y + buttonPlace.height) | ||
{ | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <iostream> | ||
#include <opencv2/core/core.hpp> | ||
|
||
#include "processing.hpp" | ||
|
||
bool onButtonClicked(cv::Rect buttonPlace, int x, int y); | ||
void onButtonsOnOffClick(int eventId, int x, int y, int flags, void *userData); | ||
|
||
class Application | ||
{ | ||
public: | ||
enum WindowState | ||
{ | ||
OnFilter, | ||
OffFilter | ||
}; | ||
struct Parameters | ||
{ | ||
std::string imgFileName; | ||
}; | ||
struct GUIElementsState | ||
{ | ||
WindowState state; | ||
cv::Rect onButtonPlace; | ||
cv::Rect offButtonPlace; | ||
cv::Rect saveButtonPlace; | ||
cv::Rect filterGrayscaleButtonPlace; | ||
cv::Rect filterPixelizeButtonPlace; | ||
cv::Rect filterCannyButtonPlace; | ||
cv::Rect filterBlurButtonPlace; | ||
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. Опять форматирование. Проверьте, пожалуйста, везде табуляцию. |
||
}; | ||
int parseArguments(int argc, const char **argv, Parameters ¶ms); | ||
int getFrame(const std::string &fileName, cv::Mat& src); | ||
int processFrame(const cv::Mat& src, cv::Mat& dst); | ||
int showFrame(const std::string &caption, | ||
const cv::Mat& src, cv::Mat& dst); | ||
|
||
Application() | ||
{ | ||
guiState.state = OnFilter; | ||
processor.setFilterType(FilterType::Blur); | ||
}; | ||
|
||
private: | ||
Processing processor; | ||
GUIElementsState guiState; | ||
struct ButonClickHandleArgs | ||
{ | ||
cv::Mat* dstBuf; | ||
}; | ||
ButonClickHandleArgs butonClickHandleArgs; | ||
static void onButtonClick(int eventId, int x, int y, int flags, void *userData); | ||
static bool isButtonClicked(cv::Rect buttonPlace, int x, int y); | ||
|
||
int drawButtons(cv::Mat &display); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include <opencv2/core/core.hpp> | ||
#include <iostream> | ||
|
||
#include "application.hpp" | ||
|
||
using namespace std; | ||
using namespace cv; | ||
|
||
enum ErrorCode { | ||
OK, | ||
WRONG_ARGUMENTS, | ||
WRONG_INPUT, | ||
CANT_PROCESS | ||
}; | ||
|
||
int main(int argc, const char **argv) | ||
{ | ||
Application app; | ||
Application::Parameters params; | ||
|
||
if (app.parseArguments(argc, argv, params) != 0) | ||
{ | ||
cout << "sample_template <image_name>" << endl; | ||
cout << "<image_name> - image name for filtering" << endl; | ||
return WRONG_ARGUMENTS; | ||
} | ||
|
||
Mat src; | ||
if (app.getFrame(params.imgFileName, src) != 0) | ||
{ | ||
cout << "Error: \'src\' image is null or empty!" << endl; | ||
return WRONG_INPUT; | ||
} | ||
|
||
const std::string caption = "OpenCV Sample"; | ||
char key = 0; | ||
Mat dst(src.rows, src.cols, src.type()); | ||
while (key != 27) // Esc | ||
{ | ||
key = app.showFrame(caption, src, dst); | ||
} | ||
|
||
return OK; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@zhiltsov-max поправьте, пожалуйста, форматирование, у вас явные проблемы с табуляцией.