Halcon旋转图像任意角度 不被裁剪

Halcon旋转图像任意角度,保证图像完整性边角不被裁剪,原图旋转后将扩大。(计算旋转后图像尺寸的时候有点问题,待改善……)

dev_update_off ()
optimize_aop ('affine_trans_image', 'byte', 'no_file', ['file_mode','model','parameters'], ['nil','threshold','false'])
set_system ('parallelize_operators', 'true')//只需开一次(默认也是true)

read_image (image_ori, 'C:/Users/Administrator/Desktop/2020-01-02_10_31_56_051.bmp_.png')
get_image_size(image_ori, Width, Height)
get_domain (image_ori, Domain)
get_region_points(Domain, Rows, Columns)

angle:=25

vector_angle_to_rigid (Height/2, Width/2, 0, Height/2, Width/2, rad(angle), HomMat2D)
*****旋转变换前先将'clip_region'置'false',不然超出图像大小的部分区域被裁剪了
set_system ('clip_region', 'false')
affine_trans_region (Domain, RegionAffineTrans, HomMat2D, 'nearest_neighbor')
set_system ('clip_region', 'true')

smallest_rectangle1 (RegionAffineTrans, Row1, Column1, Row2, Column2)
**HeightNew:=Row2-Row1
**WidthNew:=Column2-Column1
HeightNew:=Row2+abs(Row1)+1
WidthNew:=Column2+abs(Column1)+1

count_seconds (Seconds)
count_channels (image_ori, Channels)
***************单通道图像的旋转****************
if(Channels=1)
get_grayval(image_ori,Rows,Columns,Grayval)

gen_image_const(Image1, 'byte', WidthNew, HeightNew)
***关键所在,将旋转之前的图像像素值填到旋转之后新图像的居中位置
Rows:=Rows+(HeightNew/2-Height/2)
Columns:=Columns+(WidthNew/2-Width/2)
set_grayval(Image1,Rows,Columns,Grayval)
vector_angle_to_rigid(HeightNew/2, WidthNew/2, 0, HeightNew/2, WidthNew/2, rad(angle), HomMat2D1)
affine_trans_image(Image1, image_dst, HomMat2D1, 'constant', 'false')
**************3通道彩色图像的旋转****************
elseif(Channels=3)
decompose3(image_ori, ImageR, ImageG, ImageB)
get_grayval(ImageR,Rows,Columns,GrayvalR)
get_grayval(ImageG,Rows,Columns,GrayvalG)
get_grayval(ImageB,Rows,Columns,GrayvalB)

gen_image_const(Image1, 'byte', WidthNew, HeightNew)
gen_image_const(Image2, 'byte', WidthNew, HeightNew)
gen_image_const(Image3, 'byte', WidthNew, HeightNew)
Rows:=Rows+(HeightNew/2-Height/2)
Columns:=Columns+(WidthNew/2-Width/2)
set_grayval(Image1,Rows,Columns,GrayvalR)
set_grayval(Image2,Rows,Columns,GrayvalG)
set_grayval(Image3,Rows,Columns,GrayvalB)
compose3(Image1, Image2, Image3, MultiChannelImage)

vector_angle_to_rigid(HeightNew/2, WidthNew/2, 0, HeightNew/2, WidthNew/2, rad(angle), HomMat2D1)
affine_trans_image(MultiChannelImage, image_dst, HomMat2D1, 'constant', 'false')
endif

count_seconds (Seconds1)
Time:=(Seconds1-Seconds)*1000
dev_clear_window ()
dev_display (image_dst)

dev_update_on ()

————————————————
版权声明:本文为CSDN博主「c1learning」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/c1learning/article/details/105268843

posted @ 2021-04-25 16:22  Malcon机器视觉底层库  阅读(1524)  评论(0编辑  收藏  举报