繁体   English   中英

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

[英]Colourspace conversion using cuda in C++

我的目标是使用 opencv 加载图像并使用 cuda 应用色彩空间转换。 我试图转换为的初始色彩空间是来自 RGB 的 YUV422。 但是转换返回 Height: 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)没有正确设置输出图像 - (Mat Output_Image -> Mat Output_Image(Size(Width, Height), CV8UC2)

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM