As the commenters on your post have suggested, it appears that the python code and LabVIEW code are expecting different types. When you perform the test just in Python the code adapts as required to show the image but the types need to match when passing between the two environments.
As per the OP's comment below, we need to pass a grayscale image and return an RGB image.
The grayscale image is easier as it is a 2D array of uint8
types. We can convert a Grayscale IMAQ image into the correct array type using IMAQ ImageToArray.vi
.
When it comes to passing an RGB image back to LabVIEW we need to know the following:
- In OpenCV an RGB image is a 2-dimensional image with multiple "channels". Each channel represents one of the colours and the OpenCV convention is to store the channels in the Blue-Green-Red channel order
- In LabVIEW IMAQ RGB images are represented as a 2-dimensional image of unsigned 32-bit integers. The most significant byte is the Alpha channel which IMAQ cannot handle but is still stored. The next byte is the Red Channel, then the Green Channel and finally the least significant byte is the Blue Channel
We have two options - we can either format the image data before passing it from the Python side or we can take the Python image data as-is and transform it to the format LabVIEW/IMAQ needs in LabVIEW.
In The example code below I choose the latter (because I have more experience manipulating data in LabVIEW). Once theRGB image data is an array of U32 integers we can use the IMAQ ArrayToColorImage.vi
to write the data to the IMAQ image.
The associated Python code is
import numpy as np
import cv2
def threshold(data):
gray = np.array(data,dtype=np.uint8)
# perform threshold operation
ret, thresh1 = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)
#
# create an RGB image to demonstrate output
#
height = 200
width = 300
rgb = np.zeros((height,width,3), np.uint8)
# create RGB verticle stripes
# note cv2 channels are arranged BGR
# red stripe
rgb[:,0:width//3] = (0,0,255)
# green stripe
rgb[:,width//3:2*width//3] = (0,255,0)
# blue stripe
rgb[:,2*width//3:width] = (255,0,0)
# return rgb 3d-array
return rgb


Note - the labVIEW code is attached as a VI snippet so you should be able to drag it into a fresh LabVIEW Block-Diagram
Alternatively all the code is in this github gist