在Halcon中有兩個用於圖像旋轉的函數:
1,rotate_image
*Image和ImageRotate分別是輸入和輸出圖像 *Phi是輸入的旋轉度數 *interpolation是內插方式,默認為'constant' rotate_image(Image : ImageRotate : Phi, Interpolation : )
2,affine_trans_image
*開辟一個旋轉矩陣 (1)hom_mat2d_identity (HomMat2D) *為旋轉矩陣賦值 *HomMat2D是輸入的旋轉矩陣 *Phi是輸入的旋轉角度 *Px和Py分別是輸入的旋轉中心x坐標值和y坐標值 *HomMat2DRotate是輸出的旋轉矩陣 (2)hom_mat2d_rotate( : : HomMat2D, Phi, Px, Py : HomMat2DRotate) *實施旋轉 *Image : ImageAffinTrans分別為輸入輸出圖像 *HomMat2D是賦值后的旋轉矩陣。注意:是賦值后的 *Interpolation是內插方式,默認為'constant' *AdaptImageSize是bool變量,判斷輸出圖像的大小是否發生變化 (3)affine_trans_image(Image : ImageAffinTrans : HomMat2D, Interpolation, AdaptImageSize : )
但是,在使用過程中,遇到一個問題。在圖片進行旋轉之后,圖片的四個角都不能很好的保留。我想做的是將圖片旋轉后,將圖片不會缺損,也不會縮小,也就是說原圖片旋轉后將擴大。然后,我找到一種折中的辦法,但是速度很慢,也許達不到要求。還是分享記錄一下吧。
optimize_aop ('rotate_image', 'byte', 'no_file', ['file_mode','model','parameters'], ['nil','threshold','false']) set_system ('parallelize_operators', 'true')//只需開一次 get_image_size(image_ori, Width, Height) gen_rectangle1(Rectangle,0,0,Height-1,Width-1) get_region_points(Rectangle, Rows, Columns) ***************單通道圖像的旋轉**************** if(c=1) get_grayval(image_ori,Rows,Columns,Grayval) HeightL:=3084 WidthL:=3184 gen_image_const(Image1, 'byte', WidthL, HeightL) Rows:=Rows+(HeightL/2-Height/2) Columns:=Columns+(WidthL/2-Width/2) set_grayval(Image1,Rows,Columns,Grayval) Phi1:=rad(angle) vector_angle_to_rigid(HeightL/2, WidthL/2, 0, HeightL/2, WidthL/2, -Phi1, HomMat2D1) affine_trans_image(Image1, image_dst, HomMat2D1, 'constant', 'false')
**************3通道彩色圖像的旋轉**************** elseif(c=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) HeightL:=3084 WidthL:=3184 gen_image_const(Image1, 'byte', WidthL, HeightL) gen_image_const(Image2, 'byte', WidthL, HeightL) gen_image_const(Image3, 'byte', WidthL, HeightL) Rows:=Rows+(HeightL/2-Height/2) Columns:=Columns+(WidthL/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) Phi1:=rad(-35) vector_angle_to_rigid(HeightL/2, WidthL/2, 0, HeightL/2, WidthL/2, -Phi1, HomMat2D1) affine_trans_image(MultiChannelImage, image_dst, HomMat2D1, 'constant', 'false') endif