【问题标题】:Colourspace conversion using cuda in C++在 C++ 中使用 cuda 进行色彩空间转换
【发布时间】:2020-06-07 10:44:12
【问题描述】:

我的目标是使用 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();
}

【问题讨论】:

  • 希望this有所帮助。

标签: c++ opencv cuda


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-13
    • 1970-01-01
    • 2015-06-19
    • 1970-01-01
    • 2011-12-30
    相关资源
    最近更新 更多