cvPerspectiveTransform GetMat error

hi all :wink:

i’m trying to use the cvPerspectiveTransform() function to transform a set of points from a blob contour by the projection matrix, so similar to actual WarpIntome function but applied to a set of points instead of to the whole image.

i’m using the same matrix that uses of_warpIntoMe() function so a 3x3.
i’ve the specification of this function here :

# Performs perspective matrix transform of vector array
cvPerspectiveTransform = _cxDLL.cvPerspectiveTransform
cvPerspectiveTransform.restype = None # void
cvPerspectiveTransform.argtypes = [
ctypes.c_void_p, # const CvArr* src
ctypes.c_void_p, # CvArr* dst
ctypes.c_void_p # const CvMat* mat

my code looks like this …

CvArr* srcPoints = cvAlloc(sizeof(CvPoint2D32f)*nSrc);
CvArr* dstPoints = cvAlloc(sizeof(CvPoint2D32f)*nSrc);
srcPoints = new CvPoint2D32f[nSrc];
dstPoints = new CvPoint2D32f[nSrc];
//copy points from blobs[0].pts to srcPoints

it compiles ok and i’m always getting error in execution from OpenCV with CvGetMat error …

Unrecognized or unsupported array type in function cvGetMat (cxarray.cpp[2863])

has anybody worked with this function ? am i making an error on the array construction or in the matrix ?

i’ve digged into opencv informations and found some people with similar difficulties to setup the arrays and matrix to work.

any ideas ?


can you post if possible a very small example (the smallest example possible, no video, etc) that does this and I will try to take a look ?

printfs or commenting out can show you which call is doing it - if you know the call, you probably know more specifically what to google about.

today I actually have to do something similar (compute homography and just warp opengl drawing instead of image based stuff). the opengl warping is a bit like the simple opengl shadow tutorials, where you compute a tranformation matrix based off of light position and the floor plane. In the end you mulitply a box of numbers by the points you draw (opengl does this automatically all the time) the same thing you are trying to do by hand in opencv - so it might be helpful.

take care!

hi zach

here is the zip with the code .

– “q/a” + and - thershold level
– “p” then u have to set a quad of 4 points with the mouse
which define the transformation to be done.
(starting by the top-left and counterclockwise)
– “z” swap between two modes :
1/calculate matrix from 4 points then apply it to the whole image. get contour.
2/this is the mode that i’m trying to do : apply transformation to the vertexs of a contour.CRASHES.

i’m just including the /openframeworks/computervision lib because it’s the only one that changed.

i’ve added 3 new functions and some matrices :

//warp perspective
CvMat* warpMatrix;
CvPoint2D32f* warpBOXsrc;
CvPoint2D32f* warpBOXdst;
void warpImage(ofCvGrayscaleImage& mom);
void warpMatrixQ(ofPoint2f src[4], ofPoint2f dst[4]);
ofPoint2f* warpPoints(ofPoint2f* src,int nSrc);

basically i’ve a function to calculate the matrix from 4+4 points.(warpMatrixQ) and another function to apply this transformation to the whole image(warpImage) and the last one is the one to apply transformation to collection of vertex (warpPoints).(the one that crashes)

hi eloi,

I’m happy to help you with this but you have altered the opencv code in ways that are giving me trouble running your app. For example:

void ofCvGrayscaleImage::swapTemp(){  
	IplImage * temp;  
	temp 			= cvImage;  
	cvImage 		= cvImageTemp;  
	cvImageTemp 	= temp;  

note, the original code:

void ofCvGrayscaleImage::swapTemp(){  
	IplImage * temp;  
	temp 			= cvImage;  
	cvImage 		= cvImageTemp;  
	cvImageTemp 	= temp;  

why add delete(temp) ? this is bad, bad, bad – swap temp is just switching pointers of two images, if you delete one of them, you will break ofCvGrayscaleImage’s ability to work. in fact, it works only once and the second time you do anything with the image, it breaks since one of the two pointers is deleted.

can you please recode it without altering too much the OF code? I am having trouble getting to the specific problem you are writing about…

another small note:

in main.cpp, you can do this:

ofRunApp(new testApp());  

instead of :

testApp APP;  

this allows “atExit” to properly delete testApp - new since 0.03, you can see it in the examples. this will help avoid getting errors as you quit the app.


hi zach …

sorry for that … it was an obvious error . I was looking for a memory not deallocated and i test with that non-sense stuff … it got stuck on the test version i zipped, not on my full version … sorry for the mistake … my error.

can u just delete that line and leave it as originally it was ?
still too difficult to compile ?

i modify opencv files and functions looking for necessities i need on the code . i know is not the way it should be done, i should extend the OF class with a new class adding my new needed functions … sorry for that lazyness … sometimes prototyping has this problems …

still getting error on cvPerspectiveTransform() … :_(

thanks a lot for your testApp stuff, i’ve been developping in OF.0.02 and i haven’t actualized yer to OF.0.03.


hi eloi,

I can’t get to the problem you are writing about -

can you please code a very simple example showing what you want to do ?
(preferably with little modifications to OF code)

I’d be happy to look at it, but I can’t figure out the issue you have.


hi zach …

i’ve updated a new code for you to try …

in the RAR file you’ll find one folder with my app and a libs folder.

on the libs folder there’s just :
/libs/openframeworks/computerVision folder, which is the one i’m having the problem with. I think is the only code i’ve been playing with …

i think that if u copy an OF project that links with openCV and just modify it with my computerVision libs and copy my app code u should be able to compile it succesfully.

so bringing back the problem again : i was trying to apply a QMatrix transformation not to the whole image (as in WarpIntoMe), but to a collection of points (a contour from ofCountourFinder) …

in testApp.cpp … in update() there is :


this is the function that i’ve added to grayscale images that crashes, it should apply the transformation matrix to an array of points, but i can’t get it to work . always telling me cvGetMat error …

more inside it’s crashing the “cvPerspectiveTransform” function by some error on preparing the parameters that i can’t solve.

if you comment out that line you’ll see the test application running…
if you press “p” you have to click on four corners of area to warp.

really hope it works on your system …

thanks a lot for keeping with your help…


hi eloi,

if I rewrite your code:



if (contourFinder.nBlobs > 0){  

it doesn’t crash – I still don’t see the problem though.

can I ask you to maybe code a *dirt* simple example of manipulating points by opencv, ie, not hacking OF code (and preferably not using video) that shows the problem?

I’m happy to help, but I still don’t see the issue :frowning:

  • zach

I just tried to code a small example myself and I couldn’t get it to work. I think you will have to make a very simple example and ask on the opencv mailing list. there might be a bug or something you and I are doing wrong… sorry I can’t help more.

for warping 2d points, you might want to take a look at the code from anti grain geometry - I’ve used that before for perspectival warping.

sorry I can’t be more helpful

take care,