python版halcon 转numpy

新版Halcon支持python接口,Python函数和halcon函数名几乎完全相同,用python写一些对性能要求不高的插件还是很舒服的.(halcon-python环境配置可以看我之前的文章)

众所周知python中图像是用numpy表示的,python常用的图像处理库matplotlib,pillow,opencv-python,Scipy,pgmagick....等等...所以图像转成numpy很方便和其他模块融合.

开始:函数大概是这样的:

def ha_2_np(image):
    before = time.time()
    image_converted = ha.convert_image_type(image, 'byte')
    channel_num_list = ha.count_channels(image_converted)
    img_pointer, img_type, img_width_list, img_height_list = ha.get_image_pointer1(image_converted)
    #img_width_list, img_height_list = ha.get_image_size(image_converted)
    img_p=img_pointer[0] #img_p 这里大概是一个指针
    img_X=img_width_list[0]
    img_Y=img_height_list[0]
    img_channel=channel_num_list[0]
    new_img = np.empty((img_Y, img_X, img_channel), dtype=np.uint8)
    for j in range(img_X):
        for i in range(img_Y):
            img_gray_value = ha.get_grayval(image_converted, i, j) #效率很低
            if img_channel == 1:
                new_img.itemset((i,j,0),img_gray_value[0])
            else:
                if channel_num_list[0] == 3:
                    new_img[i][j][0] = img_gray_value[2]
                    new_img[i][j][1] = img_gray_value[1]
                    new_img[i][j][2] = img_gray_value[0]  
    after = time.time()
    time_consuming=after-before
    return time_consuming,new_img

get_image_pointer1返回一个指针.(怎么把数据拿出来放进numpy??) time_consuming是这个函数运行时间.

img_p不知但怎那么取出数据.只能get_grayval挨个像素取.(效率极低,8位1280*1024的图需要27秒).即使python出了名的慢27秒也是不能忍受的,于是开始想办法优化.(python不是瓶颈,pareto原则:80%的的运行耗时是由20%代码引起的.) 过早优化是万恶之源,然而27-58秒是不能忍受的.

开始想用,ctypes,或者Cython,甚至pythonnet用.net把转换过程打包返回给python,pythonnet??那还不如换C#来的快,看了两天ctypes,看了两天Cython,感觉有些眉目了,就在看ctypes的时候发现了新大陆.

    #C:\Users\Administrator\AppData\Local\Programs\Python\Python38\Lib\site-packages\halcon
    #numpy_interop.py
#这个位置有两个函数,看名字都知道干嘛用的...
r"""
    'himage_from_numpy_array',
    'himage_as_numpy_array',
"""

原来官方已经有这玩意了,白给.

def ha_ha_2_np(image):
    before = time.time()
    img=ha.himage_as_numpy_array(image)
    after = time.time()
    time_consuming=after-before    
    return time_consuming,img
def hanp__2_ha(img):
    before = time.time()
    image=ha.himage_from_numpy_array(img) 
    after = time.time()
    time_consuming=after-before    
    return time_consuming,image

r"""
耗时
0.000997781753540039
耗时
0.009822607040405273
"""

可以看到:现在可以在:<class 'halcon.hobject.HObject'>和numpy可以自由转换了.

import halcon as ha
import cv2
import numpy as np
import time
from ctypes import *

r"""
自用操作
"""
from pythonnet_lib import * 
from pyhalcon_lib import *
from CV_class import *
 
def ha_2_np(image):
    before = time.time()
    image_converted = ha.convert_image_type(image, 'byte')
    channel_num_list = ha.count_channels(image_converted)
    img_pointer, img_type, img_width_list, img_height_list = ha.get_image_pointer1(image_converted)
    print(f"""转换: 
    image_converted :     
    type:{type(image_converted)}    :    
    len:{len(image_converted)}    
    :{image_converted}  
   channel_num_list:    {type(channel_num_list)}:    {len(channel_num_list)}: {channel_num_list}
       img_pointer :     {type(img_pointer)}    :    {len(img_pointer)}    :  {img_pointer}    
       img_type    :     {type(img_type)   }    :    {len(img_type)   }    :  {img_type} 
       img_width   :     {type(img_width_list)  }    :    {len(img_width_list)  }    :  {img_width_list} 
       img_height  :     {type(img_height_list) }    :    {len(img_height_list) }    :  {img_height_list} 
    """)
    img_p=img_pointer[0]
    print(f" img_p :{img_p}")
    #C:\Users\Administrator\AppData\Local\Programs\Python\Python38\Lib\site-packages\halcon
    #numpy_interop.py
    print(ha.himage_as_numpy_array.__doc__)
    #img_width_list, img_height_list = ha.get_image_size(image_converted)
    img_X=img_width_list[0]
    img_Y=img_height_list[0]
    img_channel=channel_num_list[0]
    new_img = np.empty((img_Y, img_X, img_channel), dtype=np.uint8)
    for j in range(img_X):
        for i in range(img_Y):
            img_gray_value = ha.get_grayval(image_converted, i, j)
            if img_channel == 1:
                new_img.itemset((i,j,0),img_gray_value[0])
            else:
                if channel_num_list[0] == 3:
                    new_img[i][j][0] = img_gray_value[2]
                    new_img[i][j][1] = img_gray_value[1]
                    new_img[i][j][2] = img_gray_value[0]  
    after = time.time()
    time_consuming=after-before
    return time_consuming,new_img


def ha_ha_2_np(image):
    before = time.time()
    img=ha.himage_as_numpy_array(image)
    after = time.time()
    time_consuming=after-before    
    return time_consuming,img
def hanp__2_ha(img):
    before = time.time()
    image=ha.himage_from_numpy_array(img) 
    after = time.time()
    time_consuming=after-before    
    return time_consuming,image

if __name__ == "__main__":
    
    Image = ha.read_image('Lena')
    ty,img=ha_ha_2_np(Image)
    print("耗时")
    print(ty)    
    img_bgr=cv2.cvtColor(img,cv2.COLOR_RGB2BGR)
    ty,h_img=hanp__2_ha(img_bgr)
    print("耗时")
    print(ty)
    thread_it(SHOW,img,"opencv_img")
    thread_it(SHOW,img_bgr,"opencv_bgr")

    Width, Height = ha.get_image_size(Image)
    print(Width[0], Height[0])
    WindowHandle =open_window(Width[0]/2, Height[0]/2)
    gray = ha.rgb1_to_gray(Image)
    thres = ha.threshold(gray, 100, 200)
    ha.disp_obj(Image, WindowHandle);cmd()
    ha.clear_window(WindowHandle)
    ha.disp_obj(thres, WindowHandle);cmd()
    

24位512*512的图像转换用时不到1ms,

未完待续....

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值