Jump to content

LabVIEW crashes when calling OpenCV dll


Recommended Posts

Hi everyone. 
I wrote wrapper in Visual Studio for OpenCV calibrateCamera() function. Every operation like converting 1D array of points to vector<vector<Point2f>>  etc, works ok, except most important thing.
When function ()calibrateCamera is called, LabVIEW crashes. When this line is commented out, dll works ok (at least labview is still alive). Im out of ideas. I suspect that this is problem with memory management but i dont have an idea where it cames from. Function do not operate directly on data passed from LabVIEW.

Ultimately I can compile code as exe and call it via cmd, but I'm curious why such a thing appears.

 

Thanks for your precious time!
Zyga


dll source (if you need i can enclose some sample input data):

 

#include <opencv2/core/core.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <extcode.h>

using namespace cv;
using namespace std;


static void calcBoardCornerPositions(Size boardSize, float squareSize, vector<Point3f> &corners);
static void arr2vect(float *APoints,int *Asize, vector<vector<Point2f>> &imagePoints);

extern "C"
{
    float __declspec(dllexport)

Calibrate(float *APoints, int *Asize, int width, int height, float squareSize)
{
    //initialize variables
    vector<vector<Point2f>> imagePoints;
    Size boardSize;
    boardSize.height = height; boardSize.width = width;
    Mat cameraMatrix = Mat::eye(3, 3, CV_64F), distCoeffs = Mat::zeros(1, 1, CV_64F);
    cameraMatrix.at<double>(0,0) = 1.0;
    vector<Mat> rvecs, tvecs;
    vector<vector<Point3f>> objectPoints(1);
    Size imageSize; imageSize.width = 2040; imageSize.height = 2040;

arr2vect(APoints, Asize, imagePoints); // prepare imagePoints
calcBoardCornerPositions(boardSize, squareSize, objectPoints[0]); //prepare objectPoints
objectPoints.resize(imagePoints.size(),objectPoints[0]);

//calibrate
double rms = calibrateCamera(objectPoints, imagePoints, imageSize, cameraMatrix, distCoeffs, rvecs, tvecs);
return rms;
}
}

 

 

//definitions
static void arr2vect(float *APoints,int *Asize, vector<vector<Point2f>> &imagePoints)
{
    int page = *(Asize); int row = *(Asize+1); int col = *(Asize +2);
    Point2f pointBuf;
    vector<Point2f> vectBuf;

    for (int i = 0; i<page; i++)
    {
        for (int j = 0; j<row; j++)
        {
            pointBuf.x = *(APoints + (i*(row*col))+j*2);
            pointBuf.y = *(APoints + (i*(row*col))+j*2+1);
            vectBuf.push_back(pointBuf);
        }
        imagePoints.push_back(vectBuf);
        vectBuf.clear();
    }
    vectBuf.clear();
}

static void calcBoardCornerPositions(Size boardSize, float squareSize, vector<Point3f> &corners)
                                    
{
    corners.clear();
            for( int i = 0; i < boardSize.height; ++i )
                 for( int j = 0; j < boardSize.width; ++j )
                     corners.push_back(Point3f(float( j*squareSize ), float( i*squareSize ), 0));   

}

Edited by Zyga
Link to comment

How is the call library node configured in LabVIEW when you call this function? What are the inputs to it? Make sure all arrays (ASize and APoints here) are preallocated to the correct length in LabVIEW before passing them to the DLL.

Link to comment

Thanks for repty.
my function ptotype:
float Calibrate(float *APoints, int32_t *Asize, int32_t width, int32_t height, float squareSize);
And LabVIEW call:
post-27557-0-19450500-1422400009.png
If I remove:
double rms = calibrateCamera(objectPoints, imagePoints, imageSize, cameraMatrix, distCoeffs, rvecs, tvecs);
from compiled code, functions works. Non of from LabVIEW data is used to call this function.
objectPoints is a vector created from Apoints  by arr2vect() while Asize is used to properly place elements in mentioned vector.

Link to comment

  1. Have you made sure you pass the arrays as Array Data Pointers, and not any other format? http://zone.ni.com/reference/en-XX/help/371361L-01/lvexcodeconcepts/array_and_string_options/
  2. What happens if you call this DLL from another C++ program?

 

double rms = calibrateCamera(objectPoints, imagePoints, imageSize, cameraMatrix, distCoeffs, rvecs, tvecs);

from compiled code, functions works. Non of from LabVIEW data is used to call this function.

objectPoints is a vector created from Apoints  by arr2vect() while Asize is used to properly place elements in mentioned vector.

 

Well, you are using LabVIEW data to create objectPoints.

 

Crashes caused by memory errors don't always happen immediately. For example, the error could have happened in arr2vect(), but program only crashes when you call calibrateCamera(). Or, by removing calibrateCamera(), you changed the memory layout of your program in a way such that the error doesn't cause a crash anymore.

Edited by JKSH
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.