affine_trans_imageT_affine_trans_imageAffineTransImageAffineTransImageaffine_trans_image仿射变换图像(算子)

名称

affine_trans_imageT_affine_trans_imageAffineTransImageAffineTransImageaffine_trans_image — 对图像应用任意仿射二维变换。

签名

affine_trans_image(Image : ImageAffineTrans : HomMat2D, Interpolation, AdaptImageSize : )

Herror T_affine_trans_image(const Hobject Image, Hobject* ImageAffineTrans, const Htuple HomMat2D, const Htuple Interpolation, const Htuple AdaptImageSize)

void AffineTransImage(const HObject& Image, HObject* ImageAffineTrans, const HTuple& HomMat2D, const HTuple& Interpolation, const HTuple& AdaptImageSize)

HImage HImage::AffineTransImage(const HHomMat2D& HomMat2D, const HString& Interpolation, const HString& AdaptImageSize) const

HImage HImage::AffineTransImage(const HHomMat2D& HomMat2D, const char* Interpolation, const char* AdaptImageSize) const

HImage HImage::AffineTransImage(const HHomMat2D& HomMat2D, const wchar_t* Interpolation, const wchar_t* AdaptImageSize) const   ( Windows only)

HImage HHomMat2D::AffineTransImage(const HImage& Image, const HString& Interpolation, const HString& AdaptImageSize) const

HImage HHomMat2D::AffineTransImage(const HImage& Image, const char* Interpolation, const char* AdaptImageSize) const

HImage HHomMat2D::AffineTransImage(const HImage& Image, const wchar_t* Interpolation, const wchar_t* AdaptImageSize) const   ( Windows only)

static void HOperatorSet.AffineTransImage(HObject image, out HObject imageAffineTrans, HTuple homMat2D, HTuple interpolation, HTuple adaptImageSize)

HImage HImage.AffineTransImage(HHomMat2D homMat2D, string interpolation, string adaptImageSize)

HImage HHomMat2D.AffineTransImage(HImage image, string interpolation, string adaptImageSize)

def affine_trans_image(image: HObject, hom_mat_2d: Sequence[float], interpolation: str, adapt_image_size: str) -> HObject

描述

affine_trans_imageaffine_trans_imageAffineTransImageAffineTransImageAffineTransImageaffine_trans_imageImageImageImageImageimageimage 中给定的图像应用任意仿射二维变换,即缩放、旋转、平移和倾斜(偏斜),并将变换后的图像返回至 ImageAffineTransImageAffineTransImageAffineTransImageAffineTransimageAffineTransimage_affine_trans。仿射变换由 HomMat2DHomMat2DHomMat2DHomMat2DhomMat2Dhom_mat_2d 参数中的齐次变换矩阵描述,该矩阵可通过 hom_mat2d_identityhom_mat2d_identityHomMat2dIdentityHomMat2dIdentityHomMat2dIdentityhom_mat2d_identityhom_mat2d_scalehom_mat2d_scaleHomMat2dScaleHomMat2dScaleHomMat2dScalehom_mat2d_scalehom_mat2d_rotatehom_mat2d_rotateHomMat2dRotateHomMat2dRotateHomMat2dRotatehom_mat2d_rotate, hom_mat2d_translatehom_mat2d_translateHomMat2dTranslateHomMat2dTranslateHomMat2dTranslatehom_mat2d_translate 等算子创建,也可由 vector_angle_to_rigidvector_angle_to_rigidVectorAngleToRigidVectorAngleToRigidVectorAngleToRigidvector_angle_to_rigid 等算子生成。

齐次变换矩阵的分量解释如下:图像的 坐标对应于变换矩阵定义坐标系中的 x 轴, 坐标对应于 y 轴。这是为了获得图像的右旋坐标系所必需的。特别是,这确保了旋转操作在正确方向上进行。请注意,矩阵中 (x,y) 的顺序与图像坐标中常规的 (行,列) 顺序自然对应。

输入图像的定义域被忽略,即假定其为图像的完整矩形区域。输出图像的定义域是变换后矩形与输出图像矩形的交集区域。

