Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ cuda和opencv:我只想要与原始图片相同的图片_C++_Opencv_Memory_Cuda_Gpu - Fatal编程技术网

C++ cuda和opencv:我只想要与原始图片相同的图片

C++ cuda和opencv:我只想要与原始图片相同的图片,c++,opencv,memory,cuda,gpu,C++,Opencv,Memory,Cuda,Gpu,我只是想把test.jpg放到纹理内存中,然后通过text2D()来显示图片,但是结果非常奇怪。“结果”应与“灰色”相同。单击此处可获得图片:和。将“灰色”减少到原来的四分之一并排列成一行 这是我正在使用的代码 #include <cuda_runtime.h> #include <highgui/highgui.hpp> #include <imgproc/imgproc.hpp> #include "device_launch_par

我只是想把test.jpg放到纹理内存中,然后通过text2D()来显示图片,但是结果非常奇怪。“结果”应与“灰色”相同。单击此处可获得图片:和。将“灰色”减少到原来的四分之一并排列成一行

这是我正在使用的代码

 #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);  
}  
#包括
#包括
#包括
#包括“设备启动参数.h”
#包括
使用名称空间std;
使用名称空间cv;
纹理texRef;
__全局无效转换内核(uchar*输出,整数宽度,整数高度)
{  
无符号整数x=blockIdx.x*blockDim.x+threadIdx.x;
无符号整数y=blockIdx.y*blockDim.y+threadIdx.y;
浮动u=x/(浮动)宽度;
浮动v=y/(浮动)高度;
输出[(y*宽度+x)]=tex2D(texRef,x,y);
}  
int main()
{  
Mat image=imread(“test.jpg”,0);
整数宽度=512;
整数高度=512;
调整大小(图像、图像、大小(宽度、高度));
imshow(“灰色”,图像);
cudaChannelFormatDesc channelDesc=cudaCreateChannelDesc(32,0,0,cudaChannelFormatKindFloat);
cudaArray*cudaArray;
cudaMallocArray(&cuArray,&channelDesc,宽度,高度);
cudaMemcpyToArray(cudaMemcpyToArray,0,0,image.data,sizeof(uchar)*宽度*高度,cudaMemcpyHostToDevice);
texRef.addressMode[0]=cudaAddressModeWrap;
texRef.addressMode[1]=cudaAddressModeWrap;
texRef.filterMode=cudaFilterModeLinear;
texRef.normalized=false;
cudaBindTextureToArray(texRef、cuArray、channelDesc);
Mat imageOutput=Mat(尺寸(宽度、高度),CV_8UC1);
uchar*output=imageOutput.data;
cudamaloc((void**)和输出,宽度*高度*尺寸(float));
dim3 dimBlock(16,16);
dim3 dimGrid((宽度+dimBlock.x-1)/dimBlock.x,(高度+dimBlock.y-1)/dimBlock.y);
transformKernel>(输出、宽度、高度);
cudaMemcpy(imageOutput.data,输出,高度*宽度,cudaMemcpyDeviceToHost);
imshow(“结果”,图像输出);
waitKey();
cudaFreeArray(cudarray);
cudaFree(输出);
}  
有人能告诉我为什么吗?

#包括
#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);  

}  
#包括 #包括 #包括“设备启动参数.h” #包括 使用名称空间std; 使用名称空间cv; 纹理texRef; __全局无效转换内核(uchar*输出,整数宽度,整数高度) { 无符号整数x=blockIdx.x*blockDim.x+threadIdx.x; 无符号整数y=blockIdx.y*blockDim.y+threadIdx.y; 浮动u=x/(浮动)宽度; 浮动v=y/(浮动)高度; 输出[(y*宽度+x)]=tex2D(texRef,x,y); } int main() { Mat image=imread(“test.jpg”,0); 整数宽度=512; 整数高度=512; 调整大小(图像、图像、大小(宽度、高度)); imshow(“灰色”,图像); cudaChannelFormatDesc channelDesc=cudaCreateChannelDesc(8,0,0,cudaChannelFormatKindUnsigned); cudaArray*cudaArray; cudaMallocArray(&cuArray,&channelDesc,宽度,高度); cudaMemcpyToArray(cudaMemcpyToArray,0,0,image.data,sizeof(uchar)*宽度*高度,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(尺寸(宽度、高度),CV_8UC1); uchar*output=imageOutput.data; cudamaloc((void**)和输出,宽度*高度*尺寸(uchar)); dim3 dimBlock(16,16); dim3 dimGrid((宽度+dimBlock.x-1)/dimBlock.x,(高度+dimBlock.y-1)/dimBlock.y); transformKernel>(输出、宽度、高度); cudaMemcpy(imageOutput.data,输出,高度*宽度*大小(uchar),cudaMemcpyDeviceToHost); imshow(“结果”,图像输出); waitKey(); cudaFreeArray(cudarray); cudaFree(输出); }

只需将“float”更改为“uchar”

提示:标准化坐标、纹理的数据类型与实际图像不同、无错误检查、设备内存过度分配等…谢谢,我已经用你的提示解决了我的问题。你能把你的解决方案作为答案贴出来,这样问题就可以标记为已解决了吗?谢谢你的指点,我已经这样做了。