Python版Halcon图像转OpenCV图像
程序员文章站
2022-05-28 14:29:09
...
新版本的Halcon支持Python接口,Python函数和Halcon函数名完全相同,用python写更容易一些。
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返回的是指针,不知道如何找到图片对象,只能一个字节一个字节的复制了,效率比较低