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 post
Share on other sites

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 post
Share on other sites

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 post
Share on other sites

  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 post
Share on other sites

Join the conversation

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

Guest
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.

  • Similar Content

    • By LogMAN
      Here is another bug I discovered in LV2019+ which reliably crashes LV. Simply connect a set to the map input of the Map Get / Replace Value node on the IPE:

      This VI is executable in LV2019 SP1 f3, which means that you can actually build that into an executable without noticing.
      I have reported this, but there is no CAR yet.
    • By torekp
      DLL functions or shared variables?  Or something else?
      I have a Labview 2014-64 executable (or I can build a DLL) that runs one piece of equipment, the X-ray.  The other engineer has a large CVI Labwindows 2015 + MS Visual Studio 2012 (C++) executable that runs everything else.  I want the Labview code to be a slave of the CVI code, accepting commands to turn X-ray On or Off, reporting failures, and the like.  Translating the X-ray code into C++ would be possible in principle, but not fun.
      Shared variables look easy, but I'm kinda scared of them.  I would define all the shared variables in my LV code, since I'm more familiar with LV, then use them in both.  There's a thread in here called "Shared Variable Woes" so maybe I should be scared.  In the alternative, I tried building a proof-of-concept DLL in Labview, and calling its functions in CVI/C++, and it works, but it's kinda clunky.  (I'm attaching it below in case you want to play, or advise.)
      Your advice would be appreciated.
      XrayDLL.zip
    • By hutha
      New Toolkit : OpenCV wrapper for Raspberry Pi (LinuxRT) for LabVIEW
      Great ! learning platform student and maker for learning machine vision application with LabVIEW.
      It's working with LabVIEW Home 2014 edition and  required LINX 3.0 toolkit.
      You can run NI-VISION toolkit with Raspberry Pi board too.
      1.QwaveCameraCV is a LabVIEW Camera Driver library for Raspberry Pi 3B/3B+ (LinuxRT)
      https://github.com/QWaveSystems/QwaveCameraCV


      2.QwaveOpenCV is OpenCV wrapper functions for LabVIEW for Raspberry Pi 3B/3B+ (LinuxRT)
      https://github.com/QWaveSystems/QwaveOpenCV


      3.QwaveOpenCV Examples using OpenCV (C/C++) and NI-VISION for Raspberry Pi 3B/3B+ (LinuxRT)
      https://github.com/QWaveSystems/QwaveOpenCV-Examples


       
    • By patufet_99
      To use a controller from LabVIEW I have to use some functions of a DLL.
      For one of the functions, according to the header file .h there is a structure data with parameters of different types that I have to pass to the dll. Some of the parameres are BYTE (1 Byte)  and WORD (2 Bytes).
      When compiling this kind of structure with Visual C++ and looking at it's size with "sizeof()" it seems to me that 4 Bytes variables have to start in a position multiple of 4. For example if there is a BYTE and then a DWORD, the 3 Bytes after the BYTE are ignored and the DWORD starts at Bytes 5 to 8.
      When defining a LabVIEW cluster to match the DLL structure, will LabVIEW do the same? If in my cluster there is a U8 variable and then a U32, will anyway the U8 take 4 bytes?
       
      Thank you.
    • By torekp
      So I created a DLL from a Labview VI that has a 2D array input (and some scalar inputs) and some 2D array outputs.  Labview creates a .h file with these lines

      And then it defines the 2D array arguments to my function as being of this type: "void __cdecl Linear_discrim_4dll(DoubleArray *dataObsFeat, int32_t grpAsz," etc etc.  Trouble is, I have no idea how to fill out this structure so that the DLL can use it.  Say for simplicity I had a 2 by 3 input called dataObsFeat with elements {1,2,3; 11,12,13}; how would I create the object with these values in C or C++ and pass it to the function?  I am a total C++ noob, in case it isn't obvious.
×
×
  • Create New...

Important Information

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