Python版Halcon图像转OpenCV图像

新版本的Halcon支持Python接口,Python函数和Halcon函数名完全相同,用python写更容易一些。

import halcon as ha
import cv2
import numpy as np

def Halcon2OpenCV(filename, output_filename):
    image = ha.read_image(filename)
    image_converted = ha.convert_image_type(image, 'byte')
    channel_num_list = ha.count_channels(image_converted)
    # not like the C++, no pointer in Python
    # img_pointer, img_type, img_width, img_height = ha.get_image_pointer1(image_converted)
    img_width_list, img_height_list = ha.get_image_size(image_converted)
    new_img = np.empty((img_height_list[0], img_width_list[0], channel_num_list[0]), dtype=np.uint8)

    for j in range(img_width_list[0]):
        for i in range(img_height_list[0]):
            img_gray_value = ha.get_grayval(image_converted, i, j)
            if channel_num_list[0] == 1:
                new_img[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]          
                           
    cv2.imwrite(output_filename, new_img)
    return new_img

比较坑的一点是get_image_pointer1返回的是指针,不知道如何找到图片对象,只能一个字节一个字节的复制了,效率比较低

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值