Sending image from HALCON to Python

727 Views Asked by At

I want to take a picture of an object in HALCON and send it to Python. I will process the image there and send the result back to HALCON. So far, I wrote/found the code below and was successful to send the image to Python. However, it took so long time. I heard that sending an image via base64 is much faster. Do you know anything about that?

Protocol := 'TCP4'
Timeout := 100.0

#Open a socket connection
open_socket_connect ('192.168.20.155', 60000, ['protocol','timeout'], [Protocol,Timeout], Socket)

get_socket_param (Socket, 'address_info', Address)

#To is not evaluated for TCP and connected UDP sockets
To := []

read_image (Image, 'printer_chip/printer_chip_01')

#Make sure we have only one channel
rgb1_to_gray (Image, GrayImage)

get_image_pointer1 (GrayImage, _, _, Width, Height)    

RowIndexes := []
for j:=0 to Height-1 by 1
    tuple_gen_const (Width, j, TempIndexes)
    RowIndexes := [RowIndexes,TempIndexes]
endfor

ColIndexes := []
tuple_gen_sequence (0, Width-1, 1, Sequence)
for i:=0 to Height-1 by 1
    ColIndexes := [ColIndexes,Sequence]
endfor

#get pixel values
get_grayval (GrayImage, RowIndexes, ColIndexes, Data)

#C: one byte = 8 bit, Unsigned
Format := 'C' + Width*Height

send_data (Socket, Format, Data, To)

receive_data (Socket, 'z', Answer, From)

stop ()

close_socket (Socket)
1

There are 1 best solutions below

0
On

I was able to speed up getting gray values data by using only one loop

get_image_size(GrayImage, Width, Height)
tuple_gen_sequence(0, Width - 1, 1, SequenceCols)
Data := []

for Index := 0 to Height - 1 by 1
    tuple_gen_const(Width, Index, SequenceRows)
    get_grayval (GrayImage, SequenceRows, SequenceCols, tempData)
    tuple_concat(Data, tempData, Data)
endfor