请选择 进入手机版 | 继续访问电脑版

Hello Mat

 找回密码
 立即注册
查看: 8177|回复: 7

放射变换:旋转中心和旋转角计算

[复制链接]

1278

主题

1504

帖子

90

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22549
发表于 2020-2-22 16:12:34 | 显示全部楼层 |阅读模式
放射变换:旋转中心和旋转角计算:采用光流法计算
  1. * 初始化application initializations
  2. ImageFilenames := 'pcb_rotation/pcb_rotated_'
  3. BorderSize := 5
  4. GridWidth := 10
  5. NumImages := 10
  6. Threshold := 50
  7. Step := 1
  8. CenterRows := []
  9. CenterColumns := []
  10. *
  11. * display initializations
  12. dev_update_off ()
  13. dev_close_window ()
  14. * 初始参考位置图像
  15. read_image (Image1, ImageFilenames + '01')
  16. get_image_size (Image1, Width, Height)
  17. dev_open_window_fit_image (Image1, 0, 0, Width, Height, WindowHandle)
  18. dev_set_line_width (2)
  19. set_display_font (WindowHandle, 12, 'mono', 'true', 'false')
  20. dev_display (Image1)
  21. * 生成一个有效区域
  22. gen_rectangle1 (Rectangle, BorderSize, BorderSize, Height - BorderSize - 1, Width - BorderSize - 1)
  23. *
  24. * 主循环
  25. for Index := 1 + Step to NumImages by Step
  26.     * 读入下一张图像
  27.     read_image (Image2, ImageFilenames + '0'+ Index)
  28.     *
  29.     * 计算光流法场向量calculate optical flow field
  30.     optical_flow_mg (Image1, Image2, VectorField, 'fdrig', 0.8, 1, 20, 5, 'default_parameters', 'fast_accurate')
  31.     *
  32.     * 提取有效点extract only significant points
  33.     sobel_amp (Image1, EdgeAmplitude1, 'sum_abs', 3)
  34.     threshold (EdgeAmplitude1, Region1, Threshold, 255)
  35.     sobel_amp (Image2, EdgeAmplitude2, 'sum_abs', 3)
  36.     threshold (EdgeAmplitude2, Region2, Threshold, 255)
  37.     union2 (Region1, Region2, RegionUnion)
  38.     dilation_circle (RegionUnion, RegionDilation, 2.5)
  39.     intersection (RegionDilation, Rectangle, RegionIntersection)
  40.     * 产生一个网格图
  41.     gen_grid_region (RegionGrid, GridWidth, GridWidth, 'points', Width, Height)
  42.     intersection (RegionIntersection, RegionGrid, RegionIntersection)
  43.     * 得到有效的场向量
  44.     reduce_domain (VectorField, RegionIntersection, VectorFieldReduced)
  45.     *
  46.     * 计算转换矩阵transformation matrix
  47.     get_region_points (RegionIntersection, Rows, Columns)
  48.     vector_field_to_real (VectorFieldReduced, RowImage, ColImage)
  49.     get_grayval (RowImage, Rows, Columns, RowDisplacement)
  50.     get_grayval (ColImage, Rows, Columns, ColumnDisplacement)
  51.     * 计算仿射变换矩阵【Rows, Columns】-->【Rows+RowDisplacement, Columns+ColumnDisplacement】
  52.     vector_to_rigid (Rows, Columns, Rows + RowDisplacement, Columns + ColumnDisplacement, HomMat2D)
  53.     *
  54.     * 计算旋转中心calculate rotation center
  55.     * 这里假设:图像序列没有缩放,没有倾斜拍照,即图像序列是同一个状态下拍照的with NO scaling or slanting.
  56.     * HomMat2D is assumed to look like:
  57.     * [ cos(Alpha), -sin(Alpha), -x*cos(Alpha)+y*sin(Alpha)+x,
  58.     *   sin(Alpha), cos(Alpha), -x*sin(Alpha)-y*cos(Alpha)+y ]
  59.     * for robustness, Alpha is estimated with vector_field_to_hom_mat2d,
  60.     * and after that the rotation center can be easily calculated
  61.     hom_mat2d_to_affine_par (HomMat2D, Sx, Sy, Alpha, Theta, Tx, Ty)
  62.     CosAlpha := cos(Alpha)
  63.     SinAlpha := sin(Alpha)
  64.     Temp := (1 - CosAlpha) / SinAlpha
  65.     RotationColumn := (HomMat2D[2] + Temp * HomMat2D[5]) / (Temp * (1 - CosAlpha) + SinAlpha)
  66.     RotationRow := ((1 - CosAlpha) * RotationColumn - HomMat2D[5]) / SinAlpha
  67.     * 旋转中心
  68.     CenterRows := [CenterRows,RotationRow]
  69.     CenterColumns := [CenterColumns,RotationColumn]
  70.     *
  71.     * display results
  72.     dev_display (Image2)
  73.     dev_set_color ('green')
  74.     dev_display (VectorFieldReduced)
  75.     * 显示旋转中心点坐标信息
  76.     disp_message (WindowHandle, 'Rotation center row:  ' + RotationRow + '\nRotation center column: ' + RotationColumn, 'window', -1, -1, 'black', 'true')
  77.     gen_cross_contour_xld (Cross, RotationRow, RotationColumn, 25, Alpha)
  78.     dev_set_color ('yellow')
  79.     dev_display (Cross)
  80.     * 将Image2作为初始位置图像,并赋值给Image1
  81.     copy_obj (Image2, Image1, 1, 1)
  82. endfor
  83. *
  84. * 结果显示
  85. dev_display (Image2)
  86. dev_set_color ('blue')
  87. dev_display (VectorFieldReduced)
  88. gen_cross_contour_xld (Cross, CenterRows, CenterColumns, 25, Alpha)
  89. gen_circle (Center, mean(CenterRows), mean(CenterColumns), 3 * sqrt(pow(deviation(CenterRows),2) + pow(deviation(CenterColumns),2)))
  90. dev_set_color ('yellow')
  91. dev_display (Cross)
  92. dev_set_color ('red')
  93. dev_display (Center)
复制代码
参考:
【1】仿射变换:水平矫正
【2】仿射变换hom_mat2d_identity
【3】基于光流法的运动目标检测
【4】光流法应用于门禁系统——检测行人

算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-3-29 14:00 , Processed in 0.215927 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表