-1

我只是想把test.jpg放到纹理内存中,然后通过text2D()来显示图片,结果却很奇怪。“result”应该和“gray”一样。点击获取图片:window:graywindow:result。“gray”被缩小到原来的四分之一,并排成一排。

这是我正在使用的代码

 #include <cuda_runtime.h>   
 #include <highgui/highgui.hpp>  
 #include <imgproc/imgproc.hpp>  
 #include "device_launch_parameters.h"
 #include <iostream>

using namespace std;
using namespace cv;   

texture<float, 2, cudaReadModeElementType> texRef;  

__global__ void transformKernel(uchar* output, int width, int height)  
{  
    unsigned int x = blockIdx.x * blockDim.x + threadIdx.x;  
    unsigned int y = blockIdx.y * blockDim.y + threadIdx.y;  
    float u = x / (float)width;  
    float v = y / (float)height;  

    output[(y * width + x)] = tex2D(texRef, x, y);
}  

int main()  
{  

    Mat image = imread("test.jpg", 0);
    int width=512;
    int height=512;
    resize(image, image, Size(width, height));  
    imshow("gray", image);  

    cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat);

    cudaArray* cuArray;  
    cudaMallocArray(&cuArray, &channelDesc, width, height);  
    cudaMemcpyToArray(cuArray, 0, 0, image.data, sizeof(uchar)*width*height, cudaMemcpyHostToDevice);  

    texRef.addressMode[0] = cudaAddressModeWrap; 
    texRef.addressMode[1] = cudaAddressModeWrap;  
    texRef.filterMode = cudaFilterModeLinear;   
    texRef.normalized = false;                        
    cudaBindTextureToArray(texRef, cuArray, channelDesc);  

    Mat imageOutput = Mat(Size(width, height), CV_8UC1);  
    uchar * output = imageOutput.data;  

    cudaMalloc((void**)&output, width * height * sizeof(float));  

    dim3 dimBlock(16, 16);  
    dim3 dimGrid((width + dimBlock.x - 1) / dimBlock.x, (height + dimBlock.y - 1) / dimBlock.y);  

    transformKernel << <dimGrid, dimBlock >> > (output, width, height);  

    cudaMemcpy(imageOutput.data, output, height*width, cudaMemcpyDeviceToHost);  

    imshow("result", imageOutput);  
    waitKey();  

    cudaFreeArray(cuArray);  
    cudaFree(output);  
}  

谁能告诉我为什么?

4

1 回答 1

1
#include <cuda_runtime.h>   
#include <highgui/highgui.hpp>  
#include <imgproc/imgproc.hpp>  
#include "device_launch_parameters.h"
#include <iostream>


using namespace std;
using namespace cv;   

texture<uchar, 2, cudaReadModeElementType> texRef; 

__global__ void transformKernel(uchar* output, int width, int height)  
{  
    unsigned int x = blockIdx.x * blockDim.x + threadIdx.x;  
    unsigned int y = blockIdx.y * blockDim.y + threadIdx.y;  
    float u = x / (float)width;  
    float v = y / (float)height;  

    output[(y * width + x)] = tex2D(texRef, x, y);
}  

int main()  
{  

    Mat image = imread("test.jpg", 0);
    int width=512;
    int height=512;
    resize(image, image, Size(width, height));  
    imshow("gray", image);  

    cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(8, 0, 0, 0, cudaChannelFormatKindUnsigned);

    cudaArray* cuArray;  
    cudaMallocArray(&cuArray, &channelDesc, width, height);  

    cudaMemcpyToArray(cuArray, 0, 0, image.data, sizeof(uchar)*width*height, cudaMemcpyHostToDevice);  
    texRef.addressMode[0] = cudaAddressModeWrap; 
    texRef.addressMode[1] = cudaAddressModeWrap;  
    texRef.addressMode[2] = cudaAddressModeWrap;  
    texRef.filterMode = cudaFilterModePoint;
    texRef.normalized = false;           

    cudaBindTextureToArray(texRef, cuArray, channelDesc);  

    Mat imageOutput = Mat(Size(width, height), CV_8UC1);  
    uchar * output = imageOutput.data;  

    cudaMalloc((void**)&output, width * height * sizeof(uchar));  

    dim3 dimBlock(16, 16);  
    dim3 dimGrid((width + dimBlock.x - 1) / dimBlock.x, (height + dimBlock.y - 1) / dimBlock.y);  

    transformKernel << <dimGrid, dimBlock >> > (output, width, height);  

    cudaMemcpy(imageOutput.data, output, height*width* sizeof(uchar), cudaMemcpyDeviceToHost);  

    imshow("result", imageOutput);  
    waitKey();  

    cudaFreeArray(cuArray);  
    cudaFree(output);  

}  

只需将“浮动”更改为“uchar”

于 2017-07-14T01:20:51.817 回答