c++ - 在C++中使用cuda进行颜色空间转换

标签 c++ opencv cuda

我的目标是使用opencv加载图像并使用cuda应用色空间转换。我尝试转换为RGB的初始色空间是YUV422。但是,转换返回的高度为:0;宽度:0; channel :1。

#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include "npp.h"
#include <iostream>
#include <opencv2\opencv.hpp>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>

using namespace std;
using namespace cv;

void display_image(Mat image);

int main() {

    //Read image and display attributes
    Mat Input_Image = imread("Test_Image.png");
    Mat Output_Image;
    display_image(Input_Image);
    int nHeight = Input_Image.rows;
    int nWidth = Input_Image.cols;
    int nChannels = Input_Image.channels();
    cout << "Height: " << nHeight << "; Width: " << nWidth << "; Channels: " << nChannels << endl;

    unsigned char* Dev_Image = NULL;
    cudaMalloc(&Dev_Image, nHeight*nWidth*2);
    cudaMemcpy(Dev_Image, Input_Image.data, nHeight*nWidth*nChannels, cudaMemcpyHostToDevice);

    NppStatus errStatus;
    NppiSize Size;
    Size.width = nWidth;
    Size.height = nHeight;

    errStatus = nppiRGBToCbYCr422_8u_C3C2R(Input_Image.data, nWidth * sizeof(int) * 3, Dev_Image, nWidth * sizeof(int) * 2, Size);
    cudaMemcpy(Output_Image.data, Dev_Image, nHeight*nWidth*2, cudaMemcpyDeviceToHost);

    //Convert(Input_Image.data, nHeight, nWidth, nChannels);
    cout << "Height: " << Output_Image.rows << "; Width: " << Output_Image.cols << "; Channels: " << Output_Image.channels() << endl;
    display_image(Output_Image);
    system("pause");
    cudaFree(Dev_Image);
    return 0;

}

void display_image(Mat Image) {
    imshow("Image", Image);
    waitKey(0);
    destroyAllWindows();
}

最佳答案

意识到我犯了两个错误-
1)未正确设置输出图像-(垫输出图像->垫输出图像(尺寸(宽度,高度),CV8UC2)

2)NPP转换格式采用BGRA图像。将输入图像从BGR转换为BGRA并成功转换。

另外,将NPP转换函数中的step参数更改为Image.step

关于c++ - 在C++中使用cuda进行颜色空间转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60365146/

相关文章:

linux - 无法在 64 位 Linux 上安装 Haskell CUDA

c++ - RNA 到蛋白质。程序编译但不会停止运行

python - TypeError:期望一个单段缓冲区对象

opencv - 立体矫正后图像变形

c++11 - CUDA:如何使用barrier.sync

c++ - 了解纹理如何与 CUDA 配合使用

c++ - 构造函数参数列表中的模板参数无效

C++使用指针创建可扩展数组

c++ - 尝试将 gz 文件提供给 C++ 程序

java - 如何向谷歌数据流添加依赖项?