Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem in returning images in Python for LabVIEW

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)

enter image description here

enter image description here

like image 360
Saberi Avatar asked Apr 21 '26 00:04

Saberi


1 Answers

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

LabVIEW Code to Pass a Grayscale Image to Python and Return an RGB Image

Front Panel of the VI

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

like image 181
John Avatar answered Apr 23 '26 12:04

John



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!