Halcon 二维变换中的两种图像坐标系
像素是离散的,为了解决它们,我们有一个仅使用整数值的坐标系,即像素坐标系。但是为了获得超出像素网格的更高精度,我们需要浮点坐标,这导致亚像素精确坐标系。在 HALCON 中,我们有三种不同的亚像素坐标系实现:
- 像素中心坐标 (Pixel Centered Coordinates),HALCON 标准子像素坐标系
- 边中心坐标 (Edge Centered Coordinates)
- 极坐标 (Polar Coordinates)
Halcon标准坐标系
- 像素精确坐标系
像素坐标系将图像视为离散元素的网格,即像素,我们将原点放在左上角像素的中间(0,0);
图像大小: h e i g h t × w i d t h = m × n height×width=m×n height×width=m×n;
行坐标值从0到 m − 1 m-1 m−1,列坐标值从0到 n − 1 n-1 n−1,如下图(1)所示; - 亚像素精确坐标系:以像素为中心
该坐标系的原点也位于左上角图像像素的中心(0,0),图像的左上角坐标(-0.5,-0.5),如下图(2)所示;
适用算子: - affine_trans_point_2d
HALCON 非标准笛卡尔坐标系
如果我们将图像围绕其原点旋转 (90 度),我们希望这两个图像具有接触边缘但不相互重叠。此外,缩放图像预计不会导致负图像坐标。为此,原点必须设置在图像角落。
- 亚像素精确坐标系:边缘居中
对于这个坐标系,我们在图像的左上角设置原点。因此,左上角像素的中心具有坐标(0.5,0.5);如下图所示;
适用算子: - affine_trans_contour_xld
- affine_trans_image
- affine_trans_image_size
- affine_trans_pixel
- affine_trans_polygon_xld
- affine_trans_region
- projective_trans_contour_xld
- projective_trans_image
- projective_trans_image_size
- projective_trans_pixel
- projective_trans_region
在HALCON中,还有一种情况是,一个操作符期望它的输入在不同的坐标系中。一方面,对象被期望在它通常的坐标,标准坐标中。另一方面,对于变换矩阵HomMat2D,算子期望边中心坐标具有上述变换的优势。
该操作符将对象的坐标从HALCON标准坐标系(原点位于左上角像素的中心)转换为边缘中心坐标系(原点位于左上角像素的左上角)。用HomMat2D进行转换后,将结果转换回标准坐标系。
以下代码进行转换:
hom_mat2d_translate(HomMat2D, 0.5, 0.5, HomMat2DTmp)
hom_mat2d_translate_local(HomMat2DTmp, -0.5, -0.5, HomMat2DAdapted)
验证
代码
dev_close_window ()
dev_open_window (0, 0, 100, 100, 'black', WindowHandle)
gen_circle (Circle, 50, 50, 50)
*region to image
region_to_bin (Circle, BinImage, 255, 0, 512, 512)
area_center (Circle, Area, Row, Column)
***********
*旋转矩阵
vector_angle_to_rigid (Row, Column, 0, Row, Column, rad(90), HomMat2D)
*旋转得到未经坐标变换的区域,图1
affine_trans_region (Circle, RegionAffineTrans, HomMat2D, 'constant')
***********
*坐标变换
hom_mat2d_translate (HomMat2D, 0.5, 0.5, HomMat2DTranslate)
hom_mat2d_translate_local (HomMat2DTranslate, -0.5, -0.5, HomMat2DTranslate1)
*旋转得到经坐标变换的区域,图2
affine_trans_region (Circle, RegionAffineTrans1, HomMat2DTranslate1, 'constant')
图1 未经坐标转换
旋转后区域位置会偏移;
图2 经过坐标转换
旋转后的区域是重合的;