LabVIEW not responding when I run this code. C++ code issue or LabVIEW issue?

jfwinston32

For some background, I am compiling in Visual Studio 2019 and running the code inside LabVIEW 2017. The reason I am doing it in LabVIEW is for research to control a robotic gantry. This is the vision system and it is supposed to detect rectangles (wirebond pads for silicon detectors).

I need it to atleast show me a picture or something but when I run it in LabVIEW, it just says it is not responding and makes me hard close the program. So frustrating! If theres no huge errors in my C++ code then I know I have to dig deeper into my LabVIEW code.

The following code is my problem. I am fairly new to C++ and programming in general. I have done the step each line inside LabVIEW and when it stops responding is when it starts to grab the nominalHeight, xfov etc... or just when it goes into the WBPdetection function in general.

Any help is much appreciated. Or if someone could just point me in the right direction.

#include "stdafx.h"
#include "utils.h"
#include "WBPdetection.h"
#include "opencv2/opencv.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <vector>

using namespace std;

void show3(cv::Mat img)
{
    cv::namedWindow("MyWindow", cv::WINDOW_AUTOSIZE);
    cv::imshow("MyWindow", img);
    cv::waitKey(0);
    cv::destroyWindow("MyWindow");
}

__declspec(dllexport) int __cdecl WBPdetection(
    char* imgPtr, 
    int imgLineWidth, 
    int imgWidth, 
    int imgHeight, 
    double percent_size, 
    double nominalWidth, 
    double nominalHeight,
    double tolerance,
    double xfov,
    double yfov)
{
    cv::Mat img(imgHeight, imgWidth, CV_8U, (void*)imgPtr, imgLineWidth);
    cv::resize(img, img, cv::Size(img.cols * percent_size, img.rows * percent_size), 0, 0);

    //PREPPING IMAGE FOR DETECTION ALGORITHIM
    cv::threshold(img, img, 125, 255, cv::THRESH_OTSU);
    cv::GaussianBlur(img, img, cv::Size(5, 5), 0);
    cv::erode(img, img, cv::Mat(), cv::Point(-1, -1), 2, 1, 1); 
    cv::dilate(img, img, cv::Mat(), cv::Point(-1, -1), 1, 1, 1);
    
    //USE FIND CONTOURS ALGORITHIM
    vector<vector<cv::Point>> contours;
    vector<cv::Vec4i> hierarchy; 
    cv::findContours(img, contours, hierarchy, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE); 
    for( int i = 0; i < contours.size(); i++ ) 
    {   approxPolyDP( cv::Mat(contours[i]), contours_poly[i], 3, true ); 
        boundRect[i] = cv::boundingRect( cv::Mat(contours_poly[i]) ); 
     }

    vector<vector<double>> dimRects; //ex [ [w1,h1], [w2,h2], [w3,h3], ...]
    vector<cv::Point> centerRects; //ex [ [c1], [c2], [c3], ... ]

    //PUTTING DIMENSIONS OF ALL RECTANGLES IN VECTORS
    for (int i = 0; i < contours.size(); i++)
    {
        cv::Point center = ((boundRect[i].tl().x + boundRect[i].br().x) / 2, (boundRect[i].tl().y + boundRect[i].br().y) / 2); //what about even pixels
        double rectWidth = (boundRect[i].br().x - boundRect[i].tl().x) * (xfov / img.cols); //might not matter tbh
        double rectHeight = (boundRect[i].tl().y - boundRect[i].br().y) * (yfov / img.rows);
        dimRects[i].push_back(rectWidth);
        dimRects[i].push_back(rectHeight);
        centerRects.push_back(center);
    }

    //DEFINING minWidth, etc... FROM tolerance AND nominalWidth
    double minWidth = nominalWidth * (1 - tolerance);
    double maxWidth = nominalWidth * (1 + tolerance);
    double minHeight = nominalHeight * (1 - tolerance);
    double maxHeight = nominalHeight * (1 + tolerance);

  // DRAWING CONTOURS AND BOUNDING RECTANGLE + CENTER
    for( int i = 0; i< dimRects.size(); i++ )
     {
       cv::Scalar color = cv::Scalar(255,255,255); //creates color
       if ((dimRects[i][0] > minWidth && dimRects[i][0] < maxWidth) && (dimRects[i][1] > minHeight && dimRects[i][1] < maxHeight)) 
       {
           drawContours(img, contours_poly, i, color, 1, 8, vector<cv::Vec4i>(), 0, cv::Point()); 
           rectangle(img, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0); 
           circle(img, centerRects[i], 1, cv::Scalar(0, 0, 255), 1, cv::LINE_8);
       }
    }

    show3(img);
    return 0;
}
john

Well there is one error here

vector<vector<double>> dimRects; 
...
for (int i = 0; i < contours.size(); i++)
{
    ...
    dimRects[i].push_back(rectWidth);
    dimRects[i].push_back(rectHeight);

dimRects has zero size but your code treats it as if it has the same size as contours.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Code Coverage issue when spyon is used

Issue with binary tree, C code, using malloc

Pygame window not responding error when i try to run a code

When I run my code, i get a segfault, but when the debugger runs it, it says there is no issue

Getting issue when convert c# code to PHP as below

when I run this code in Pycharm i didn't get any outputs simply it shows------ process finished with exit code 0. please help me with this issue

I have an issue with HTML that created with php code

C# search button code issue.

Force close issue when I run app

Animation Issue when Reusing code Android

MFC-C Code merge issue

issue with loading .dll lib in C# code

The code not working. I cannot identify the issue

C# SQL output code issue

How to solve this cmake issue with c++ code?

Viewing backslash code and removing them from a string in Labview 2012

Issue with this Javascript code (I think with the execution context)

C++ code loop issue

C++- If statement issue. Code will not run the else or else if

Issue with C++ code

Overlapping issue when adding UIViews through code

How to solve the traceback error, when it is not a code issue?

How to fix issue of code coverage not finding functions when run tests do?

Issue with console when executing C# code in Visual Studio

VS Code debugging issue when reading data from a file in C

Code Issue (Does not return the output I want)

I have an issue in Kivy each time I try to run my code

I'm getting this issue when trying to run the code I found on GitHub. Pydot and graphivz are installed but still getting this error

Issue in Pyspark code when running Glue Script