Python版Halcon图像转OpenCV图像

这篇博客介绍了如何利用Halcon的新Python接口将图像转换为OpenCV格式。文章提到,虽然Halcon函数与Python函数名相同,使得使用Python更加便捷,但遇到的一个挑战是get_image_pointer1函数返回的是指针,不直接支持Python,因此需要逐字节复制图像数据,导致效率降低。作者提供了详细的代码示例展示如何进行转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

新版本的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返回的是指针,不知道如何找到图片对象,只能一个字节一个字节的复制了,效率比较低

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值