gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 12:02:09 24 4
gpt4 key购买 nike

Halcon Progress 20 为不同的协议(protocol)(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. Examplesfor these are get_image_pointer1, get_region_runs, or get_contour_xld. Operators like these allow fullaccess 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 bedeveloped 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 errorwhile 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

关于c - Halcon/HDevelop 套接字发送通用图像数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64893249/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com