国产探花免费观看_亚洲丰满少妇自慰呻吟_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):


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 靖宇县| 兴仁县| 宝应县| 雅安市| 涪陵区| 北碚区| 临沭县| 晴隆县| 盐源县| 阿荣旗| 利川市| 麟游县| 太湖县| 波密县| 乐东| 南京市| 那坡县| 城市| 上饶县| 临泉县| 游戏| 潢川县| 曲沃县| 惠来县| 平阳县| 盐津县| 太保市| 亳州市| 石楼县| 平利县| 卓尼县| 曲阳县| 尚义县| 保山市| 新田县| 青田县| 雅安市| 峨眉山市| 左云县| 大冶市| 巨野县|