Photo to colored dot patterns with OpenCV
来源:互联网 发布:oracle的端口号 编辑:程序博客网 时间:2024/06/07 23:30
I was trying to write the code to achieve the same photo effect with this Photoshop tricks. With only a few lines of code, I’m pretty amazed with the result. Given a photo, the code will turn the photo into a colored dot patterns as shown in Figure 1d.
In Photoshop, this is done by first applying the mosaic filter and create a new layer containing the white circles on black background. By multiplying the mosaic filtered layer and the new layer, we obtain the result image. In OpenCV, I write the code to mimic the steps.
The algorithm:
- Load the source image.
- Apply pixelate effect by averaging NxN block neighborhood.
- Create circles mask.
- Multiply the pixelated image with the mask to obtain the final result.
Figure 1. The steps for creating colored dot patterns.
(a) The source image. (b) The pixelated image. (c) The mask. (d) The final result.
Here is the full code.
// im2dots.cpp// OpenCV code to achieve similar photo effect as shown in // http://photoshopessentials.com/photo-effects/color-dots/#include <opencv2/imgproc/imgproc.hpp>#include <opencv2/highgui/highgui.hpp>int main(){ cv::Mat src = cv::imread("image.jpg"); if (!src.data) return -1; cv::Mat dst = cv::Mat::zeros(src.size(), CV_8UC3); cv::Mat cir = cv::Mat::zeros(src.size(), CV_8UC1); int bsize = 10; for (int i = 0; i < src.rows; i += bsize) { for (int j = 0; j < src.cols; j += bsize) { cv::Rect rect = cv::Rect(j, i, bsize, bsize) & cv::Rect(0, 0, src.cols, src.rows); cv::Mat sub_dst(dst, rect); sub_dst.setTo(cv::mean(src(rect))); cv::circle( cir, cv::Point(j+bsize/2, i+bsize/2), bsize/2-1, CV_RGB(255,255,255), -1, CV_AA ); } } cv::Mat cir_32f; cir.convertTo(cir_32f, CV_32F); cv::normalize(cir_32f, cir_32f, 0, 1, cv::NORM_MINMAX); cv::Mat dst_32f; dst.convertTo(dst_32f, CV_32F); std::vector<cv::Mat> channels; cv::split(dst_32f, channels); for (int i = 0; i < channels.size(); ++i) channels[i] = channels[i].mul(cir_32f); cv::merge(channels, dst_32f); dst_32f.convertTo(dst, CV_8U); cv::imshow("dst", dst); cv::waitKey(); return 0;}
One thing to note is, when mutiplying the circles mask with the pixelated image both images need to be converted to the floating-point numbers e.g: CV_32F or CV_64F.
Below are some results using the code above.
Figure 2. The photo of my wife.
Figure 3. Mariko shinoda of AKB48.
Figure 4. Band of Horses, courtesy of NRKP3 on Flickr.
Figure 5. Band of Horses, courtesy of derekskey on Flickr.
Perhaps the code still needs some improvements. But in general I’m quite happy with the results. Tell me what you think in the comments below.
- Photo to colored dot patterns with OpenCV
- Introduction to programming with OpenCV
- Introduction to programming with OpenCV
- Introduction to programming with OpenCV
- Introduction to programming with OpenCV
- A Game with Colored Balls
- How to write GIMP plugin with OpenCV
- A guide to connecting Matlab with OpenCV
- 522B. Photo to Remember
- 【opencv】,【matlab】How to connect Matlab with OpenCV on Windows7
- 【OpenCV】How to Use Tesseract with C++ or OpenCV
- Path Patterns 和 Patterns with Placeholders
- An Introduction to Design Patterns in C++ with Qt 4 (Bruce Perens Open Source)
- POJ 3476--A Game with Colored Balls
- IPHONE Swipe Effect with Photo Gallery
- Creating photo album with jQuery Chop Slider
- Refactoring to Patterns
- Refactoring to Patterns
- Java 获取可用 UDP 端口号的方法
- TCP+UDP区别&&TCP三次握手理解
- Microsoft Techdays Paris - Day Three
- 关于ABAP调试中的F5,F6,F7,F8的区别和用法
- 在树莓派Debian系统命令行下截图
- Photo to colored dot patterns with OpenCV
- 项目进度
- NSMutableArray的相关方法
- Uva 10025 - The ? 1 ? 2 ? ... ? n = k problem
- 【自己动手写数据结构】 -- 简单顺序栈的实现
- 《渗透测试实践指南》读书笔记之信息搜集
- Qt编写串口通信程序全程图文讲解
- 安卓系统文件夹及其文件解析
- AsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);