LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

LabVIEW crashed when calling an opencv .dll

I am new to labview and opencv. I saw that someone already created a wrapper to use opencv functions inside labview (installed with VIPM). I have been using it for a bit now, but I got stuck in one .dll and I dont know what is causing the error. I am working with image segmentation. I wrote a small algorithm that works perfectly in visual studio or eclipse but when I try to run it in labview with a .dll it just crashes. I started to run the algorithm step by step, compiling the .dll adding function by function and saw that the problem was with the opencv "findcontours" function. However, i believe that it has to do with the number of contours it finds. I started using several images and found that it would only crash with images that had a higher number of objects (like the attached image.) If i performed a distance transform with a higher threshold the smaller dots would disappear and the .vi would run normally, but if I used a smaller threshold the dots would remain and the .vi would crash. 

 

Also, I am not exporting the array created by the "findcontours" function, only using it to apply some more image processing functions to the final image.

 

 

Does anyone have any advise as to what to do? 

 

I also tried this with another example code that I found online and the same crash happens because of the same reason.

 

#include <vector>
#include <opencv2/features2d.hpp>
#include <opencv2/objdetect.hpp>
#include <opencv2/calib3d.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <NIVisionExtLib.h>


using namespace std;
using namespace cv;

typedef cv::Point2f PointFloat;
typedef cv::Point2d PointDouble;


EXTERN_C void NI_EXPORT NIVisOpenCV_watershed(NIImageHandle sourceHandle, NIImageHandle destHandle, NIErrorHandle errorHandle) {

NIERROR error = NI_ERR_SUCCESS;
ReturnOnPreviousError(errorHandle);
try
{
if (!sourceHandle || !destHandle || !errorHandle) {
ThrowNIError(NI_ERR_NULL_POINTER);
}
NIImage source(sourceHandle);
NIImage dest(destHandle);

Mat sourceMat;
Mat res;
Mat destMat;

ThrowNIError(source.ImageToMat(sourceMat));
ThrowNIError(dest.ImageToMat(destMat));

Mat src=sourceMat;

Mat gray;
cvtColor(src, gray, COLOR_BGR2GRAY);

Mat thresh;
threshold(gray, thresh, 0, 255, THRESH_BINARY | THRESH_OTSU);

// noise removal
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3));
Mat opening;
morphologyEx(thresh, opening, MORPH_OPEN, kernel, Point(-1, -1), 2);

Mat kernelb = getStructuringElement(MORPH_RECT, Size(21, 21));
Mat background;
morphologyEx(thresh, background, MORPH_DILATE, kernelb);
background = ~background;

// Perform the distance transform algorithm
Mat dist_transform;
distanceTransform(opening, dist_transform, CV_DIST_L2, 5);

// Normalize the distance image for range = {0.0, 1.0}
// so we can visualize and threshold it
normalize(dist_transform, dist_transform, 0, 1., NORM_MINMAX);

// Threshold to obtain the peaks
// This will be the markers for the foreground objects
Mat dist_thresh;
threshold(dist_transform, dist_thresh, 0.3, 1., CV_THRESH_BINARY);

Mat dist_8u;
dist_thresh.convertTo(dist_8u, CV_8U);

// Find total markers
vector<vector<Point> > contours;
findContours(dist_8u, contours, RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);

// Create the marker image for the watershed algorithm
Mat1i markers(dist_thresh.rows, dist_thresh.cols, int(0));

// Background as 1
Mat1i one(markers.rows, markers.cols, int(1));
bitwise_or(one, markers, markers, background);

// Draw the foreground markers (from 2 up)
for (int i = 0; i < int(contours.size()); i++)
drawContours(markers, contours, i, Scalar(i + 2), -1);

// Perform the watershed algorithm
Mat dbg;
cvtColor(opening, dbg, COLOR_GRAY2BGR);
watershed(dbg, markers);


markers.convertTo(res, CV_8UC3);
normalize(res, res, 0, 255, NORM_MINMAX);

 

 

destMat = Mat::zeros(res.size(), CV_8UC3);

destMat = res;

ThrowNIError(dest.MatToImage(destMat));

}

catch (NIERROR _err) {
error = _err;
}

catch (...) {
error = NI_ERR_OCV_USER;
}
ProcessNIError(error, errorHandle);
}

Download All
0 Kudos
Message 1 of 3
(3,430 Views)

I’m not personally familiar with OpenCV , but after some research I found this page: http://www.ni.com/white-paper/53072/en/. Maybe this will be helpful for you to ensure that your system meets the requirements for using LabVIEW and OpenCV together.  

0 Kudos
Message 2 of 3
(3,368 Views)

Hi,

 

Not sure if you have solved this problem. I encountered labview crash when I tried to run vi with OpenCV functions, but I resolved it by copying OpenCV dlls to the folder where the dll your vi is calling. Maybe you could try this?

0 Kudos
Message 3 of 3
(2,954 Views)