OpenCV——马赛克
2024-10-20 12:43:37
具体的算法可以参考:
// define head function
#ifndef PS_ALGORITHM_H_INCLUDED
#define PS_ALGORITHM_H_INCLUDED #include <iostream>
#include <string>
#include "cv.h"
#include "highgui.h"
#include "cxmat.hpp"
#include "cxcore.hpp" using namespace std;
using namespace cv; void Show_Image(Mat&, const string &); #endif // PS_ALGORITHM_H_INCLUDED /*
This program will generate
"Mosaic" effect. */ #include "PS_Algorithm.h"
#include <time.h> using namespace std;
using namespace cv; int main(void)
{
string Img_name("4.jpg");
Mat Image_in;
Image_in=imread(Img_name);
// Show_Image(Image_in, Img_name); Mat Image_out(Image_in.size(), CV_32FC3);
Image_in.convertTo(Image_out, CV_32FC3); int P_size=9;
float k1, k2; int n_row;
int n_col; float m, n; int h,w; n_row=Image_in.rows;
n_col=Image_in.cols; Mat sub_mat; srand( (unsigned)time(NULL) ); for (int i=P_size; i<Image_in.rows-P_size-1; i=i+P_size)
{
for (int j=P_size; j<Image_in.cols-1-P_size; j=j+P_size)
{
k1=(double)((rand() % 100))/100.0-0.5;
k2=(double)((rand() % 100))/100.0-0.5; m=(k1*(P_size*2-1));
n=(k2*(P_size*2-1)); h=(int)(i+m)% n_row;
w=(int)(j+n)% n_col; sub_mat=Image_out.operator()(Range(i-P_size,i+P_size), Range(j-P_size,j+P_size)); sub_mat.setTo(Scalar(Image_in.at<Vec3b>(h,w))); }
} Image_out=Image_out/255.0; Show_Image(Image_out, "out"); imwrite("out.jpg", Image_out*255); waitKey();
cout<<"All is well."<<endl; } // define the show image
#include "PS_Algorithm.h"
#include <iostream>
#include <string> using namespace std;
using namespace cv; void Show_Image(Mat& Image, const string& str)
{
namedWindow(str.c_str(),CV_WINDOW_AUTOSIZE);
imshow(str.c_str(), Image); }
原图
效果图
最新文章
- jira操作
- sklearn学习笔记1
- webpack入门指南(转载)
- android 入门-基础了解
- 录制简单的自动化测试工具SlikMobile初体验
- 使用代码创建AutoLayout约束
- 30道四则运算<;2>;
- secureCRT中文字符乱码
- Entity Framework关联实体的三种加载方法
- Lightbox 图片展示插件
- JAVA设计模式总结之六大设计原则
- JAVA第十次作业
- SQL Server进阶(十一)存储过程
- linux 权限管理
- ThreadPoolExecutor 入参 corePoolSize 和 maximumPoolSize 的联系
- Python—装饰器详解
- L236
- 好的API设计
- Spring管理过滤器:org.springframework.web.filter.DelegatingFilterProxy
- hdu 2121 Ice_cream’s world II