国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

CUDA+OpenCV 繪制朱利亞(Julia)集合圖形

2019-11-14 12:23:07
字體:
來源:轉載
供稿:網友

Julia集中的元素都是經過簡單的迭代計算得到的,很適合用CUDA進行加速。對一個600*600的圖像,需要進行360000次迭代計算,所以在CUDA中創建了600*600個線程塊(block),每個線程塊包含1個線程,并行執行360000次運行,圖像的創建和顯示通過OpenCV實現:

#include "cuda_runtime.h"#include <highgui.hpp>using namespace cv;#define DIM 600   //圖像長寬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)(DIM / 2 - x) / (DIM / 2);	float jy = scale * (float)(DIM / 2 - y) / (DIM / 2);	cuComplex c(0.25, 0.010);	cuComplex a(jx, jy);	int i = 0;	for (i = 0; i < 200; i++)	{		a = a * a + c;		if (a.magnitude2() > 1000)			return 0;	}	return 1;}__global__ void kernel(unsigned char *ptr){	// map from blockIdx to pixel position	int x = blockIdx.x;	int y = blockIdx.y;	int offset = x + y * gridDim.x;	// now calculate the value at that position	int juliaValue = julia(x, y);	ptr[offset * 3 + 0] = 0;	ptr[offset * 3 + 1] = 0;	ptr[offset * 3 + 2] = 255 * juliaValue;}// globals needed by the update routinestruct DataBlock{	unsigned char   *dev_bitmap;};int main(void){	DataBlock   data;	cudaError_t error;	Mat image = Mat(DIM, DIM, CV_8UC3, Scalar::all(0));	data.dev_bitmap = image.data;	unsigned char    *dev_bitmap;	error = cudaMalloc((void**)&dev_bitmap, 3 * image.cols*image.rows);	data.dev_bitmap = dev_bitmap;	dim3    grid(DIM, DIM);	kernel << <grid, 1 >> > (dev_bitmap);	error = cudaMemcpy(image.data, dev_bitmap,		3 * image.cols*image.rows,		cudaMemcpyDeviceToHost);	error = cudaFree(dev_bitmap);	imshow("CUDA For Julia | c(0.25, 0.010)", image);	waitKey();}

c(-0.8,0.156):

c(-0.85,0.06):

c(-0.305,0.60):

c(0.25,0.010):


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 崇阳县| 志丹县| 邹城市| 呼和浩特市| 古田县| 调兵山市| 招远市| 泗阳县| 普洱| 永春县| 林口县| 庆阳市| 青龙| 姜堰市| 河池市| 英吉沙县| 阿拉善盟| 元阳县| 哈尔滨市| 土默特右旗| 安福县| 九龙坡区| 阳山县| 志丹县| 运城市| 芜湖市| 广平县| 衡山县| 达州市| 尚义县| 长春市| 富阳市| 侯马市| 太谷县| 石林| 山东省| 青州市| 炉霍县| 新竹县| 阿城市| 新兴县|