通常,变换后的点会位于像素坐标之间。因此必须采用适当的插值方案。插值也可用于避免缩放图像中的锯齿效应。插值的质量和速度可通过参数 InterpolationInterpolationInterpolationInterpolationinterpolationinterpolation 进行设置:

nearest_neighbor最近邻

最近邻插值:灰度值由最近像素的灰度值确定(可能质量较低,速度极快)。

bilinear双线性

双线性插值。灰度值通过双线性插值从四个最近像素点确定。若仿射变换包含缩放系数 < 1 的缩放操作,则不执行平滑处理,这可能导致严重的锯齿效应(中等质量和运行时间)。

bicubic双三次

双三次插值。灰度值通过双三次插值从 最近像素中确定。若仿射变换包含缩放系数 < 1 的缩放操作,则不执行平滑处理,这可能导致严重的锯齿效应(适用于高品质放大,速度较慢)。

constant常量

双线性插值。灰度值通过双线性插值从四个最近像素确定。若仿射变换包含缩放系数 < 1 的缩放操作,则采用一种均值滤波器来防止混叠效应(中等质量与运行时间)。

weighted加权

双线性插值。灰度值通过双线性插值从四个最近像素点确定。若仿射变换包含缩放系数 < 1 的缩放操作,则采用高斯滤波器防止混叠效应(高质量,速度慢)。

此外,系统参数 'int_zooming'"int_zooming""int_zooming""int_zooming""int_zooming""int_zooming"(参见 set_systemset_systemSetSystemSetSystemSetSystemset_system) )会影响变换的精度。若将 'int_zooming'"int_zooming""int_zooming""int_zooming""int_zooming""int_zooming" 设置为 'true'"true""true""true""true""true",则对 byte、int2 和 uint2 图像的变换将在内部采用定点运算实现,从而显著缩短执行时间。然而,在此情况下转换后的灰度值精度较低。对于字节图像,与更精确的计算(使用 'int_zooming'"int_zooming""int_zooming""int_zooming""int_zooming""int_zooming" = 'false'"false""false""false""false""false")相比,差异通常小于两个灰度级。相应地,对于 int2 和 uint2 图像,灰度值差异小于图像动态灰度值范围的 1/128 倍,即若使用完整的 16 位动态范围,其差异可达 512 个灰度级。使用定点运算时,生成的图像的取值范围也可能不同。此外,若应用较大缩放系数并获得大尺寸输出图像,则图像下边缘和右边缘可能出现未定义的灰度值。该未定义灰度值边界的最大宽度 可估算为 ,其中 S 为单维度缩放因子,I 为输出图像在对应维度上的尺寸。对于实数图像,参数 'int_zooming'"int_zooming""int_zooming""int_zooming""int_zooming""int_zooming" 不会影响精度,因为内部计算始终采用浮点运算。

目标图像的尺寸可通过参数 AdaptImageSizeAdaptImageSizeAdaptImageSizeAdaptImageSizeadaptImageSizeadapt_image_size 进行控制:如果设置为 'true'"true""true""true""true""true",则尺寸将自动调整,确保右侧和底部边缘不会出现裁剪现象。如果设置为 'false'"false""false""false""false""false",目标图像将与输入图像具有相同的尺寸。请注意,无论是否启用 AdaptImageSizeAdaptImageSizeAdaptImageSizeAdaptImageSizeadaptImageSizeadapt_image_size,图像始终会在左侧和顶部边缘进行裁剪,即所有在变换后坐标为负值的图像部分都会被裁剪掉。

注意

输入图像的区域将被忽略。

