|
放射变换:旋转中心和旋转角计算:采用光流法计算- * 初始化application initializations
- ImageFilenames := 'pcb_rotation/pcb_rotated_'
- BorderSize := 5
- GridWidth := 10
- NumImages := 10
- Threshold := 50
- Step := 1
- CenterRows := []
- CenterColumns := []
- *
- * display initializations
- dev_update_off ()
- dev_close_window ()
- * 初始参考位置图像
- read_image (Image1, ImageFilenames + '01')
- get_image_size (Image1, Width, Height)
- dev_open_window_fit_image (Image1, 0, 0, Width, Height, WindowHandle)
- dev_set_line_width (2)
- set_display_font (WindowHandle, 12, 'mono', 'true', 'false')
- dev_display (Image1)
- * 生成一个有效区域
- gen_rectangle1 (Rectangle, BorderSize, BorderSize, Height - BorderSize - 1, Width - BorderSize - 1)
- *
- * 主循环
- for Index := 1 + Step to NumImages by Step
- * 读入下一张图像
- read_image (Image2, ImageFilenames + '0'+ Index)
- *
- * 计算光流法场向量calculate optical flow field
- optical_flow_mg (Image1, Image2, VectorField, 'fdrig', 0.8, 1, 20, 5, 'default_parameters', 'fast_accurate')
- *
- * 提取有效点extract only significant points
- sobel_amp (Image1, EdgeAmplitude1, 'sum_abs', 3)
- threshold (EdgeAmplitude1, Region1, Threshold, 255)
- sobel_amp (Image2, EdgeAmplitude2, 'sum_abs', 3)
- threshold (EdgeAmplitude2, Region2, Threshold, 255)
- union2 (Region1, Region2, RegionUnion)
- dilation_circle (RegionUnion, RegionDilation, 2.5)
- intersection (RegionDilation, Rectangle, RegionIntersection)
- * 产生一个网格图
- gen_grid_region (RegionGrid, GridWidth, GridWidth, 'points', Width, Height)
- intersection (RegionIntersection, RegionGrid, RegionIntersection)
- * 得到有效的场向量
- reduce_domain (VectorField, RegionIntersection, VectorFieldReduced)
- *
- * 计算转换矩阵transformation matrix
- get_region_points (RegionIntersection, Rows, Columns)
- vector_field_to_real (VectorFieldReduced, RowImage, ColImage)
- get_grayval (RowImage, Rows, Columns, RowDisplacement)
- get_grayval (ColImage, Rows, Columns, ColumnDisplacement)
- * 计算仿射变换矩阵【Rows, Columns】-->【Rows+RowDisplacement, Columns+ColumnDisplacement】
- vector_to_rigid (Rows, Columns, Rows + RowDisplacement, Columns + ColumnDisplacement, HomMat2D)
- *
- * 计算旋转中心calculate rotation center
- * 这里假设:图像序列没有缩放,没有倾斜拍照,即图像序列是同一个状态下拍照的with NO scaling or slanting.
- * HomMat2D is assumed to look like:
- * [ cos(Alpha), -sin(Alpha), -x*cos(Alpha)+y*sin(Alpha)+x,
- * sin(Alpha), cos(Alpha), -x*sin(Alpha)-y*cos(Alpha)+y ]
- * for robustness, Alpha is estimated with vector_field_to_hom_mat2d,
- * and after that the rotation center can be easily calculated
- hom_mat2d_to_affine_par (HomMat2D, Sx, Sy, Alpha, Theta, Tx, Ty)
- CosAlpha := cos(Alpha)
- SinAlpha := sin(Alpha)
- Temp := (1 - CosAlpha) / SinAlpha
- RotationColumn := (HomMat2D[2] + Temp * HomMat2D[5]) / (Temp * (1 - CosAlpha) + SinAlpha)
- RotationRow := ((1 - CosAlpha) * RotationColumn - HomMat2D[5]) / SinAlpha
- * 旋转中心
- CenterRows := [CenterRows,RotationRow]
- CenterColumns := [CenterColumns,RotationColumn]
- *
- * display results
- dev_display (Image2)
- dev_set_color ('green')
- dev_display (VectorFieldReduced)
- * 显示旋转中心点坐标信息
- disp_message (WindowHandle, 'Rotation center row: ' + RotationRow + '\nRotation center column: ' + RotationColumn, 'window', -1, -1, 'black', 'true')
- gen_cross_contour_xld (Cross, RotationRow, RotationColumn, 25, Alpha)
- dev_set_color ('yellow')
- dev_display (Cross)
- * 将Image2作为初始位置图像,并赋值给Image1
- copy_obj (Image2, Image1, 1, 1)
- endfor
- *
- * 结果显示
- dev_display (Image2)
- dev_set_color ('blue')
- dev_display (VectorFieldReduced)
- gen_cross_contour_xld (Cross, CenterRows, CenterColumns, 25, Alpha)
- gen_circle (Center, mean(CenterRows), mean(CenterColumns), 3 * sqrt(pow(deviation(CenterRows),2) + pow(deviation(CenterColumns),2)))
- dev_set_color ('yellow')
- dev_display (Cross)
- dev_set_color ('red')
- dev_display (Center)
复制代码 参考:
【1】仿射变换:水平矫正
【2】仿射变换hom_mat2d_identity
【3】基于光流法的运动目标检测
【4】光流法应用于门禁系统——检测行人
|
|