我需要在我的代码中使用 getRectSubPix 函数作为裁剪和旋转图像部分的过程的一部分。这通常适用于 3 channel 图像,但一旦我尝试将它与 BGRA 或 RGBA 垫图像一起使用,它就会崩溃,对我说
OpenCV Error: Unsupported format or combination of formats () in cvGetRectSubPix, file /home/biotracking/Downloads/OpenCV-2.4.2/modules/imgproc/src/samplers.cpp, line 550
我的代码基本上是这样的
cv::cvtColor(polymask, polymask, CV_BGR2BGRA);
getRectSubPix(polymask, cv::Size(sqrt(biggestdistancesquared)*2,sqrt(biggestdistancesquared)*2), src_center, polymask);
如果此功能真的不适用于带有 alpha channel 的垫子,那似乎很疯狂。有人知道吗?
最佳答案
前一段时间我注意到了这个问题和@blorggggg的回答,深深地怀疑,因为问题与channels有关,而答案与depth有关.
我的回答:
错误上升是因为 getRectSubPix
仅支持 channel 1 或 3 的图像。
相关代码:
if( (cn != 1 && cn != 3) || !CV_ARE_CNS_EQ( src, dst ))
CV_Error( CV_StsUnsupportedFormat, "" );
线程中的语句here by @luhb 是真的,因为它确实只支持深度 8 或 32 的图像是真的,但与这里的问题无关。
相关代码:
if( CV_MAT_DEPTH( src->type ) != CV_8U || CV_MAT_DEPTH( dst->type ) != CV_32F )
CV_Error( CV_StsUnsupportedFormat, "" );
我检查了2.4/2.8/2.9版本的代码片段。
最后,为了解决你的麻烦,你可以先拆分Mat,在每个 channel 上getRectSuxPix
,然后合并四个 channel 。
关于带有 Alpha channel 的 OpenCV getRectSubPix,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12048381/