affine_trans_imageaffine_trans_imageAffineTransImageAffineTransImageAffineTransImageaffine_trans_image 不采用 HALCON 标准坐标系(其原点位于左上角像素的中心),而是使用与 affine_trans_pixelaffine_trans_pixelAffineTransPixelAffineTransPixelAffineTransPixelaffine_trans_pixel 相同的坐标系,即原点位于左上角像素的左上角。因此,应用 affine_trans_image_sizeaffine_trans_image_sizeAffineTransImageSizeAffineTransImageSizeAffineTransImageSizeaffine_trans_image_size 相当于对图像的每个点(输入和输出像素作为齐次向量)施加一组变换(参见 affine_trans_pixelaffine_trans_pixelAffineTransPixelAffineTransPixelAffineTransPixelaffine_trans_pixel)。因此,当基于图像衍生的坐标(例如通过 area_center_grayarea_center_grayAreaCenterGrayAreaCenterGrayAreaCenterGrayarea_center_gray 等算子获取)创建仿射变换时,可能会产生意外结果。例如,若使用该算子计算旋转对称图像的质心,再通过 hom_mat2d_rotatehom_mat2d_rotateHomMat2dRotateHomMat2dRotateHomMat2dRotatehom_mat2d_rotate 围绕该点旋转图像,则生成的图像将不会与原始图像重合。在这种情况下,您可以在将 HomMat2DHomMat2DHomMat2DHomMat2DhomMat2Dhom_mat_2d 用于 affine_trans_imageaffine_trans_imageAffineTransImageAffineTransImageAffineTransImageaffine_trans_image 之前,通过应用以下变换来补偿这种影响:

hom_mat2d_translate(HomMat2D, 0.5, 0.5, HomMat2DTmp)hom_mat2d_translate(HomMat2D, 0.5, 0.5, HomMat2DTmp)HomMat2dTranslate(HomMat2D, 0.5, 0.5, HomMat2DTmp)HomMat2dTranslate(HomMat2D, 0.5, 0.5, HomMat2DTmp)HomMat2dTranslate(HomMat2D, 0.5, 0.5, HomMat2DTmp)hom_mat2d_translate(HomMat2D, 0.5, 0.5, HomMat2DTmp)
hom_mat2d_translate_local(HomMat2DTmp, -0.5, -0.5, HomMat2DAdapted)hom_mat2d_translate_local(HomMat2DTmp, -0.5, -0.5, HomMat2DAdapted)HomMat2dTranslateLocal(HomMat2DTmp, -0.5, -0.5, HomMat2DAdapted)HomMat2dTranslateLocal(HomMat2DTmp, -0.5, -0.5, HomMat2DAdapted)HomMat2dTranslateLocal(HomMat2DTmp, -0.5, -0.5, HomMat2DAdapted)hom_mat2d_translate_local(HomMat2DTmp, -0.5, -0.5, HomMat2DAdapted)
affine_trans_image(Image, ImageAffineTrans, HomMat2DAdapted, 'constant', 'false')affine_trans_image(Image, ImageAffineTrans, HomMat2DAdapted, "constant", "false")AffineTransImage(Image, ImageAffineTrans, HomMat2DAdapted, "constant", "false")AffineTransImage(Image, ImageAffineTrans, HomMat2DAdapted, "constant", "false")AffineTransImage(Image, ImageAffineTrans, HomMat2DAdapted, "constant", "false")affine_trans_image(Image, ImageAffineTrans, HomMat2DAdapted, "constant", "false")

有关 HALCON 中使用的不同二维坐标系的说明,请参阅 变换 / 二维变换 一章的引言。

执行信息

参数

ImageImageImageImageimageimage (输入对象)  (multichannel-)image(-array) objectHImageHObjectHImageHobject (byte / int2 / uint2 / real)

输入图像。

ImageAffineTransImageAffineTransImageAffineTransImageAffineTransimageAffineTransimage_affine_trans (输出对象)  (multichannel-)image(-array) objectHImageHObjectHImageHobject * (byte / int2 / uint2 / real)

转换后的图像。

HomMat2DHomMat2DHomMat2DHomMat2DhomMat2Dhom_mat_2d (输入控制)  hom_mat2d HHomMat2D, HTupleSequence[float]HTupleHtuple (real) (double) (double) (double)

输入变换矩阵。

InterpolationInterpolationInterpolationInterpolationinterpolationinterpolation (输入控制)  string HTuplestrHTupleHtuple (string) (string) (HString) (char*)

插值类型。

默认值: 'constant' "constant" "constant" "constant" "constant" "constant"

值列表: 'bicubic'"bicubic""bicubic""bicubic""bicubic""bicubic", 'bilinear'"bilinear""bilinear""bilinear""bilinear""bilinear", 'constant'"constant""constant""constant""constant""constant", 'nearest_neighbor'"nearest_neighbor""nearest_neighbor""nearest_neighbor""nearest_neighbor""nearest_neighbor", 'weighted'"weighted""weighted""weighted""weighted""weighted"

