CPU和GPU实现julia
# include <iostream >
# include "opencv2/core/core.hpp"
# include "opencv2/highgui/highgui.hpp"
# include "opencv2/imgproc/imgproc.hpp"
using namespace std;
using namespace cv;
# define DIM 512
struct cuComplex
{
float r;
float i;
cuComplex( float a, float b) :r(a),i(b){}
float magnitude2( void){ return r *r +i *i;}
cuComplex operator *( const cuComplex & a)
{
return cuComplex(r *a.r -i *a.i,i *a.r +r *a.i);
}
cuComplex operator +( const cuComplex & a)
{
return cuComplex(r +a.r,i +a.i);
}
};
int julia( int x, int y)
{
const float scale = 1. 5;
float jx = scale *( float)(DIM / 2 - x) /(DIM / 2);
float jy = scale *( float)(DIM / 2 - y) /(DIM / 2);
cuComplex c( - 0. 8, 0. 156);
cuComplex a(jx,jy);
for ( int i = 0;i < 200;i ++)
{
a =a *a +c;
if (a.magnitude2() > 1000)
{
return 0;
}
}
return 1;
}
int _tmain( int argc, _TCHAR * argv[])
{
Mat src = Mat(DIM,DIM,CV_8UC3); //创建画布
for ( int x = 0;x <src.rows;x ++)
{
for ( int y = 0;y <src.cols;y ++)
{
for ( int c = 0;c < 3;c ++)
{
src.at <Vec3b >(x,y)[c] =julia(x,y) * 255;
}
}
}
imshow( "src",src);
waitKey();
return 0;
}
# include "stdafx.h"
# include <iostream >
# include "opencv2/core/core.hpp"
# include "opencv2/highgui/highgui.hpp"
# include "opencv2/imgproc/imgproc.hpp"
# include <stdio.h >
# include <assert.h >
# include <cuda_runtime.h >
# include <helper_functions.h >
# include <helper_cuda.h >
using namespace std;
using namespace cv;
# define N 250
//test1的kernel
__global__ void test1kernel( int *t)
{
int x = blockIdx.x;
int y = blockIdx.y;
int offset = x +y *gridDim.x;
t[offset] = 255 -t[offset];
}
int main( void)
{
//step0.数据和内存初始化
Mat src = imread( "opencv-logo.png", 0);
resize(src,src,Size(N,N));
int *dev_t;
int t[N *N];
Mat dst = Mat(N,N,CV_8UC3);
for ( int i = 0;i <N *N;i ++)
{
t[i] =( int)src.at < char >(i /N,i %N);
}
checkCudaErrors(cudaMalloc(( void * *) &dev_t, sizeof( int) *N *N));
//step1.由cpu向gpu中导入数据
checkCudaErrors(cudaMemcpy(dev_t, t, sizeof( int) *N *N, cudaMemcpyHostToDevice));
//step2.gpu运算
dim3 grid(N,N);
test1kernel << <grid, 1 >> >(dev_t);
//step3.由gpu向cpu中传输数据
checkCudaErrors(cudaMemcpy(t, dev_t, sizeof( int) *N *N, cudaMemcpyDeviceToHost));
//step4.显示结果
for ( int i = 0;i <N;i ++)
{
for ( int j = 0;j <N;j ++)
{
int offset = i *N +j;
for ( int c = 0;c < 3;c ++)
{
dst.at <Vec3b >(i,j)[c] =t[offset];
}
}
}
//step5,释放资源
checkCudaErrors(cudaFree(dev_t));
imshow( "dst",dst);
waitKey();
return 0;
}
# include "stdafx.h"
# include <iostream >
# include "opencv2/core/core.hpp"
# include "opencv2/highgui/highgui.hpp"
# include "opencv2/imgproc/imgproc.hpp"
# include <stdio.h >
# include <assert.h >
# include <cuda_runtime.h >
# include <helper_functions.h >
# include <helper_cuda.h >
using namespace std;
using namespace cv;
# define N 250
struct cuComplex
{
float r;
float i;
__device__ cuComplex( float a, float b) : r(a),i(b){}
__device__ float magnitude2( void)
{
return r *r +i *i;
}
__device__ cuComplex operator *( const cuComplex & a)
{
return cuComplex(r *a.r - i *a.i,i *a.r + r *a.i);
}
__device__ cuComplex operator +( const cuComplex & a)
{
return cuComplex(r +a.r,i +a.i);
}
};
__device__ int julia( int x, int y)
{
const float scale = 1. 5;
float jx = scale *( float)(N / 2 - x) /(N / 2);
float jy = scale *( float)(N / 2 - y) /(N / 2);
cuComplex c( - 0. 8, 0. 156);
cuComplex a(jx,jy);
for ( int i = 0;i < 200;i ++)
{
a =a *a +c;
if (a.magnitude2() > 1000)
{
return 0;
}
}
return 1;
}
__device__ int fblx( int offset)
{
if (offset == 0 || offset == 1)
{
return offset;
}
else
{
return(fblx(offset - 1) +fblx(offset - 2));
}
}
//test3的kernel
__global__ void juliakernel( int *t)
{
int x = blockIdx.x;
int y = blockIdx.y;
int offset = x +y *gridDim.x;
int juliaValue = julia(x,y);
t[offset] =juliaValue * 255;
}
int main( void)
{
//step0.数据和内存初始化
int *dev_t;
int t[N *N];
Mat dst = Mat(N,N,CV_8UC3);
for ( int i = 0;i <N *N;i ++)
{
t[i] = 0;
}
checkCudaErrors(cudaMalloc(( void * *) &dev_t, sizeof( int) *N *N));
//step1.由cpu向gpu中导入数据
checkCudaErrors(cudaMemcpy(dev_t, t, sizeof( int) *N *N, cudaMemcpyHostToDevice));
//step2.gpu运算
dim3 grid(N,N);
juliakernel << <grid, 1 >> >(dev_t);
//step3.由gpu向cpu中传输数据
checkCudaErrors(cudaMemcpy(t, dev_t, sizeof( int) *N *N, cudaMemcpyDeviceToHost));
//step4.显示结果
for ( int i = 0;i <N;i ++)
{
for ( int j = 0;j <N;j ++)
{
int offset = i *N +j;
printf( "%d is %d",offset,t[offset]);
for ( int c = 0;c < 3;c ++)
{
dst.at <Vec3b >(i,j)[c] =t[offset];
}
}
}
//step5,释放资源
checkCudaErrors(cudaFree(dev_t));
imshow( "dst",dst);
waitKey();
return 0;
}

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
验证码识别--type7
验证码识别--type7 一、干扰分析 有黑色边框,然后点干扰,线干扰 去边框 去点干扰 变成这样的结果,方便运算吗?也可以多种方式联合起来运算的。我相信在很多情况下,都可能会遇到类似的结果。我们人类能够识别这样的结果,是通过一个复杂的识别的过程,那么机器如何来识别?一定有一些方法套路在里面。 通过分析,还是在联通区域上面做文章,想办法做出了这样的效果 这个效果,相对来说,去除的更多了一点,比较明显的是干扰线被去除了,然后再做一次 blob分析,去除孤立的 blob 这里我采取的是contours分析的方法,多次使用,非常熟悉 达到这样的效果,此时,干扰点都已经去除了,可以来做分割。采用的方法还是投影分割. 下面就是识别的问题了 二、识别 三、小结 本例较多地运用了字符级别的识别,特别是去除噪音、去除干扰线这个部分,还是有许多值得研究的东西。 来自为知笔记(Wiz) 目前方向:图像拼接融合、图像识别 联系方式:jsxyhelu@foxmail.com
- 下一篇
基本形态学算法
基本形态学算法 为什么要做基本形态学算法的研究和实现?是因为形态学是一个非常有力,应用 广泛的工具,但同时也是研究不是很清楚的工具。往往一个恰到好处的变换,就能够省下许多的劳动。对此的分类和研究就显得非常有必要,而相关代码的积累,也很有价值。 零、基本概念: 膨胀:白->黑; dilate 腐蚀:黑->白; erode 开 : 腐蚀->膨胀 平滑物体轮廓,断开较窄的地方,清除细突; 闭 :膨胀->腐蚀 弥合较窄的间断,填补细长的沟壑; 顶帽 :src-(开运算(src)) 底帽 :闭运算(src)-src 一、边界提取 先腐蚀,然后相减。 作为生成算法可能价值不是很大,但是在生成镂空字符的时候,价值就会比较大。 变成 以及 //-------------------生成验证码--------------// void creatCode() { Matedge; Matcode = imread( "abcd123.bmp" , 0 ); threshold(code,code, 0 , 255 ,THRESH_OTSU); threshold(code,...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS8安装Docker,最新的服务器搭配容器使用
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- Hadoop3单机部署,实现最简伪集群
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- Linux系统CentOS6、CentOS7手动修改IP地址
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- Windows10,CentOS7,CentOS8安装Nodejs环境
- 设置Eclipse缩进为4个空格,增强代码规范
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题