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

时间:2020-02-23 17:56:04

标签: c++ opencv cuda

我的目标是使用opencv加载图像,并使用cuda应用色彩空间转换。我尝试转换为RGB的初始色空间是YUV422。但是,转换返回的高度为:0;宽度:0;频道: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 个答案:

答案 0 :(得分:1)

意识到我犯了两个错误- 1)未正确设置输出图像-(Mat Output_Image-> Mat Output_Image(Size(Width,Height),CV8UC2)

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

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

相关问题