我只是想把test.jpg放到纹理内存中,然后通过text2D()来显示图片,结果却很奇怪。“result”应该和“gray”一样。点击获取图片:window:gray和window: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);
}
谁能告诉我为什么?