AdaptImageSizeAdaptImageSizeAdaptImageSizeAdaptImageSizeadaptImageSizeadapt_image_size (输入控制)  string HTuplestrHTupleHtuple (string) (string) (HString) (char*)

调整结果图像的大小。

默认值: 'false' "false" "false" "false" "false" "false"

值列表: 'false'"false""false""false""false""false", 'true'"true""true""true""true""true"

示例(HDevelop)

* Reduction of an image (512 x 512 Pixels) by 50%, rotation
* by 180 degrees and translation to the upper-left corner:

read_image (Image, 'ic0')
hom_mat2d_identity(Matrix1)
hom_mat2d_scale(Matrix1,0.5,0.5,256.0,256.0,Matrix2)
hom_mat2d_rotate(Matrix2,3.14,256.0,256.0,Matrix3)
hom_mat2d_translate(Matrix3,-128.0,-128.0,Matrix4)
affine_trans_image(Image,TransImage,Matrix4,'constant','true')

* Enlarging the part of an image in the interactively
* chosen rectangular window sector:

dev_get_window (WindowHandle)
draw_rectangle2(WindowHandle,L,C,Phi,L1,L2)
hom_mat2d_identity(Matrix1)
get_system('width',Width)
get_system('height',Height)
hom_mat2d_translate(Matrix1,Height/2.0-L,Width/2.0-C,Matrix2)
hom_mat2d_rotate(Matrix2,3.14-Phi,Height/2.0,Width/2.0,Matrix3)
hom_mat2d_scale(Matrix3,Height/(2.0*L2),Width/(2.0*L1), \
                Height/2.0,Width/2.0,Matrix4)
affine_trans_image(Image,TransImage,Matrix4,'constant','true')

结果

如果矩阵 HomMat2DHomMat2DHomMat2DHomMat2DhomMat2Dhom_mat_2d 表示仿射变换(即非投影变换),affine_trans_imageaffine_trans_imageAffineTransImageAffineTransImageAffineTransImageaffine_trans_image 返回 2 ( H_MSG_TRUE )。如果输入为空时,可设置行为通过算子 set_system(::'no_object_result',<Result>:)set_system("no_object_result",<Result>)SetSystem("no_object_result",<Result>)SetSystem("no_object_result",<Result>)SetSystem("no_object_result",<Result>)set_system("no_object_result",<Result>)。如有必要,则抛出异常。

可能的前趋

hom_mat2d_identityhom_mat2d_identityHomMat2dIdentityHomMat2dIdentityHomMat2dIdentityhom_mat2d_identity, hom_mat2d_translatehom_mat2d_translateHomMat2dTranslateHomMat2dTranslateHomMat2dTranslatehom_mat2d_translate, hom_mat2d_rotatehom_mat2d_rotateHomMat2dRotateHomMat2dRotateHomMat2dRotatehom_mat2d_rotate, hom_mat2d_scalehom_mat2d_scaleHomMat2dScaleHomMat2dScaleHomMat2dScalehom_mat2d_scale, hom_mat2d_reflecthom_mat2d_reflectHomMat2dReflectHomMat2dReflectHomMat2dReflecthom_mat2d_reflect

替代

affine_trans_image_sizeaffine_trans_image_sizeAffineTransImageSizeAffineTransImageSizeAffineTransImageSizeaffine_trans_image_size, zoom_image_sizezoom_image_sizeZoomImageSizeZoomImageSizeZoomImageSizezoom_image_size, zoom_image_factorzoom_image_factorZoomImageFactorZoomImageFactorZoomImageFactorzoom_image_factor, mirror_imagemirror_imageMirrorImageMirrorImageMirrorImagemirror_image, rotate_imagerotate_imageRotateImageRotateImageRotateImagerotate_image, affine_trans_regionaffine_trans_regionAffineTransRegionAffineTransRegionAffineTransRegionaffine_trans_region

另见

set_part_styleset_part_styleSetPartStyleSetPartStyleSetPartStyleset_part_style

模块

基础