From 4b1aff9d3f92718b9a373c92d7b57f6bdcc6afed Mon Sep 17 00:00:00 2001 From: xamcaa Date: Fri, 21 Aug 2015 16:07:08 +0300 Subject: [PATCH 1/3] rewritten addition of scratches with opencv --- include/retro_filter.hpp | 10 ++++++++++ sample/retro_sample.cpp | 14 +++++--------- src/retro_filter.cpp | 32 +++++++++++++++----------------- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/include/retro_filter.hpp b/include/retro_filter.hpp index 86f3ee5..031eef6 100644 --- a/include/retro_filter.hpp +++ b/include/retro_filter.hpp @@ -3,6 +3,16 @@ #include "opencv2/core/core.hpp" +// 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 + class RetroFilter { public: diff --git a/sample/retro_sample.cpp b/sample/retro_sample.cpp index 1b0019f..b5bd5b8 100644 --- a/sample/retro_sample.cpp +++ b/sample/retro_sample.cpp @@ -12,15 +12,7 @@ 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 }" @@ -97,6 +89,8 @@ int main(int argc, const char** argv) params.frameSize = frame.size(); RetroFilter filter(params); + TS(total); + for(;;) { Mat retroFrame; @@ -114,5 +108,7 @@ int main(int argc, const char** argv) if(frame.empty()) break; } + TE(total); + return 0; } diff --git a/src/retro_filter.cpp b/src/retro_filter.cpp index f38b65a..047dce6 100644 --- a/src/retro_filter.cpp +++ b/src/retro_filter.cpp @@ -34,28 +34,21 @@ RetroFilter::RetroFilter(const Parameters& params) : rng_(time(0)) hsvOffset_ = 20; } +#include void RetroFilter::applyToVideo(const Mat& frame, Mat& retroFrame) { - int col, row; - Mat luminance; - cvtColor(frame, luminance, CV_BGR2GRAY); + Mat gray; + frame.copyTo(retroFrame); + double beta = 0.5; - // 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); + TS(scratching); + cvtColor(frame,gray,COLOR_BGR2GRAY); + resize(params_.scratches, retroFrame, frame.size()); + addWeighted(gray, beta, retroFrame, beta, 0.0, retroFrame); - for (row = 0; row < luminance.size().height; row += 1) - { - for (col = 0; col < luminance.size().width; col += 1) - { - uchar pix_color = params_.scratches.at(row + y, col + x) ? (int)scratchColor.at(row, col) : luminance.at(row, col); - luminance.at(row, col) = pix_color; - } - } + TE(scratching); - // Add fuzzy border + /*// Add fuzzy border Mat borderColor(params_.frameSize, CV_32FC1, Scalar::all(meanColor[0] * 1.5)); alphaBlend(borderColor, luminance, params_.fuzzyBorder); @@ -64,6 +57,9 @@ void RetroFilter::applyToVideo(const Mat& frame, Mat& retroFrame) retroFrame.create(luminance.size(), CV_8UC3); Mat hsv_pixel(1, 1, CV_8UC3); Mat rgb_pixel(1, 1, CV_8UC3); + + TS(sepia); + for (col = 0; col < luminance.size().width; col += 1) { for (row = 0; row < luminance.size().height; row += 1) @@ -79,4 +75,6 @@ void RetroFilter::applyToVideo(const Mat& frame, Mat& retroFrame) retroFrame.at(row, col)[2] = rgb_pixel.ptr()[0]; } } + + TE(sepia);*/ } From 97ea4854347bb84ed8ffb8fe05c28d9092333872 Mon Sep 17 00:00:00 2001 From: xamcaa Date: Fri, 21 Aug 2015 17:42:00 +0300 Subject: [PATCH 2/3] rewritten addition fuzzy border with opencv --- src/retro_filter.cpp | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/retro_filter.cpp b/src/retro_filter.cpp index 047dce6..5e8bfd2 100644 --- a/src/retro_filter.cpp +++ b/src/retro_filter.cpp @@ -34,7 +34,6 @@ RetroFilter::RetroFilter(const Parameters& params) : rng_(time(0)) hsvOffset_ = 20; } -#include void RetroFilter::applyToVideo(const Mat& frame, Mat& retroFrame) { Mat gray; @@ -48,12 +47,24 @@ void RetroFilter::applyToVideo(const Mat& frame, Mat& retroFrame) TE(scratching); - /*// Add fuzzy border - Mat borderColor(params_.frameSize, CV_32FC1, Scalar::all(meanColor[0] * 1.5)); - alphaBlend(borderColor, luminance, params_.fuzzyBorder); + // Add fuzzy border + int col, row; + Mat luminance; + cvtColor(frame, luminance, CV_BGR2GRAY); + Scalar meanColor = mean(luminance.row(luminance.rows / 2)); + TS(bording); - // Apply sepia-effect + Mat borderColor(params_.frameSize, CV_32FC3, Scalar::all(meanColor[0] * 1.5)); + Mat tmp; + cvtColor(borderColor,tmp,COLOR_BGR2GRAY); + addWeighted(gray, 0.5, retroFrame, 0.5, 0.0, retroFrame); + resize(params_.fuzzyBorder, tmp, frame.size()); + addWeighted(tmp, 0.5, retroFrame, 0.5, 0.0, retroFrame); + + TE(bording); + + /*// Apply sepia-effect retroFrame.create(luminance.size(), CV_8UC3); Mat hsv_pixel(1, 1, CV_8UC3); Mat rgb_pixel(1, 1, CV_8UC3); From a7282f0f15fa772ac9019cb28d4022358c27ce18 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 21 Aug 2015 18:22:06 +0300 Subject: [PATCH 3/3] rewritten addition of sepia-effect with opencv --- src/retro_filter.cpp | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/src/retro_filter.cpp b/src/retro_filter.cpp index 5e8bfd2..77ef135 100644 --- a/src/retro_filter.cpp +++ b/src/retro_filter.cpp @@ -64,28 +64,26 @@ void RetroFilter::applyToVideo(const Mat& frame, Mat& retroFrame) TE(bording); - /*// Apply sepia-effect - retroFrame.create(luminance.size(), CV_8UC3); - Mat hsv_pixel(1, 1, CV_8UC3); - Mat rgb_pixel(1, 1, CV_8UC3); + // Apply sepia-effect TS(sepia); - 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(luminance.at(row, col) * hsvScale_ + hsvOffset_); - hsv_pixel.ptr()[0] = 19; - hsv_pixel.ptr()[1] = 78; - - cvtColor(hsv_pixel, rgb_pixel, CV_HSV2RGB); - - retroFrame.at(row, col)[0] = rgb_pixel.ptr()[2]; - retroFrame.at(row, col)[1] = rgb_pixel.ptr()[1]; - retroFrame.at(row, col)[2] = rgb_pixel.ptr()[0]; - } - } + Mat h(retroFrame.size(),CV_8UC1); + Mat s(retroFrame.size(),CV_8UC1); + Mat v(retroFrame.size(),CV_8UC1); + + h = Scalar(19); + s = Scalar(78); + v = retroFrame * hsvScale_ + hsvOffset_; + + std::vector array_to_merge; + array_to_merge.push_back(h); + array_to_merge.push_back(s); + array_to_merge.push_back(v); + + merge(array_to_merge,retroFrame); + cvtColor(retroFrame,retroFrame,COLOR_HSV2BGR); + + TE(sepia); - TE(sepia);*/ }