1

I need to use the getRectSubPix function in my code as part of a process of cropping an rotating a section of my image. This works fine normally with 3 channel images, but as soon as I try to use it with a BGRA or a RGBA mat image it crashes saying to me

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

my code is basically like this

                cv::cvtColor(polymask, polymask, CV_BGR2BGRA);
                getRectSubPix(polymask, cv::Size(sqrt(biggestdistancesquared)*2,sqrt(biggestdistancesquared)*2), src_center, polymask);

If this function truly didn't work for Mats with alpha channels that seems crazy. Anybody know?

blorgggg
  • 414
  • 6
  • 15

2 Answers2

4

I noticed this problem and the answer by @blorgggg a while ago, and doubted deeply, since the problem is related with channels, but the answer is related with depth.


My answer: The error rises because getRectSubPix supports only image of channels 1 or 3.

Related Code:

if( (cn != 1 && cn != 3) || !CV_ARE_CNS_EQ( src, dst ))
    CV_Error( CV_StsUnsupportedFormat, "" );

The statement in thread here by @luhb is true, since it indeed supports only image of depth 8 or 32 is true, but has nothing to do with the question here.

Related code:

if( CV_MAT_DEPTH( src->type ) != CV_8U || CV_MAT_DEPTH( dst->type ) != CV_32F )
            CV_Error( CV_StsUnsupportedFormat, "" );

I check the code snippet in version 2.4/2.8/2.9.


Finally, to solve your trouble, you could first split the Mat, getRectSuxPix on each channel, and then merge the four channels.

Community
  • 1
  • 1
squid
  • 2,597
  • 1
  • 23
  • 19
0

Another guy nailed the answer to this question in this other question user luhb answered: Open CV using 16 bit signed format not working... Cropping and re-sizing an image

It seems getRectSubPix only work with {src,dst} type of {CV_8U, CV_8U}, {CV_32F, CV_32F} and {CV_8U, CV_32F}.

I dip into the source code and figure that out. There's no specification in the API reference.

Community
  • 1
  • 1
blorgggg
  • 414
  • 6
  • 15