From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Machine Vision

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I return calculated Histogram results from OpenCV back to LabVIEW? I have no problems creating my DLL and sending images back & forth between LabVIEW and OpenCV DLL's.

Here is the code I am working with:

 

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2\highgui\highgui.hpp>

using namespace std;
using namespace cv;

// extern C
extern "C" {

	_declspec (dllexport) int MyHistCalc(unsigned char *imageIN, int rows, int cols, double threshold1, double threshold2, int kernel_size, unsigned char *imageOUT, unsigned char *HistCalcImageOUT);
}



_declspec (dllexport) int MyHistCalc(unsigned char *imageIN,
	int rows,
	int cols,
	double threshold1,
	double threshold2,
	int kernel_size,
	unsigned char *imageOUT,
	unsigned char *HistCalcImageOUT)
{
	Mat image_input(rows, cols, CV_8U, &imageIN[0]); // THIS IS THE INPUT IMAGE, POINTER TO DATA
	Mat image_output(rows, cols, CV_8U, &imageOUT[0]); // THIS IS THE INPUT IMAGE, POINTER TO DATA
	Mat image_histcalcoutput(512, 400, CV_8U, &HistCalcImageOUT[0]);

	//Mat gray = imread("image.jpg", 0);
	namedWindow("Gray", 1);    imshow("Gray", image_input);
	image_output = image_input;
	namedWindow("Image Output", 1);    imshow("Image Output", image_output);
	// Initialize parameters
	int histSize = 256;    // bin size
	float range[] = { 0, 255 };
	const float *ranges[] = { range };

	// Calculate histogram
	MatND hist;
	//hist is a 256x1 array

	calcHist(&image_input, 1, 0, Mat(), hist, 1, &histSize, ranges, true, false);

	// Plot the histogram
	int hist_w = 512; int hist_h = 400;
	int bin_w = cvRound((double)hist_w / histSize);

	Mat histImage(hist_h, hist_w, CV_8UC1, Scalar(0, 0, 0));
	normalize(hist, hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
	//image_histcalcoutput = hist;

	for (int i = 1; i < histSize; i++)
	{
		line(histImage, Point(bin_w*(i - 1), hist_h - cvRound(hist.at<float>(i - 1))),
			Point(bin_w*(i), hist_h - cvRound(hist.at<float>(i))),
			Scalar(255, 0, 0), 2, 8, 0);
	}

	image_histcalcoutput = histImage;
	namedWindow("CalcHist Result 1", 1);    imshow("CalcHist Result 1", histImage);
	namedWindow("CalcHist Result 2", 1);    imshow("CalcHist Result 2", image_histcalcoutput);

	waitKey(0);
	return histSize;
}

I am unsure if my problem is catching image_histcalcoutput incorrectly in LabVIEW or returning result correctly.

 

Please find screenshot of my VI attached.

0 Kudos
Message 1 of 4
(3,456 Views)
why you do not use labview vision histogram? it is so easy to use
0 Kudos
Message 2 of 4
(3,447 Views)

My question (and implementation) is part of a bigger problem with portions already utilising OpenCV.

0 Kudos
Message 3 of 4
(3,445 Views)