-
Notifications
You must be signed in to change notification settings - Fork 23
Optimizing, Shchedrin #6
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
rashchedrin
wants to merge
14
commits into
itseez-academy:master
Choose a base branch
from
rashchedrin:optimizing
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 all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e643931
included ctime fixed compile error
be8f9d4
fixed empty frame bug
c092a04
created my files from template
7428f93
made rng constant
7034258
add tests + ???
971f70c
bgr rgb gix
acae09e
dead code removed
a9705e8
used setTo instead of loops
69b6893
cleaned
c59962d
cleaned
b0f19b0
cleaned
be16b1d
optimizing alphaBlend
9443ca7
optimizing bleng
fe3d490
cleaned
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| set(target gtest) | ||
|
|
||
| FILE(GLOB hdrs "gtest/*.h") | ||
| FILE(GLOB srcs "gtest/*.cc") | ||
|
|
||
| ADD_LIBRARY(${target} STATIC ${srcs}) | ||
|
|
||
| # FIXME: update this check (Kirill Kornyakov) | ||
| if((CXX_GCC OR CXX_CLANG) AND (${CMAKE_SYSTEM_NAME} MATCHES "Linux")) | ||
| set(Pthread "-pthread") | ||
| endif() | ||
| TARGET_LINK_LIBRARIES(${target} ${Pthread}) |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,116 @@ | ||
| #include <stdio.h> | ||
| #include <iostream> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include "opencv2/core/core.hpp" | ||
| #include "opencv2/highgui/highgui.hpp" | ||
| #include "opencv2/objdetect/objdetect.hpp" | ||
|
|
||
| #include "retro_filter.hpp" | ||
|
|
||
| using namespace std; | ||
| using namespace cv; | ||
|
|
||
| // Macros for time measurements | ||
| #if 1 | ||
| #define TS(name) int64 t_##name = getTickCount() | ||
| #define TE(name) printf("TIMER_" #name ": %.2fms\n", \ | ||
| 1000.f * ((getTickCount() - t_##name) / getTickFrequency())) | ||
| #else | ||
| #define TS(name) | ||
| #define TE(name) | ||
| #endif | ||
|
|
||
| const char* params = | ||
| "{ h | help | false | print usage }" | ||
| "{ | video | | video file to detect on }" | ||
| "{ | camera | false | whether to detect on video stream from camera }" | ||
| "{ | border | | image to use as a border }" | ||
| "{ | scratches | | image to use for scratches }"; | ||
|
|
||
| int main(int argc, const char** argv) | ||
| { | ||
| // Parse command line arguments | ||
| CommandLineParser parser(argc, argv, params); | ||
|
|
||
| // If help flag is given, print help message and exit | ||
| if (parser.get<bool>("help")) | ||
| { | ||
| parser.printParams(); | ||
| return 1; | ||
| } | ||
|
|
||
| string image_file = parser.get<string>("image"); | ||
| string video_file = parser.get<string>("video"); | ||
| bool use_camera = parser.get<bool>("camera"); | ||
| string border = parser.get<string>("border"); | ||
| string scratches = parser.get<string>("scratches"); | ||
|
|
||
| RetroFilter::Parameters params; | ||
| params.fuzzyBorder = imread(border, 0); | ||
| params.scratches = imread(scratches, 0); | ||
|
|
||
| if (params.fuzzyBorder.empty()) | ||
| cout << "Error: failed to open image to use as a border: " << border << endl; | ||
| if (params.scratches.empty()) | ||
| cout << "Error: failed to open image to use for scratches: " << scratches << endl; | ||
| if (params.fuzzyBorder.empty() || params.scratches.empty()) | ||
| return 1; | ||
|
|
||
| VideoCapture capture; | ||
| if (!video_file.empty()) | ||
| { | ||
| capture.open(video_file); | ||
| if (!capture.isOpened()) | ||
| { | ||
| cout << "Error: failed to open video stream for: " << video_file << endl; | ||
| return 1; | ||
| } | ||
| } | ||
| else if (use_camera) | ||
| { | ||
| capture.open(0); | ||
| if (!capture.isOpened()) | ||
| { | ||
| cout << "Error: failed to open video stream for camera #0" << endl; | ||
| return 1; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| cout << "Error: declare a source of images" << endl; | ||
| parser.printParams(); | ||
| return 1; | ||
| } | ||
|
|
||
| Mat frame; | ||
| capture >> frame; | ||
|
|
||
| while (frame.empty()) | ||
| { | ||
| capture >> frame; | ||
| } | ||
|
|
||
| params.frameSize = frame.size(); | ||
| RetroFilter filter(params); | ||
|
|
||
| for(;;) | ||
| { | ||
| Mat retroFrame; | ||
| TS(filter); | ||
| filter.applyToVideo(frame, retroFrame); | ||
| TE(filter); | ||
|
|
||
| imshow("Original Movie", frame); | ||
| imshow("Retro Movie", retroFrame); | ||
| char c = (char) waitKey(1); | ||
| if( c == 27 ) // Esc | ||
| break; | ||
|
|
||
| capture >> frame; | ||
| if(frame.empty()) break; | ||
| } | ||
|
|
||
| return 0; | ||
| } |
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,82 @@ | ||
| #include "retro_filter.hpp" | ||
| #include "opencv2/imgproc/imgproc.hpp" | ||
| #include <ctime> | ||
|
|
||
| using namespace std; | ||
| using namespace cv; | ||
|
|
||
| inline void alphaBlend(const Mat& src, Mat& dst, const Mat& alpha) | ||
| { | ||
| Mat w, d, s, dw, sw; | ||
| alpha.convertTo(w, CV_32S); | ||
| src.convertTo(s, CV_32S); | ||
| dst.convertTo(d, CV_32S); | ||
|
|
||
| multiply(s, w, sw); | ||
| multiply(d, -w, dw); | ||
| d = (d*255 + sw + dw)/255.0; | ||
| d.convertTo(dst, CV_8U); | ||
| } | ||
|
|
||
| RetroFilter::RetroFilter(const Parameters& params) : rng_(time(0)) | ||
| { | ||
| params_ = params; | ||
|
|
||
| resize(params_.fuzzyBorder, params_.fuzzyBorder, params_.frameSize); | ||
|
|
||
| if (params_.scratches.rows < params_.frameSize.height || | ||
| params_.scratches.cols < params_.frameSize.width) | ||
| { | ||
| resize(params_.scratches, params_.scratches, params_.frameSize); | ||
| } | ||
|
|
||
| hsvScale_ = 1; | ||
| hsvOffset_ = 20; | ||
| } | ||
|
|
||
| void RetroFilter::applyToVideo(const Mat& frame, Mat& retroFrame) | ||
| { | ||
| int col, row; | ||
| Mat luminance; | ||
| cvtColor(frame, luminance, CV_BGR2GRAY); | ||
|
|
||
| // Add scratches | ||
| Scalar meanColor = mean(luminance.row(luminance.rows / 2)); | ||
| Mat scratchColor(params_.frameSize, CV_8UC1, meanColor * 2.0); | ||
| int x = rng_.uniform(0, params_.scratches.cols - luminance.cols); | ||
| int y = rng_.uniform(0, params_.scratches.rows - luminance.rows); | ||
|
|
||
| for (row = 0; row < luminance.size().height; row += 1) | ||
| { | ||
| for (col = 0; col < luminance.size().width; col += 1) | ||
| { | ||
| uchar pix_color = params_.scratches.at<uchar>(row + y, col + x) ? (int)scratchColor.at<uchar>(row, col) : luminance.at<uchar>(row, col); | ||
| luminance.at<uchar>(row, col) = pix_color; | ||
| } | ||
| } | ||
|
|
||
| // Add fuzzy border | ||
| Mat borderColor(params_.frameSize, CV_32FC1, Scalar::all(meanColor[0] * 1.5)); | ||
| alphaBlend(borderColor, luminance, params_.fuzzyBorder); | ||
|
|
||
|
|
||
| // Apply sepia-effect | ||
| retroFrame.create(luminance.size(), CV_8UC3); | ||
| Mat hsv_pixel(1, 1, CV_8UC3); | ||
| Mat rgb_pixel(1, 1, CV_8UC3); | ||
| for (col = 0; col < luminance.size().width; col += 1) | ||
| { | ||
| for (row = 0; row < luminance.size().height; row += 1) | ||
| { | ||
| hsv_pixel.ptr()[2] = cv::saturate_cast<uchar>(luminance.at<uchar>(row, col) * hsvScale_ + hsvOffset_); | ||
| hsv_pixel.ptr()[0] = 19; | ||
| hsv_pixel.ptr()[1] = 78; | ||
|
|
||
| cvtColor(hsv_pixel, rgb_pixel, CV_HSV2RGB); | ||
|
|
||
| retroFrame.at<Vec3b>(row, col)[0] = rgb_pixel.ptr()[2]; | ||
| retroFrame.at<Vec3b>(row, col)[1] = rgb_pixel.ptr()[1]; | ||
| retroFrame.at<Vec3b>(row, col)[2] = rgb_pixel.ptr()[0]; | ||
| } | ||
| } | ||
| } |
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,21 @@ | ||
| set(target practice5_tests) | ||
|
|
||
| FILE(GLOB hdrs "*.h*") | ||
| FILE(GLOB srcs "*.cpp") | ||
|
|
||
| INCLUDE_DIRECTORIES("${CMAKE_CURRENT_SOURCE_DIR}/../3rdparty/gtest") | ||
|
|
||
| # VS2012 doesn't support correctly the tuples yet, | ||
| # see http://code.google.com/p/googletest/issues/detail?id=412 | ||
| if(MSVC) | ||
| ADD_DEFINITIONS(/D _VARIADIC_MAX=10) | ||
| endif() | ||
|
|
||
| ADD_EXECUTABLE(${target} ${srcs} ${hdrs}) | ||
| TARGET_LINK_LIBRARIES(${target} gtest retrofilter) | ||
|
|
||
| add_custom_command( | ||
| TARGET practice5_tests | ||
| POST_BUILD | ||
| COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_SOURCE_DIR}/testdata" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE}/testdata" | ||
| ) |
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.
Можно упростить вот эту функцию.