OpenCV——Perlin Noise
// 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"
#include "math.h"
using namespace std;
using namespace cv;
void Show_Image(Mat&, const string &);
#endif // PS_ALGORITHM_H_INCLUDED
/*
perlin noise.
*/
#include "PS_Algorithm.h"
#include <time.h>
using namespace std;
using namespace cv;
void Generate_smoothnoise(Mat& src, Mat& std, int octave);
float Cosine_Interpolate(float x1,float x2,float alpha);
#define pi 3.1415926
int main()
{
string Img_name("4.jpg");
Mat Img;
Img=imread(Img_name);
Mat Cloud(Img.size(), CV_32FC1);
Mat Cloud_Temp(Img.size(), CV_32FC1);
Mat Base_Noise(Img.size(), CV_32FC1);
cv::randn(Base_Noise, 0.5, 0.25);
// Show_Image(Base_Noise, "N1");
float persistance = 0.8;
float totalAmplitude = 0.0;
float amplitude;
int octaveCount=8;
for (int i=0; i<octaveCount; i++)
{
amplitude=std::pow(persistance,(octaveCount-i));
totalAmplitude=totalAmplitude+amplitude;
Generate_smoothnoise(Base_Noise, Cloud_Temp, i);
Cloud=Cloud+Cloud_Temp*amplitude;
}
Cloud=Cloud/totalAmplitude;
Show_Image(Cloud, "out.jpg");
imwrite("Out.jpg", Cloud*255);
waitKey();
}
void Generate_smoothnoise(Mat& src, Mat& dst, int octave)
{
src.copyTo(dst);
int width=src.cols;
int height=src.rows;
float samplePeriod=pow(2,octave);
float sampleFrequency=1/samplePeriod;
int sample_i0, sample_i1;
float vertical_blend, horizontal_blend;
int sample_j0, sample_j1;
float top, bottom;
for (int i=0; i<height-1; i++)
{
sample_i0=(int)(i/samplePeriod)*samplePeriod;
sample_i1=(int)(sample_i0+samplePeriod)%height;
vertical_blend = (i - sample_i0) * sampleFrequency;
for (int j=0; j<width-1; j++)
{
sample_j0 = (int)(j / samplePeriod) * samplePeriod;
sample_j1 = (int)(sample_j0 + samplePeriod)% width;
horizontal_blend = (j - sample_j0) * sampleFrequency;
if (sample_i0<0) sample_i0=0;
if (sample_j0<0) sample_j0=0;
if (sample_i1<0) sample_i1=0;
if (sample_j1<0) sample_j1=0;
// blend the top two corners
top = Cosine_Interpolate(src.at<float>(sample_i0,sample_j0),
src.at<float>(sample_i0,sample_j1), horizontal_blend);
// blend the bottom two corners
bottom = Cosine_Interpolate(src.at<float>(sample_i1,sample_j0),
src.at<float>(sample_i1,sample_j1), horizontal_blend);
// final blend
dst.at<float>(i,j) = Cosine_Interpolate(top, bottom, vertical_blend);
}
}
}
float Cosine_Interpolate(float x1,float x2,float alpha)
{
float ft, f;
float y;
ft = alpha * pi;
f = (1 - cos(ft)) * .5;
y=x1*(1-f)+x2*f;
return y;
}
// 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);
}
原图
效果图
OpenCV——Perlin Noise的更多相关文章
- Perlin Noise 及其应用
Perlin Noise 可以用来表现自然界中无法用简单形状来表达的物体的形态,比如火焰.烟雾.表面纹路等.要生成 Perlin Noise 可以使用工具离线生成,也可以使用代码运行时生成.最简单常用 ...
- 【Ray Tracing The Next Week 超详解】 光线追踪2-4 Perlin noise
Preface 为了得到更好的纹理,很多人采用各种形式的柏林噪声(该命名来自于发明人 Ken Perlin) 柏林噪声是一种比较模糊的白噪声的东西:(引用书中一张图) 柏林噪声是用来生成一些看似杂乱 ...
- python perlin noise
python 利用 noise 生成纹理. # -*- coding: utf-8 -*- """ Created on Mon Apr 23 20:04:41 2018 ...
- 利用perlin noise 生成 wood texture
%%% Perlin Noise %%% Wood_texture clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image P ...
- OpenCV - Add Noise的一些方法
噪声常用有两种:一种椒盐噪声,一种高斯噪声. import numpy as np def pepper_and_salt(src, proportion): """ : ...
- 利用Perlin nosie 完毕(PS 滤镜—— 分成云彩)
%%%% Cloud %%%% 利用perlin noise生成云彩 clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image ...
- 利用Perlin nosie 完成(PS 滤镜—— 分成云彩)
%%%% Cloud %%%% 利用perlin noise生成云彩 clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image ...
- GraphicsLab Project 之 Curl Noise
作者:i_dovelemon 日期:2020-04-25 主题:Perlin Noise, Curl Noise, Finite Difference Method 引言 最近在研究流体效果相关的模拟 ...
- python 不同版本下载资源
Unofficial Windows Binaries for Python Extension Packages by Christoph Gohlke, Laboratory for Fluore ...
随机推荐
- leetcode 题解 || Remove Nth Node From End of List 问题
problem: Given a linked list, remove the nth node from the end of list and return its head. For exam ...
- mysql创建还原点
set autocommit = 0; insert into t1(name) values ("user1"); savepoint p1; insert into t ...
- 我的Android进阶之旅------>Android中android:windowSoftInputMode的使用方法
面试题:怎样在显示某个Activity时马上弹出软键盘? 答案:在AndroidManifest.xml文件里设置<activity>标签的android:windowSoftInputM ...
- [oracle]pl/sql --分页过程demo
这句sql能够用来查询一张表中的特定位置的记录 --查询的方法获取分页的语句 select *from (select t1.*,rownum rn from (select *from books) ...
- Linux 中权限控制实例
前言 前文对 Linux 中的权限进行了较为透彻的分析.而本文,则在前文的基础上,具体说明如何在代码中进行权限控制. 下面的代码涉及到以下几个方面: 1. 创建文件时设置文件权限 2. 修改文件的默认 ...
- cocos2d-x lua 中使用protobuf并对http进行处理
cocos2d-x lua 中使用protobuf并对http进行处理 本文介绍 cocos2d-x lua 中使用http 和 基于cocos2d-x 对lua http的封装(部分ok) 本博客链 ...
- javascript正则表达式提取子匹配项
C#里所用的正则表达式,如果要提取字符串里的子匹配项(我都不知道那个叫啥名字,别名?)是很方便的,比如: Regex rx = new Regex(@"<title>(?< ...
- hdu 1542&&poj 1151 Atlantis[线段树+扫描线求矩形面积的并]
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- hdu5258简单枚举
百度之星复赛第一题.不明白这么水的题为何一堆人没过...这些人是咋晋级复赛的呢... /* * Author : ben */ #include <cstdio> #include < ...
- file descriptor 0 1 2 一切皆文件 stdout stderr stdin /dev/null 沉默是金 pipes
$>emtry_or_create_a_file.f $ll>>append_a_file.f standard output input error $ls -l /usr/bin ...