【问题标题】:How to map an image of 2048 * 2048?如何映射2048 * 2048的图像?
【发布时间】:2017-11-18 16:54:20
【问题描述】:

我正在 Cuda C 中进行分形,我已经为 1024 * 1024 的图像编写了程序,但我想要 2048 * 2048 的更大图像,我对图像映射如何帮助我有疑问 附件我的两个编码 1024 * 1024 和我要做什么

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <cuda.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctime>
#define MAX_ITER 5000
#define N 1024
#define BLOCKS 32
#define THREAD 1
using namespace cv;
using namespace std;
__global__ void mul(unsigned char *imagen){
int i=blockIdx.y*gridDim.x+blockIdx.x;
int j=threadIdx.y*blockDim.x+threadIdx.x;   
    double x,y,a,b,xnew,ynew,sq;
    double iter;
    iter=0;
        x=0;
        y=0;
        a=((3.0/(N))*j-2);
        b=((2.0/(N))*i-1);
        sq=abs(sqrt(pow(x,2)+pow(y,2)));
        while((sq<2)&&(iter<MAX_ITER))
        {
            xnew=((x*x)-(y*y))+a;
            ynew=(2*x*y)+b;
    x=xnew;
            y=ynew;       
    sq=abs(sqrt(pow(x,2)+pow(y,2)));
            iter=iter+1;        
        }
        if(iter==MAX_ITER)
        {
            imagen[i*(N)+j]=255;
        }
        else
        {
            imagen[i*(N)+j]=0;
        }
}
int main(){
dim3 bloques (32,32);
dim3 threads(32,32);
unsigned char *matriz_a;
unsigned char *matriz_dev_a;

matriz_a = (unsigned char *)malloc(sizeof(unsigned char) * N*N);
cudaMalloc((void **)&matriz_dev_a, N*N*sizeof(unsigned char));
cudaMemcpy(matriz_dev_a, matriz_a, sizeof(unsigned char) *N*N, cudaMemcpyHostToDevice);
/**************************************************************/
mul<<<bloques, threads>>>(matriz_dev_a);
cudaMemcpy(matriz_a, matriz_dev_a, sizeof(unsigned char) *N*N, cudaMemcpyDeviceToHost);
/**************************************************************************/
/************************************************************************/
/***********************************************************************/
const cv::Mat img(cv::Size(N, N), CV_8U, matriz_a);
cv::namedWindow("foobar");
cv::imshow("foobar", img);
cv::waitKey(0);
free(matriz_a);
cudaFree(matriz_dev_a);
}

很好的映射,例如只更改几行

#define N 2048
dim3 bloques (45,45);
mul<<<bloques, 1>>>(matriz_dev_a);

考虑在每个块中发送一个线程,但是当运行时不执行任何操作时,我需要花一点时间来考虑映射可能是什么样的。 对不起我的英语不好 晚上好,我还是希望能说声谢谢

【问题讨论】:

    标签: c++ opencv image-processing cuda


    【解决方案1】:

    当前代码存在两个问题。

    1. 代码不可扩展,因为块数是固定的。
    2. 内核中的索引不正确。全局索引 j 不会随着区块数量的变化而扩展。

    问题可以解决如下:

    使块数动态化,即取决于输入数据大小:

    dim3 threads(32,32);
    dim3 bloques;
    bloques.x = (N + threads.x - 1)/threads.x;
    bloques.y = (N + threads.y - 1)/threads.y;
    

    标准化内核内部的索引:

    int i= blockIdx.y * blockDim.y + threadIdx.y;
    int j= blockIdx.x * blockDim.x + threadIdx.x;
    

    修改后的代码在分形大小 2048 x 2048 上运行良好。

    【讨论】:

    • 哦,谢谢,我想我要多学习,我会努力的,谢谢!!
    猜你喜欢
    • 1970-01-01
    • 2015-02-03
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 2020-02-12
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    相关资源
    最近更新 更多