Qu Ang
Qu Ang

Reputation: 27

Segment fault while convert cvPoint[][] to cvPoint** in openCV

I tried to use cvFillPoly() and cvPolyline() but it doesn't work. Please help me what i did wrong?

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>

int main() {

    IplImage *background;

    background = cvCreateImage( cvSize(800,600) , IPL_DEPTH_8U , 3 );

    CvPoint listpoint[][4] = {
        {cvPoint(35,70),cvPoint(160,34),cvPoint(245,570),cvPoint(23,700)},
        {cvPoint(563,341),cvPoint(20,80),cvPoint(320,40)},
        {cvPoint(350,470),cvPoint(700,599),cvPoint(400,400)}
    };

    int _npts[3] = {4,3,3};

    cvFillPoly( background , listpoint , _npts , 1 , cvScalar(255,255,255) );

    cvNamedWindow( "Drawing Things" , CV_WINDOW_AUTOSIZE );
    cvShowImage( "Drawing Things" , background );

    cvWaitKey(0);

    cvReleaseImage( &background );
    cvDestroyWindow( "Drawing Things" );

    return 0;
}

And this is error: (on Qt creator)

 cannot convert 'CvPoint (*)[4]' to 'CvPoint**' for argument '2' to 'void cvPolyLine(CvArr*, CvPoint**, const int*, int, int, CvScalar, int, int, int)'
 cvPolyLine( BasedImage , listpoint , numberpoint , 1 , 0 , cvScalar(255,255,255) , 0 , CV_AA );
                                                                                              ^

Upvotes: 1

Views: 252

Answers (1)

rabensky
rabensky

Reputation: 2934

You need to know the difference between int A[4][5] and int**B: the first has 20 elements consecutively in memory (starting with A) and the other has pointers to int arrays in memory.

In other words - in B you get a list of arrays - each one located at a different memory location, each can have a different size. When you look at the data in the memory location of B itself you see a pointer to the first array.

In A - it's just a convenient shortcut: You can think of int A[4][5] as int real_A[20], and each time you do A[i][j] you're actually doing (in the assembly code) real_A[i*5+j]

When you look at the data in the memory location of A itself you see the data of A[0][0].

So when you pass listpoint to openCV, it looks at the data in the listpoint location in memory (which is actually a cvPoint) and interprets it as a pointer in memory, getting garbage location and segmentation fault :)

Edit - try this to see for yourself:

int A[4][5];
cout << A<<" "<<&A[0]<<" "<<&A[0][0]<<endl;

You'll get the same value 3 times!

Upvotes: 2

Related Questions