Created
February 16, 2018 14:53
-
-
Save insaneyilin/605106cac9df3cb1acbce6bb85528029 to your computer and use it in GitHub Desktop.
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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#include <string> | |
#include <iostream> | |
#include <opencv2/core/core.hpp> | |
#include <opencv2/highgui/highgui.hpp> | |
void color_reduce1(const cv::Mat& src_mat, int div, cv::Mat* dst_mat) { | |
*dst_mat = src_mat.clone(); | |
for (int r = 0; r < src_mat.rows; ++r) { | |
for (int c = 0; c < src_mat.cols; ++c) { | |
dst_mat->at<cv::Vec3b>(r, c)[0] = | |
src_mat.at<cv::Vec3b>(r, c)[0] / div * div + div / 2; | |
dst_mat->at<cv::Vec3b>(r, c)[1] = | |
src_mat.at<cv::Vec3b>(r, c)[1] / div * div + div / 2; | |
dst_mat->at<cv::Vec3b>(r, c)[2] = | |
src_mat.at<cv::Vec3b>(r, c)[2] / div * div + div / 2; | |
} | |
} | |
} | |
void color_reduce2(const cv::Mat& src_mat, int div, cv::Mat* dst_mat) { | |
*dst_mat = src_mat.clone(); | |
int num_rows = src_mat.rows; | |
int num_elems_per_row = src_mat.cols * src_mat.channels(); | |
for (int i = 0; i < num_rows; ++i) { | |
const uchar* in_data = src_mat.ptr<uchar>(i); | |
uchar* out_data = dst_mat->ptr<uchar>(i); | |
for (int j = 0; j < num_elems_per_row; ++j) { | |
out_data[j] = in_data[j] / div * div + div / 2; | |
} | |
} | |
} | |
int main(int argc, char** argv) { | |
srand(time(NULL)); | |
if (argc != 2) { | |
std::cerr << "Usage: color_reduce <img_dir>\n"; | |
return 1; | |
} | |
cv::Mat img = cv::imread(argv[1]); | |
cv::Mat img_out1; | |
cv::Mat img_out2; | |
int div = 64; | |
clock_t start, end; | |
start = clock(); | |
for (int i = 0; i < 1000; ++i) { | |
color_reduce1(img, div, &img_out1); | |
} | |
end = clock(); | |
std::cout << (1.0 * end - start) / CLK_TCK << std::endl; | |
start = clock(); | |
for (int i = 0; i < 1000; ++i) { | |
color_reduce2(img, div, &img_out2); | |
} | |
end = clock(); | |
std::cout << (1.0 * end - start) / CLK_TCK << std::endl; | |
cv::imshow("img", img); | |
cv::imshow("img_out1", img_out1); | |
cv::imshow("img_out2", img_out2); | |
cv::waitKey(0); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment