Skip to content

Instantly share code, notes, and snippets.

@yoggy
Last active January 1, 2016 07:19
Show Gist options
  • Save yoggy/8111255 to your computer and use it in GitHub Desktop.
Save yoggy/8111255 to your computer and use it in GitHub Desktop.
#pragma once
#include <SDKDDKVer.h>
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <vector>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#ifdef _DEBUG
#pragma comment(lib, "opencv_core247d.lib")
#pragma comment(lib, "opencv_imgproc247d.lib")
#pragma comment(lib, "opencv_highgui247d.lib")
#else
#pragma comment(lib, "opencv_core247.lib")
#pragma comment(lib, "opencv_imgproc247.lib")
#pragma comment(lib, "opencv_highgui247.lib")
#endif
//
// see also... https://github.com/nkint/OF_sketches/blob/master/Posterize/src/Posterizer.cpp
//
cv::Mat segment(cv::Mat src, const int &k_size) {
int cols = src.cols;
int rows = src.rows;
int numpix = cols*rows;
cv::Mat clustered, colored, bestLabels, centers, samples;
int *colors= new int[k_size];
for(int i = 0;i < k_size;i++){
colors[i] = 255 / (i + 1);
}
src.convertTo(samples, CV_32FC3);
samples = samples.reshape(3, samples.rows*samples.cols);
cv::kmeans(samples, k_size, bestLabels,
cv::TermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0),
1, cv::KMEANS_PP_CENTERS, centers);
clustered = cv::Mat(rows, cols, CV_32F);
for(int i = 0;i < numpix;i++){
int label = bestLabels.at<int>(0, i);
clustered.at<float>(i / cols, i % cols) = (float)((colors[label]));
}
clustered.convertTo(clustered, CV_8U);
delete colors;
colors = NULL;
return clustered;
}
cv::Mat posterize(cv::Mat &src, const int &k_size)
{
cv::Mat blurred_img;
cv::blur(src, blurred_img, cv::Size(16, 16));
cv::Mat clustered_img = segment(blurred_img, k_size);
cv::erode(clustered_img, clustered_img, cv::Mat());
cv::dilate(clustered_img, clustered_img, cv::Mat());
cv::Mat result_img = cv::Mat(src.rows, src.cols, CV_8UC3);
for(int i = 0;i < k_size;i++){
cv::Mat mask;
cv::Scalar meanColor;
int color = 255 / (i + 1);
cv::inRange(clustered_img, color - 1, color + 1, mask);
meanColor = cv::mean(src, mask);
if(src.channels() == 1) {
meanColor = cv::Scalar(meanColor[0], meanColor[0], meanColor[0]);
}
result_img.setTo(meanColor, mask);
}
return result_img;
}
int main(int argc, char* argv[])
{
cv::VideoCapture capture;
if (capture.open(0) == false) {
printf("error: capture.open() failed...\n");
return -1;
}
cv::Rect roi(160, 120, 320, 240);
cv::Mat capture_img, scaled_img, postalization_img, result_img;
while(true) {
capture.grab();
capture >> capture_img;
cv::Mat roi_img = capture_img(roi);
cv::Mat scaled_img;
cv::resize(roi_img, scaled_img, cv::Size(roi_img.cols/4, roi_img.rows/4));
postalization_img = posterize(scaled_img, 16);
cv::resize(postalization_img, result_img, cv::Size(postalization_img.cols*2, postalization_img.rows*2));
// draw
cv::rectangle(capture_img, roi, CV_RGB(0,255,0), 2);
cv::imshow("capture_img", capture_img);
cv::imshow("result_img", result_img);
int c = cv::waitKey(1);
if (c == 27) break;
}
cv::destroyAllWindows();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment