I am just starting to learn LabVIEW. I want to get a threshold from my image in a python function and display the image in LabVIEW. But when the function returns the image, it gives an error in LabVIEW. I am sending the relevant code in Python and the LabVIEW program as an attachment. Thanks
import numpy as np
import cv2
def thershold(data):
gray = np.array(data,dtype=np.uint8)
ret, thresh1 = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)
return np.array( thresh1, dtype=np.float64)
if __name__ == '__main__':
data = cv2.imread('C:/Users/user00/Desktop/LabView/1120220711_151148.tiff', 1)
thresh = thershold(data)
cv2.imshow('thresh1',thresh)
cv2.waitKey(0)


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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With