Halcon/HDevelop 套接字发送通用图像数据

Halcon/HDevelop socket send generic image data

Halcon Progress 20 为不同协议(HALCON、UDP、TCP)提供套接字,send_data(Socket, Format, Data, To) procedure 使用通用套接字通信发送任意数据。如何使用此过程将图像从 hdevelop 发送到另一个连接的套接字?

程序可视化的 Halcon 解决方案指南指出如下:

Sometimes it might be necessary not to apply the standard HALCON visualization operators, but to use a selfprogrammed version. This can be achieved by using the access functions provided for all data types. Examples for these are get_image_pointer1, get_region_runs, or get_contour_xld. Operators like these allow full access to all internal data types. Furthermore, they provide the data in various forms (e.g., runlength encoding, points, or contours) to make further processing easier. Based on this data, a self programmed visualization can be developed easily.

这是我认为应该如何工作的基本示例:

read_image (Image, 'printer_chip/printer_chip_01')

* Send image via socket
Protocol := 'TCP4'
Timeout := 5.0
* Open a socket connection
open_socket_connect ('127.0.0.1', 5000, ['protocol','timeout'], [Protocol,Timeout], Socket)
* To is only used with protocol HALCON or UDP (empty for bound TCP connections)
To := []
* The following send_data is working, the connected socket receives this message
send_data (Socket, 'z', ['Generic sockets are great!'], To)

* Make sure we have only one channel
rgb1_to_gray (Image, GrayImage)
get_image_pointer1 (GrayImage, Pointer, Type, Width, Height)

* get_image_pointer returns pointer of type 'byte' -> use a byte format specifier for socket
* c: one byte = 8 bit, signed
* Append the type specifier with a number for a repeat count, e.g. 'c5' means 'ccccc'
Format := 'c' + Width*Height
* Format := 'c'
* How to cast the raw pointer correctly to send the data it points to?
send_data (Socket, Format, [Pointer], [])

最后一行send_data (Socket, Format, [Pointer], [])抛出异常:

Unhandled program exception:

HALCON operator error while calling 'send_data' in procedure 'main' line: 78.

Format specification does not match the data (HALCON error code: 5628)

显然 Pointer 是指向图像在内存中的地址,而不是可能导致错误的实际数据。

有没有办法在 HDevelop 中正确转换原始指针以通过套接字发送它? 或者这只能在使用 Halcon 库的外部 C/C++/C# 应用程序中完成吗?

Halcon documentation on gen_image_pointer1 仅提供了这个 C 示例:

Hobject  Image;
char     typ[128];
Hlong     width,height;
unsigned char *ptr;

read_image(&Image,"fabrik");
get_image_pointer1(Image,(Hlong*)&ptr,typ,&width,&height);

我想使用 HDevelop 的功能,而不是使用 C/C++/C# 中的套接字(尽管这是另一种方式)。唯一缺少的是实际将图像从 HDevelp 发送到接收套接字。

代码在这里希望能帮到你。 它序列化数据并发送它们。

* Send image via socket
Protocol := 'TCP4'
Timeout := 5.0
* Open a socket connection
open_socket_connect ('127.0.0.1', 5000, ['protocol','timeout'], [Protocol,Timeout], Socket)
get_socket_param (Socket, 'address_info', Address)
* 
* To is not evaluated for TCP and connected UDP sockets
To := []

* 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)

[编辑2]

可以使用以下代码优化生成 RowIndexes 和 ColIndexes 的代码部分:

tuple_gen_const (Width*Height, 0, RowIndexes)
tuple_gen_const (Width, 0, Const)
tuple_gen_sequence (0, Width-1, 1, Sequence)
for j:=1 to Height-1 by 1    
    IndexSequence:= Sequence+j*Width    
    RowIndexes[IndexSequence] := Const+j
endfor    

tuple_gen_const (Width*Height, 0, ColIndexes)
for i:=0 to Height-1 by 1
    IndexSequence:= Sequence+i*Width 
    ColIndexes[IndexSequence] := Sequence
endfor

我稍微修改了上面的代码并发送给Python。我得到了结果。但这需要很长时间。有没有办法用base64发送图片?

协议 := 'TCP4' 超时 := 1.0

打开套接字连接

open_socket_connect ('192.168.20.155', 60000, ['protocol','timeout'], [协议,超时], 套接字)

get_socket_param(套接字,'address_info',地址)

不针对 TCP 和已连接的 UDP 套接字评估 To

至 := []

read_image(图片,'printer_chip/printer_chip_01')

确保我们只有一个频道

rgb1_to_gray(图像,灰度图像)

get_image_pointer1(灰度图像,_,_,宽度,高度)

行索引 := [] 对于 j:=0 到 Height-1 乘以 1 tuple_gen_const(宽度,j,温度指数) RowIndexes := [RowIndexes,TempIndexes] 结束

列索引 := [] tuple_gen_sequence (0, Width-1, 1, Sequence) for i:=0 to Height-1 by 1 ColIndexes := [ColIndexes,序列] 结束

获取像素值

get_grayval(灰度图像、行索引、列索引、数据)

C: 一个字节 = 8 位,无符号

格式 := 'C' + 宽*高

send_data(套接字、格式、数据、目标)

receive_data(套接字,'z',答案,发件人)

停止()

close_socket(插座)