Ask Your Question
1

error: (-215) func != 0 in function convertTo

asked 2013-08-28 10:45:49 -0500

dagey gravatar image

updated 2013-08-28 11:56:56 -0500

Hi,

In the following code I get an opencv assertion and I don't understand why. Any help will be very much appreciated.

The code:

Mat m(pm->get_current_tracked_keypoints()); // after this line: m.type=7 and m.depth=7
Mat m1;
m.convertTo(m1, CV_32F);

The error:

OpenCV Error: Assertion failed (func != 0) in convertTo, file /build/buildd/opencv-2.3.1/modules/core/src/convert.cpp, line 937
terminate called after throwing an instance of 'cv::Exception'
  what():  /build/buildd/opencv-2.3.1/modules/core/src/convert.cpp:937: error: (-215) func != 0 in function convertTo

Thanks

edit retag flag offensive close merge delete

Comments

1

m.depth=7 means CV_USRTYPE1. It is a generic type contant and OpenCV doesn't know, how to work with it. What exactly does pm->get_current_tracked_keypoints() return?

Vladislav Vinogradov gravatar imageVladislav Vinogradov ( 2013-08-28 11:53:44 -0500 )edit

It returns an std::vector<KeyPoint>. I am trying to get a CV_32F mat that contains this list of points. Thanks

dagey gravatar imagedagey ( 2013-08-28 15:36:32 -0500 )edit
1

Then loop over your vector and copy each keypoint to a row in the matrix.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-08-28 15:41:26 -0500 )edit
1

It worked, thank you very much !!

dagey gravatar imagedagey ( 2013-08-29 01:34:37 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2013-08-28 11:49:15 -0500

Moster gravatar image

updated 2013-08-28 11:50:30 -0500

Its simple. The conversion from Mat depth 7 to 5 does not exist in opencv.

BinaryFunc func = noScale ? getConvertFunc(sdepth, ddepth) : getConvertScaleFunc(sdepth, ddepth);

This is the the function func that you see in cv_assert.

BinaryFunc getConvertFunc(int sdepth, int ddepth)
{
     return cvtTab[CV_MAT_DEPTH(ddepth)][CV_MAT_DEPTH(sdepth)];
}

This is the function that determines the function which is used for conversion. It checks the array cvtTab. (ddepth is 5, sdepth is 7)

static BinaryFunc cvtTab[][8] =
{
{
    (BinaryFunc)cvt8u, (BinaryFunc)cvt8s8u, (BinaryFunc)cvt16u8u,
    (BinaryFunc)cvt16s8u, (BinaryFunc)cvt32s8u, (BinaryFunc)cvt32f8u,
    (BinaryFunc)cvt64f8u, 0
},
{
    (BinaryFunc)cvt8u8s, (BinaryFunc)cvt8u, (BinaryFunc)cvt16u8s,
    (BinaryFunc)cvt16s8s, (BinaryFunc)cvt32s8s, (BinaryFunc)cvt32f8s,
    (BinaryFunc)cvt64f8s, 0
},
{
    (BinaryFunc)cvt8u16u, (BinaryFunc)cvt8s16u, (BinaryFunc)cvt16u,
    (BinaryFunc)cvt16s16u, (BinaryFunc)cvt32s16u, (BinaryFunc)cvt32f16u,
    (BinaryFunc)cvt64f16u, 0
},
{
    (BinaryFunc)cvt8u16s, (BinaryFunc)cvt8s16s, (BinaryFunc)cvt16u16s,
    (BinaryFunc)cvt16u, (BinaryFunc)cvt32s16s, (BinaryFunc)cvt32f16s,
    (BinaryFunc)cvt64f16s, 0
},
{
    (BinaryFunc)cvt8u32s, (BinaryFunc)cvt8s32s, (BinaryFunc)cvt16u32s,
    (BinaryFunc)cvt16s32s, (BinaryFunc)cvt32s, (BinaryFunc)cvt32f32s,
    (BinaryFunc)cvt64f32s, 0
},
{
    (BinaryFunc)cvt8u32f, (BinaryFunc)cvt8s32f, (BinaryFunc)cvt16u32f,
    (BinaryFunc)cvt16s32f, (BinaryFunc)cvt32s32f, (BinaryFunc)cvt32s,
    (BinaryFunc)cvt64f32f, 0
},
{
    (BinaryFunc)cvt8u64f, (BinaryFunc)cvt8s64f, (BinaryFunc)cvt16u64f,
    (BinaryFunc)cvt16s64f, (BinaryFunc)cvt32s64f, (BinaryFunc)cvt32f64f,
    (BinaryFunc)cvt64s, 0
},
{
    0, 0, 0, 0, 0, 0, 0, 0
}
};

As you can see cvtTab[5][7] is 0, so cv_assert will fail. Depth 7 is CV_USRTYPE1. I dont exactly know what usrtype1 is, but I guess it means a custom Mat type. So opencv cant handle the conversion since it does not exactly know the type.

edit flag offensive delete link more

Comments

Thank you very much for this detailed response!

I am trying to build a kd-tree with cv::flann::Index. So, I was trying to build it with a set of keypoints that I have by inserting them to a Mat but in order to build it I need a CV_32F mat and I can't get it right. How do you think I can solve this problem ? Thanks

dagey gravatar imagedagey ( 2013-08-28 15:33:10 -0500 )edit
2

Do what Steven says. Loop through all KeyPoints and add them manually. Simplest way would be:

int size = keypointVector.size();

Mat keyPoints (size, 2, CV32FC1);

for(int i = 0; i < size; i++) {

keyPoints.at<float>(i,0) = keyPointVector[i].pt.x;

keyPoints.at<float>(i,1) = keyPointVector[i].pt.y;

}

This might be done more elegant or faster: http://docs.opencv.org/doc/tutorials/core/how_to_scan_images/how_to_scan_images.html

Moster gravatar imageMoster ( 2013-08-28 16:26:36 -0500 )edit

aaaa how I didn't think of it ! It worked, thank you very much !

dagey gravatar imagedagey ( 2013-08-29 01:33:52 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2013-08-28 10:45:49 -0500

Seen: 11,439 times

Last updated: Aug 28 '13