Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions sample/retro_sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,30 @@ int main(int argc, const char** argv)
}

Mat frame;
capture >> frame;

if (frame.empty())
const int maxAttemps = 5; // attempt to get first non-empty frame
int attemptIdx = 0;
while (frame.empty() && attemptIdx < maxAttemps)
{
// empty video; lets consider this to be OK
return 0;
capture >> frame;
attemptIdx++;
}
if (attemptIdx == maxAttemps)
{
cout << "Error: empty first frames." << endl;
return 1;
}


params.frameSize = frame.size();
RetroFilter filter(params);

for(;;)
{
if(frame.empty())
{
capture >> frame;
continue;
}
Mat retroFrame;
TS(filter);
filter.applyToVideo(frame, retroFrame);
Expand All @@ -110,7 +121,6 @@ int main(int argc, const char** argv)
break;

capture >> frame;
if(frame.empty()) break;
}

return 0;
Expand Down
37 changes: 17 additions & 20 deletions src/retro_filter.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "retro_filter.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <time.h>

using namespace std;
using namespace cv;
Expand Down Expand Up @@ -36,46 +37,42 @@ RetroFilter::RetroFilter(const Parameters& params) : rng_(time(0))
void RetroFilter::applyToVideo(const Mat& frame, Mat& retroFrame)
{
int col, row;
Mat luminance;
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);
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 (row = 0; row < luminance.size().height; row++)
{
for (col = 0; col < luminance.size().width; col += 1)
for (col = 0; col < luminance.size().width; col++)
{
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;
luminance.at<uchar>(row, col) = params_.scratches.at<uchar>(row + y, col + x) ? (int)scratchColor.at<uchar>(row, col) : luminance.at<uchar>(row, col);
}
}



// 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 (col = 0; col < luminance.size().width; col++)
{
for (row = 0; row < luminance.size().height; row += 1)
for (row = 0; row < luminance.size().height; row++)
{
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];
retroFrame.at<Vec3b>(row, col)[0] = 19;
retroFrame.at<Vec3b>(row, col)[1] = 78;
retroFrame.at<Vec3b>(row, col)[2] = cv::saturate_cast<uchar>(luminance.at<uchar>(row, col) * hsvScale_ + hsvOffset_);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Используйте функцию merge, чтобы уйти от попиксельных операций.

}
}

cvtColor(retroFrame, retroFrame, CV_HSV2BGR);

}