Halcon算子应用和技巧14
提示:若没有查找的算子,可以评论区留言,会尽快更新
Halcon算子应用和技巧14
- 前言
- 一、Halcon应用?
- 二、算子汇总
- 三、应用算子
- 131. rgb1_to_gray()
- 132. decompose3()
- 133. trans_from_rgb ()
- 134. get_region_index()
- 135. get_mbutton()
- 136. abs_diff_image ()
- 137. full_domain ()
- 138. get_domain ()
- 139. rectangle1_domain()
- 140. expand_domain_gray()
前言
提示:可以使用搜索小工具搜索对应算子名称:
本篇博文主要用于记录学习Halcon中算子的应用场景,及其使用代码和图像展示。只讲通俗易懂使用方法,不讲原理,不讲原理,不讲原理,重要的事情说三遍。
提示:以下是本篇文章正文内容,下面案例可供参考,注意参数坐标的使用,能帮助你理解算子
一、Halcon应用?
Halcon 是一个强大的图像处理工具,该工具是为了解决机器视觉项目任务而创建的。
二、算子汇总
每一博文仅展示10个算子,点击此链接进行查询所有算子,并点击对应算子跳转相应博文。
【跳转链接】
三、应用算子
131. rgb1_to_gray()
先上代码:
read_image (Image, 'D:/HALCON_learn/理论/131-140/水果.jpg')
rgb1_to_gray (Image, GrayImage)
解析
将彩色图像转为灰度图像,也将变为单通道图像
Gray = R0.299 + G0.587 + B*0.114
132. decompose3()
先上代码:
read_image (Image, 'D:/HALCON_learn/理论/131-140/水果.jpg')
decompose3 (Image, R, G, B)
解析
将多通道彩色图像拆分为R G B 三个单通道的图像,RGB相互组合可以组成任意色彩,即三原色
可以观察到不同颜色在不同通道所占分量不同,原图色彩在对应通道会更加亮,比如苹果的
133. trans_from_rgb ()
先上代码:
read_image (Image, 'D:/HALCON_learn/理论/131-140/水果.jpg')
decompose3 (Image, R, G, B)
trans_from_rgb (R, G, B, H, S, V, 'hsv')
解析
从RGB空间,转换到HSV空间,即色调,饱和度,亮度,三个层面去看待图像,这对于处理有颜色的需求会有很大帮助
HSV:建议去百度一下,理解六棱锥的含义,就很好理解HSV
HSV
134. get_region_index()
先上代码:
read_image (Image, 'D:/HALCON_learn/理论/131-140/tu1.png')
rgb1_to_gray (Image, GrayImage)
threshold (GrayImage, Region, 128, 255)
connection (Region, ConnectedRegions)
get_region_index (ConnectedRegions, 123, 417, Index)
解析
获得你指定像素点的对象(obj)索引, 配合select_obj() 使用
135. get_mbutton()
先上代码
read_image (Image, 'D:/HALCON_learn/理论/131-140/tu1.png')
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
rgb1_to_gray (Image, GrayImage)
threshold (GrayImage, Region, 128, 150)
connection (Region, ConnectedRegions)
get_mbutton (WindowHandle, Row, Column, Button)
get_region_index (ConnectedRegions, Row, Column, Index)
select_obj (ConnectedRegions, ObjectSelected, Index)
解析
配合get_region_index()使用鼠标选择出你想要的对象,实际功能是过的鼠标点击的像素点位置
可以将上面代码复制,实际运行一下看效果,鼠标点击的过程没办法图像记录
136. abs_diff_image ()
先上代码
read_image (Image, 'D:/HALCON_learn/理论/131-140/tu1.png')
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
rgb1_to_gray (Image, GrayImage)
get_image_size (GrayImage, Width, Height)
gen_image_const (Image1, 'byte', Width, Height)
abs_diff_image (Image1, GrayImage, ImageAbsDiff, 1)
sub_image (Image1, GrayImage, ImageSub, 1, 0)
解析
图像相同坐标下的像素点灰度值相减的绝对值,和sub_image (),不同,比如Image1是一张灰度值全0的图,该图减去卡通图依然是卡通图,但是使用sub_image()得到的是全零的图,因为灰度值相减后小于0的值会截断变为0。
abs_diff_image ()公式:g’ = |(g1 - g2)| * Mult
sub_image() 公式:g’ := (g1 - g2) * Mult + Add
137. full_domain ()
先上代码:
read_image (Image, 'D:/HALCON_learn/理论/131-140/tu1.png')
rgb1_to_gray (Image, GrayImage)
get_image_size (GrayImage, Width1, Height1)
dev_open_window (0, 0, Width1/2, Height1/2, 'black', WindowHandle)
rotate_image (GrayImage, ImageRotate, 15, 'constant')
get_image_size (ImageRotate, Width, Height)
full_domain (ImageRotate, ImageFull)
解析
是否曾经困扰过你,无效区域如何处理,今天它来了,将“-”变为“0”,神奇不,这样就可以处理这个位置像素了
算子的作用是扩充到全域,full_domain的真实作用,就是让整个域中的图片都显示出来,没有图片的地方用0填充。
full_domain参考文章
138. get_domain ()
先上代码:
read_image (Image, 'D:/HALCON_learn/理论/131-140/tu1.png')
rgb1_to_gray (Image, GrayImage)
get_image_size (GrayImage, Width1, Height1)
dev_open_window (0, 0, Width1/2, Height1/2, 'black', WindowHandle)
rotate_image (GrayImage, ImageRotate, 15, 'constant')
get_image_size (ImageRotate, Width, Height)
get_domain (ImageRotate, Domain)
full_domain (ImageRotate, ImageFull)
get_domain (ImageFull, Domain1)
解析
通俗理解就是获得有效的计算区域,“_”将不是有效计算区域,参考下图
经过full_domain(),有效区域增加,就补全了能够计算的区域
139. rectangle1_domain()
先上代码:
read_image (Image, 'D:/HALCON_learn/理论/131-140/tu1.png')
rgb1_to_gray (Image, GrayImage)
rectangle1_domain (GrayImage, ImageReduced, 25,231, 577,839)
解析
通过指定矩形位置指定有效区域,相比较reduce_domain(),节省一步创建矩形区域的步骤
黑色区域灰度值都是 “-”。
140. expand_domain_gray()
先上代码:
read_image (Image, 'D:/HALCON_learn/理论/131-140/tu1.png')
rgb1_to_gray (Image, GrayImage)
threshold (GrayImage, Region, 128, 150)
connection (Region, ConnectedRegions)
get_region_index (ConnectedRegions, 841,476, Index)
select_obj (ConnectedRegions, ObjectSelected, Index)
reduce_domain (GrayImage, ObjectSelected, ImageReduced1)
expand_domain_gray (ImageReduced1, ExpandedImage,30)
解析
扩展有效计算区域,比如经过reduce_domain()的image,经过expand_domain_gray()可以获得增加范围的有效区域,扩展范围就是你设定的参数,如30。
理解前提是你对reduce_domain()的结果知晓含义。
以上内容陆续更新。。。
如有问题,欢迎大家指出,谢谢!!!