简体   繁体   中英

wxWidgets + OpenCV rectangle crash OnButtonClick()

I am trying to draw a simple rectangle in a OpenCV Mat when a wxbutton is clicked, but program crash when calls rectangle function.

void wxFrameFrame::OnButton1Click(wxCommandEvent& event) 
{
    Mat A(480,640,CV_32F);
    Point p1(10, 10);
    Point p2(20,200);
    rectangle(A, p1, p2, Scalar(255,0,255,0), 0);

    // Show what you got
    namedWindow( "src", CV_WINDOW_AUTOSIZE );
    imshow( "src", A );
}

I have tried this code in a console application and works fine. I have also tried to put this code in the wxFrame constructor and works fine too. If I comment the rectangle function all is ok (it shows a black image).

I have this problem for two days now, my original problem was I couldn't warpAffine an image because it crashed too.

I can draw in the image with

A.at<uchar>(0,0)=255 

and I know I can do a for loop to draw the rectangle.

My environment is Windows 7 64-bit, OpenCv 2.3.1, wxWidgets 2.8.12 and CodeBlocks 10.05

Any help and idea is appreciated, Thanks.

---edit----

I have tried the functions rectangle, warpAffine and putText and the three crashed when called inside OnButtonClick. The functions line and circle work fine.

It looks like the problem is in the rectangle routine. Is this something in OpenCV? or a method of wxFrameFrame?

The 1st thing I would try is to see if rectangle is having a problem with being passed automatics that go out of scope as soon as it is called.

Try this

void wxFrameFrame::OnButton1Click(wxCommandEvent& event) 
{
    static Mat A(480,640,CV_32F);
    static Point p1(10, 10);
    static Point p2(20,200);
    static Scaler s(255,0,255,0);
    static int n = 0;
    rectangle(A, p1, p2, s, n);

    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM