首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Halcon/HDe信封套接字发送通用图像数据

Halcon/HDe信封套接字发送通用图像数据
EN

Stack Overflow用户
提问于 2020-11-18 12:35:02
回答 2查看 376关注 0票数 1

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

关于程序可视化的Halcon解决方案指南规定如下:

有时可能不需要应用标准的HALCON可视化操作符,而是使用自编程版本。这可以通过使用为所有数据类型提供的访问函数来实现。这些例子有get_image_pointer1get_region_runsget_contour_xld。这些操作符允许完全访问所有内部数据类型。此外,它们以各种形式(例如,游程编码、点或等值线)提供数据,以使进一步的处理更容易。在此基础上,可以方便地开发出一个自编程可视化系统。

下面是我认为应该如何工作的一个基本例子:

代码语言:javascript
复制
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], [])抛出一个异常:

未处理程序异常:

在过程'send_data‘中调用’send_data‘时出现的HALCON操作符错误: 78。

格式规范与数据不匹配(HALCON错误代码: 5628)

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

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

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

代码语言:javascript
复制
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内部实际发送到接收套接字。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-11-18 20:53:09

这是我希望能帮到你的代码。它序列化并发送数据。

代码语言:javascript
复制
* 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)

Edit2

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

代码语言:javascript
复制
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
票数 1
EN

Stack Overflow用户

发布于 2022-06-03 06:25:56

我修改了上面的代码,并将其发送给Python。我得到了结果。但花了这么长时间。有办法用base64发送图像吗?

代码语言:javascript
复制
Protocol := 'TCP4'
Timeout := 1.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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64893249

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档