我正在使用Tensor RT,并且在opencv::Mat
中有输入图像,我想将其加载到下面的函数中并将其转换为float4*
。下面的函数在const char* filename
中接收文件名的路径。
下面的函数将使用QImage
读取文件名并将其转换为float4*
。假设我有cv::Mat input
而不是文件名路径,并且我想不使用Mat
将float4*
直接转换为QImage
。有什么办法吗?
示例取自:https://github.com/dusty-nv/jetson-utils/blob/2fb2b9dfd8323f99c22d3e2755b88345abd2f3a8/loadImage.cpp
bool loadImageRGBA( const char* filename, float4** cpu, float4** gpu, int* width, int* height )
{
if( !filename || !cpu || !gpu || !width || !height )
{
printf("loadImageRGBA - invalid parameter\n");
return false;
}
// verify file path
const std::string path = locateFile(filename);
if( path.length() == 0 )
{
printf("failed to find image '%s'\n", filename);
return false;
}
// load original image
QImage qImg;
if( !qImg.load(path.c_str()) )
{
printf("failed to load image '%s'\n", path.c_str());
return false;
}
if( *width != 0 && *height != 0 )
qImg = qImg.scaled(*width, *height, Qt::IgnoreAspectRatio);
const uint32_t imgWidth = qImg.width();
const uint32_t imgHeight = qImg.height();
const uint32_t imgPixels = imgWidth * imgHeight;
const size_t imgSize = imgWidth * imgHeight * sizeof(float) * 4;
printf("loaded image %s (%u x %u) %zu bytes\n", filename, imgWidth, imgHeight, imgSize);
// allocate buffer for the image
if( !cudaAllocMapped((void**)cpu, (void**)gpu, imgSize) )
{
printf(LOG_CUDA "failed to allocated %zu bytes for image %s\n", imgSize, filename);
return false;
}
float4* cpuPtr = *cpu;
for( uint32_t y=0; y < imgHeight; y++ )
{
for( uint32_t x=0; x < imgWidth; x++ )
{
const QRgb rgb = qImg.pixel(x,y);
const float4 px = make_float4(float(qRed(rgb)),
float(qGreen(rgb)),
float(qBlue(rgb)),
float(qAlpha(rgb)));
cpuPtr[y*imgWidth+x] = px;
}
}
*width = imgWidth;
*height = imgHeight;
return true;
}