Machine Vision

cancel
Showing results for 
Search instead for 
Did you mean: 

How can I get an image back to labview from opencv dll code?

Solved!
Go to solution

Hi, I'm using the  vi below to aquire an image from labview then pass it to an opencv dll code through , first getting image pixel pointer and then passing the result parameters to create a "Mat " in my c++ code. Untill this point everything works. Now I do some processing in opencv  the final resultiing image I want to bring it back to LabView. This I can't figure it out. (btw the image is a binary image). How can I get this image back??? please help 

 

P.S. actually what I get in image3 is the same image I captured from LV.

 

VI.png

0 Kudos
Message 1 of 15
(12,567 Views)

Hello,

 

first of all, if you've got only a binary image, U8 datatype is sufficient. Also, be careful since Labview uses RGB and OpenCV (by default) BGR notation.

 

I have never used the "map pixel pointer" in Labview, instead I always copy the image to a 2D array and than pass a pointer to Mat pixel matrix. I have never seen any performance issues with this. Using high resolution counter, it takes ~1.2 ms on average to copy images with the size of 1920x1080 pixels (on my low(mid) range 3 years old laptop). Of course, the "map pixel pointer" should be faster, but I cannot say how much faster.

 

There are numerous examples here if you are interested:

 

https://decibel.ni.com/content/blogs/kl3m3n

 

In your case, try using:

 

Mat your_image(rows,cols,CV_8U,&imdata[0])

 

where the function prototype is:

 

_declspec (dllexport) int your_funciton (uchar *imdata, int cols, int rows)

 

and then process the image (your_image).

 

You can attach a simple C++ source code along with the Labview program in order to help you further.

 

Best regards,

K

 

P.S. I see now that the pixel pointer is U64 datatype. Is this the only option?

It also seems that you need to unmap the pointer:

 

http://zone.ni.com/reference/en-XX/help/370281P-01/imaqvision/imaq_getimagepixelptr_example/

 

 

 


https://decibel.ni.com/content/blogs/kl3m3n



"Kudos: Users may give one another Kudos on the forums for posts that they found particularly helpful or insightful."
0 Kudos
Message 2 of 15
(12,562 Views)
Solution
Accepted by RYagan

Hello,

 

a simple example:

 

OpenCV code:

 

#include <opencv2/imgproc/imgproc.hpp>

using namespace std;
using namespace cv;

// extern C
extern "C" {	
	
	_declspec (dllexport) int CannyEdges(unsigned char *imageIN, int rows, int cols, double threshold1, double threshold2, int kernel_size, unsigned char *imageOUT);
}



_declspec (dllexport) int CannyEdges(unsigned char *imageIN,
				     int rows,
				     int cols,
				     double threshold1, 
				     double threshold2,
				     int kernel_size,
   				     unsigned char *imageOUT)
{
	// check odd kernel size
	if(kernel_size % 2 == 0) return -1;
	// allocate memory
	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 OUTPUT IMAGE, POINTER TO DATA
	// blur before canny
	blur(image_input, image_output, Size(3,3));
	// canny
	Canny(image_output, image_output, threshold1, threshold2, kernel_size);
	return 0;
}

You can operate directly on the "imageIN" by eliminating the image_output and replacing with image_input.

 

Labview code:

 

test_BD.png

 

I am attaching the Labview code (2013) and the .dll for you to test.

 

Hope this helps.

 

Regards,

K


https://decibel.ni.com/content/blogs/kl3m3n



"Kudos: Users may give one another Kudos on the forums for posts that they found particularly helpful or insightful."
Message 3 of 15
(12,537 Views)

That looks interesting, but in my case I have to bring a RGB image not a grayscaled one. 

0 Kudos
Message 4 of 15
(12,518 Views)

Thanks alot for your help. I managed to merge both techniques (what you suggested+the image pixelpointer) and I got my result. 🙂

 

0 Kudos
Message 5 of 15
(12,486 Views)

@RYagan wrote:

That looks interesting, but in my case I have to bring a RGB image not a grayscaled one.


 

Just declare a pointer to uint32, instead of uint8. Modify the code and it should work.

 

Anyway, great you managed to get it working. Did you consider rgb to bgr conversion?  Just curious.

 

Regards,

K

 

 

 

 

 

 

 


https://decibel.ni.com/content/blogs/kl3m3n



"Kudos: Users may give one another Kudos on the forums for posts that they found particularly helpful or insightful."
0 Kudos
Message 6 of 15
(12,477 Views)

I'm not sure if that will work, what I've tried to do before is to wire my RGB image to the Image to array block but LV showed an error. Also, the array can't be encoded to U32 type. (not showing in the block terminals)

 

Also, I didn't perform rgb to bgr conversion between LV and OpenCV. Doing this will invert my image colors wrong wise . OpenCV accepeted the image as is. 

 

 

0 Kudos
Message 7 of 15
(12,471 Views)

Hello,

 

a late reply, but if you create a CV_8UC4 image in OpenCV and pass the pointer to the U32 image array it works properly.

The array can be encoded in U32 (RGBA image), but you will find this on the color pallete not grayscale.

 

Can you share your solution? Just out of curiosity.

 

Best regards,

K


https://decibel.ni.com/content/blogs/kl3m3n



"Kudos: Users may give one another Kudos on the forums for posts that they found particularly helpful or insightful."
Message 8 of 15
(11,855 Views)

I suspect that opencv expects a grey scale image for canny.

i.e it will fail with any other type.

0 Kudos
Message 9 of 15
(11,529 Views)

Hello,

 

yes, it needs 8-bit single channel image:

 

http://docs.opencv.org/modules/imgproc/doc/feature_detection.html?highlight=canny#canny

 

Regards,

K


https://decibel.ni.com/content/blogs/kl3m3n



"Kudos: Users may give one another Kudos on the forums for posts that they found particularly helpful or insightful."
0 Kudos
Message 10 of 15
(11,523 Views)