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..77ef135 100644 --- a/src/retro_filter.cpp +++ b/src/retro_filter.cpp @@ -36,47 +36,54 @@ RetroFilter::RetroFilter(const Parameters& params) : rng_(time(0)) void RetroFilter::applyToVideo(const Mat& frame, Mat& retroFrame) { + Mat gray; + frame.copyTo(retroFrame); + double beta = 0.5; + + TS(scratching); + cvtColor(frame,gray,COLOR_BGR2GRAY); + resize(params_.scratches, retroFrame, frame.size()); + addWeighted(gray, beta, retroFrame, beta, 0.0, retroFrame); + + TE(scratching); + + // Add fuzzy border 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); + TS(bording); - 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; - } - } - - // Add fuzzy border - Mat borderColor(params_.frameSize, CV_32FC1, Scalar::all(meanColor[0] * 1.5)); - alphaBlend(borderColor, luminance, params_.fuzzyBorder); + 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); - 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]; - } - } + + TS(sepia); + + 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); + }