目录
在halcon中经常会用到图像变换的操作,然后这次作业是用矩阵来实现相关算子的功能,学到了挺多的所以就记录下来方便复习。
图像变换介绍
基础部分可以看这篇文章,写的挺好的
用Halcon自带的算子实现图像变换
在学习矩阵先用算子实现一遍,毕竟这些封装好算子才是高频使用的东西,而且也可以方便
接下来自己实现时比对效果
read_image (Image, 'datacode/ecc200/ecc200_to_preprocess_001.png')
hom_mat2d_identity (HomMat2DIdentity)
*平移
hom_mat2d_translate (HomMat2DIdentity, 64, 64, HomMat2DTranslate)
affine_trans_image (Image, ImageAffineTrans, HomMat2DTranslate, 'constant', 'false')
*缩放
hom_mat2d_scale (HomMat2DIdentity, 2, 2, 0, 0, HomMat2DScale)
affine_trans_image (Image, ImageAffineTrans1, HomMat2DScale, 'constant', 'false')
*旋转
hom_mat2d_rotate (HomMat2DIdentity, 0.78, 0, 0, HomMat2DRotate)
affine_trans_image (Image, ImageAffineTrans2, HomMat2DRotate, 'constant', 'false')
*镜像
get_image_size (Image, Width, Height)
hom_mat2d_reflect (HomMat2DIdentity, 0, Width/2, 10, Width/2 , HomMat2DReflect1)
affine_trans_image (Image, ImageAffineTrans3, HomMat2DReflect1, 'constant', 'false')
这里我提一嘴hom_mat2d_reflect的第2到第5个的参数。这其实是两个坐标点,halcon根据这两个坐标形成的线来当作对称轴。比如我这里的坐标点是(0, Width/2)和(10, Width/2),也就是图像列的中点,所以图像会左右镜像。
顺便一提,这个图片是halcon自带的,打开read_image的算子窗口点击文件夹的图标就可以找到你的halcon的图片存放的路径,上面我已经改成相对路径所以应该可以直接运行
使用矩阵来实现相关算子的功能
一、平移
基本的思路是将图像的每个坐标和矩阵相乘得到新的坐标,然后将旧坐标的像素值赋与新坐标
首先创建一个矩阵
*平移
*[1,0,x,\
*0,1,y,\
*0,0,1]
create_matrix (3, 3, [1,0,64,\
0,1,64,\
0,0,1], MatrixID)
平移矩阵形式如上,x和y分别是你在x轴和y轴上的平移量。
然后遍历图片,将每个坐标与矩阵相乘,在这里我们先创建一个新的空图像,然后将变换后的图输入到新图像中,这样就不会覆盖原图
*使用gen_image_const 创建一个空图像
get_image_size (Image, Width, Height)
gen_image_const (Imageout, 'byte', Width, Height)
for x := 0 to Height - 1 by 1
for y := 0 to Width - 1 by 1
*得到原图的坐标点的像素值
get_grayval (Image, x, y, Grayval)
*将坐标转化为矩阵形式以用于矩阵相乘
create_matrix (3, 1, [x,\
y,\