sol*_*les 5 c++ cuda image-processing convolution npp
Nvidia Performance Primitives(NPP)提供了nppiFilter将用户提供的图像与用户提供的内核进行卷积的功能.对于1D卷积内核,nppiFilter可以正常工作.但是,nppiFilter正在为2D内核生成垃圾图像.
我使用典型的Lena图像作为输入:

这是我使用1D卷积内核的实验,它可以产生良好的输出.
#include <npp.h> // provided in CUDA SDK
#include <ImagesCPU.h> // these image libraries are also in CUDA SDK
#include <ImagesNPP.h>
#include <ImageIO.h>
void test_nppiFilter()
{
npp::ImageCPU_8u_C1 oHostSrc;
npp::loadImage("Lena.pgm", oHostSrc);
npp::ImageNPP_8u_C1 oDeviceSrc(oHostSrc); // malloc and memcpy to GPU
NppiSize kernelSize = {3, 1}; // dimensions of convolution kernel (filter)
NppiSize oSizeROI = {oHostSrc.width() - kernelSize.width + 1, oHostSrc.height() - kernelSize.height + 1};
npp::ImageNPP_8u_C1 oDeviceDst(oSizeROI.width, oSizeROI.height); // allocate device image of appropriately reduced size
npp::ImageCPU_8u_C1 oHostDst(oDeviceDst.size());
NppiPoint oAnchor = {2, 1}; // found that oAnchor = {2,1} or {3,1} works for kernel [-1 0 1]
NppStatus eStatusNPP;
Npp32s hostKernel[3] = {-1, 0, 1}; // convolving with this should do edge detection
Npp32s* deviceKernel;
size_t deviceKernelPitch;
cudaMallocPitch((void**)&deviceKernel, &deviceKernelPitch, kernelSize.width*sizeof(Npp32s), kernelSize.height*sizeof(Npp32s));
cudaMemcpy2D(deviceKernel, deviceKernelPitch, hostKernel,
sizeof(Npp32s)*kernelSize.width, // sPitch
sizeof(Npp32s)*kernelSize.width, // width
kernelSize.height, // height
cudaMemcpyHostToDevice);
Npp32s divisor = 1; // no scaling
eStatusNPP = nppiFilter_8u_C1R(oDeviceSrc.data(), oDeviceSrc.pitch(),
oDeviceDst.data(), oDeviceDst.pitch(),
oSizeROI, deviceKernel, kernelSize, oAnchor, divisor);
cout << "NppiFilter error status " << eStatusNPP << endl; // prints 0 (no errors)
oDeviceDst.copyTo(oHostDst.data(), oHostDst.pitch()); // memcpy to host
saveImage("Lena_filter_1d.pgm", oHostDst);
}
Run Code Online (Sandbox Code Playgroud)
用内核输出上面的代码[-1 0 1]- 它看起来像一个合理的渐变图像:

但是,nppiFilter如果我使用2D卷积内核,则输出垃圾图像.以下是我从上面的代码更改为使用2D内核运行的内容[-1 0 1; -1 0 1; -1 0 1]:
NppiSize kernelSize = {3, 3};
Npp32s hostKernel[9] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
NppiPoint oAnchor = {2, 2}; // note: using anchor {1,1} or {0,0} causes error -24 (NPP_TEXTURE_BIND_ERROR)
saveImage("Lena_filter_2d.pgm", oHostDst);
Run Code Online (Sandbox Code Playgroud)
下面是使用2D内核的输出图像[-1 0 1; -1 0 1; -1 0 1].

这篇StackOverflow文章描述了一个类似的问题,如用户Steenstrup的图片所示:http://1ordrup.dk/kasper/image/Lena_boxFilter5.jpg
最后几点说明:
NppiPoint oAnchor = {0, 0}或{1, 1}),我得到错误-24,NPP_TEXTURE_BIND_ERROR根据NPP用户指南转换.此StackOverflow帖子中简要提到了此问题.小智 3
您正在为内核数组使用 2D 内存分配器。内核数组是密集的一维数组,而不是典型 NPP 图像那样的二维跨步数组。
只需将 2D CUDA malloc 替换为大小为 kernelWidth*kernelHeight*sizeof(Npp32s) 的简单 cuda malloc,并执行正常的 CUDA memcopy 而不是 memcopy 2D。
//1D instead of 2D
cudaMalloc((void**)&deviceKernel, kernelSize.width * kernelSize.height * sizeof(Npp32s));
cudaMemcpy(deviceKernel, hostKernel, kernelSize.width * kernelSize.height * sizeof(Npp32s), cudaMemcpyHostToDevice);
Run Code Online (Sandbox Code Playgroud)
顺便说一句,“比例因子”1 并不意味着不缩放。缩放系数为 2^(-ScaleFactor)。
| 归档时间: |
|
| 查看次数: |
1898 次 |
| 最近记录: |