我正在使用DirectX创建一个C ++ Windows应用程序,以在HDR中显示PPM图像。我正在使用cv::imread
将文件读入Mat
,然后创建一个ID3D11Texture2D*
纹理。但是,当我运行它时,我在运行cv::directx::convertToD3D11Texture2D(mat, tex)
时遇到访问冲突,它说tex
是NULL
。
据我了解,CreateTexture2D
初始化纹理,convertToD3D11Texture2D
将图像复制到纹理。
这是相关代码:
auto mat = cv::imread("fileL.ppm", cv::IMREAD_ANYCOLOR | cv::IMREAD_ANYDEPTH);
D3D11_TEXTURE2D_DESC desc;
desc.Width = 3840;
desc.Height = 2160;
desc.MipLevels = desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.SampleDesc.Count = 1;
desc.Usage = D3D11_USAGE_DYNAMIC;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
desc.MiscFlags = 0;
ID3D11Texture2D* tex = nullptr;
m_resources->GetDevice()->CreateTexture2D(&desc, nullptr, &tex);
cv::directx::convertToD3D11Texture2D(mat, tex);
具体来说,错误是:
0x00007FFC4750A839 in file.exe: Microsoft C++ exception: _com_error at memory location 0x00000045A28FF340.
Exception thrown at 0x00007FFBCD08A390 (opencv_world410d.dll) in file.exe: 0xC0000005: Access violation reading location 0x0000000000000000.
编辑:添加了desc.SampleDesc.quality = 0
之后,我不再遇到CreateTexture2D
的错误,但是在convertToD3D11Texture2D:
Exception at memory location 0x0000002A3836E8D0
上却得到了一个通用异常
编辑2 :缓存异常后,异常消息为:
OpenCV(4.1.0) c:\build\master_winpack-build-win64-vc15\opencv\modules\core\src\directx.cpp:1040: error: (-215:Assertion failed) textureType == srcType in function 'cv::directx::__convertToD3D11Texture2DNV'
答案 0 :(得分:0)
在终于能够正确调试该错误之后,我发现问题是由于我的输入矩阵具有3个通道而不是4个通道。要解决此问题,我使用了cv::mixChannels
添加了一个空的alpha通道,并且所以现在我的纹理和矩阵的类型匹配了。
工作代码:
cv::Mat mat = cv::imread("file.ppm", cv::IMREAD_ANYCOLOR | cv::IMREAD_ANYDEPTH);
cv::Mat as4channelMat(mat.size(), CV_MAKE_TYPE(mat.depth(), 4));
int conversion[] = { 0, 0, 1, 1, 2, 2, -1, 3 };
cv::mixChannels(&mat, 1, &as4channelMat, 1, conversion, 4);
D3D11_TEXTURE2D_DESC desc;
desc.Width = 3840;
desc.Height = 2160;
desc.MipLevels = desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_R16G16B16A16_UNORM;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = 0;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
desc.MiscFlags = 0;
ID3D11Texture2D* tex = nullptr;
auto hr = m_resources->GetDevice()->CreateTexture2D(&desc, nullptr, &tex);
if FAILED(hr)
{
_com_error err(hr);
LPCTSTR errMsg = err.ErrorMessage();
}
try {
cv::directx::convertToD3D11Texture2D(as4channelMat, tex);
} catch (cv::Exception& e)
{
std::cerr << "ERROR: " << e.msg << std::endl;
throw e;
}