QQ登录

只需一步,快速开始

halcon proj_match_points_distortion_ransac函数介绍

[ 复制链接 ]
一.proj_match_points_distortion_ransac(Image1, Image2 : : Rows1, Cols1, Rows2, Cols2, GrayMatchMethod, MaskSize, RowMove, ColMove, RowTolerance, ColTolerance, Rotation,
MatchThreshold, EstimationMethod, DistanceThreshold, RandSeed : HomMat2D, Kappa, Error, Points1, Points2)

在输入图像Image1 Image2中给出一组特征点的坐标(Rows1 Cols1)和(Rows2 Cols2),
其必须相同的大小。
proj_match_points_distortion_ransac自动确定对应点,齐次射影变换矩阵HomMat2D。
径向畸变系数 Kappa(k),最佳满足以下方程:
2019-05-28_112343.jpg
r1,c1,r2,c2表示用除法模型对输入图像点进行不失真处理得到的图像点,参考 calibrate_cameras。
2019-05-28_112343.jpg
2019-05-28_112343.jpg
表示相对于图像中心指定的畸变图像点,w和h表示输入图像的宽度和高度。
因此,proj_match_points_transformtion_ransac假设相机的主点,即,径向畸变的中心位于图像的中心。


返回的Kappa可用于构造可用于纠正图像或点的相机参数
(参见change_radial_transformtion_cam_par、change_radial_transformtion_image和change_radial_distortion_points):
2019-05-28_112343.jpg


匹配过程基于特征点,可以用points_foerstner或points_harris等点操作符提取特征点。
匹配本身分两步进行:
首先,确定第一幅图像和第二幅图像中输入点周围掩模窗口的灰度值相关性,
利用两幅图像中窗口的相似性生成掩模窗口之间的初始匹配。
然后,利用RANSAC算法求出在上述约束条件下使对应数最大化的投影变换矩阵和径向畸变系数。


用于匹配的掩码窗口的大小为MaskSize x MaskSize。
可以选择三个相关指标。
如果GrayMatchMethod的值为‘ssd’,则使用灰度值差的平方和,
‘sad’表示绝对差的和,‘ncc’表示归一化互相关。
详情请参考binocular_differences。
在所有可能的点对上最小化('ssd'、'sad')或最大化('ncc')。
因此,只有当度量值低于MatchThreshold值('ssd'、'sad')或高于该值('ncc')时,才接受找到的匹配。


为了提高算法的性能,可以通过指定匹配候选项的大小和偏移量将搜索区域限制为矩形。
只考虑窗口内2*RowTolerance*2*ColTolerance的点。
第二幅图像中搜索窗口中心相对于第一幅图像中当前点位置的偏移量由RowMove和ColMove给出。


如果转换包含旋转,
即,如果第一幅图像相对于第二幅图像进行旋转,
则参数旋转可以包含旋转角度的估计值或以弧度为单位的角度区间。
好的猜测可以提高灰度值匹配的质量。
如果实际旋转与指定的估计值相差太大,匹配通常会失败。
在这种情况下,应该指定一个角度间隔,并且旋转是一个包含两个元素的元组。
给定的区间越大,操作符的速度就越慢,因为RANSAC算法在区间内运行所有(自动确定的)角度增量。


初始匹配完成后,采用随机搜索算法(RANSAC)确定投影变换矩阵HomMat2D和径向畸变系数Kappa。
它试图找到与最大对应数一致的参数。
对于要接受的点,其到对应转换点的像素距离不能超过阈值DistanceThreshold。


参数 EstimationMethod 确定了计算投影变换矩阵的算法。
如果将EstimationMethod 设置为“linear”,则使用线性算法。
该算法速度非常快,对于点坐标的小到中等噪声和大多数失真(小失真除外)都能返回准确的结果。
对于EstimationMethod = 'gold_standard',使用了一种数学上最优但较慢的优化方法,使几何重投影误差最小化。
通常,最好使用EstimationMethod = 'gold_standard'。


Error表示估计过程的总体质量,是点与其对应的变换点之间以像素为单位的平均对称欧氏距离。


与上述约束一致的点对被认为是对应的点。
Points1包含第一张图像中匹配的输入点的索引,
Points2包含第二张图像中对应点的索引。


参数RandSeed可用于控制RANSAC算法的随机性,从而获得可重复的结果。
如果将RandSeed设置为正数,则操作符将在每次调用时返回相同的结果,并使用相同的参数,
因为内部使用的随机数生成器是用RandSeed初始化的。
如果RandSeed = 0,则使用当前时间初始化随机数生成器。
在这种情况下,结果可能无法重现。


halcon自带例程:
dev_update_off ()
* Read and display the images
read_image (Image1, 'mosaic/building_01')
read_image (Image2, 'mosaic/building_02')
get_image_size (Image1, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'white', WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_display (Image1)
disp_message (WindowHandle, 'Image 1 to be matched', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
dev_display (Image2)
disp_message (WindowHandle, 'Image 2 to be matched', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*
* Extract points to be matched from the images
points_foerstner (Image1, 1, 2, 3, 50, 0.1, 'gauss', 'true', Rows1, Columns1, CoRRJunctions, CoRCJunctions, CoCCJunctions, RowArea, ColumnArea, CoRRArea, CoRCArea, CoCCArea)
points_foerstner (Image2, 1, 2, 3, 50, 0.1, 'gauss', 'true', Rows2, Columns2, CoRRJunctions, CoRCJunctions, CoCCJunctions, RowArea, ColumnArea, CoRRArea, CoRCArea, CoCCArea)
*
* We will first perform a normal projective matching that does not take
* the radial distortions into account to show the errors that are caused
* by neglecting the radial distortions.
proj_match_points_ransac (Image1, Image2, Rows1, Columns1, Rows2, Columns2, 'ncc', 10, 0, 0, Height, Width, 0, 0.5, 'gold_standard', 2, 42, HomMat2DUnrectified, Points1Unrectified, Points2Unrectified)
* Construct a projective mosaic from the two unrectified images.
concat_obj (Image1, Image2, Images)
gen_projective_mosaic (Images, MosaicImageUnrectified, 1, 1, 2, HomMat2DUnrectified, 'default', 'false', MosaicMatrices2DUnrectified)
*
* Display unrectified results
get_image_size (MosaicImageUnrectified, Width, Height)
dev_set_window_extents (-1, -1, Width, Height)
dev_clear_window ()
dev_display (MosaicImageUnrectified)
* Display seam line
projective_trans_pixel (MosaicMatrices2DUnrectified[9:17], [0,493], [0,0], RowTrans, ColumnTrans)
gen_contour_polygon_xld (Contour, RowTrans, ColumnTrans)
set_line_style (WindowHandle, [1,5])
dev_set_line_width (1)
dev_set_color ('yellow')
dev_display (Contour)
set_line_style (WindowHandle, [])
dev_set_draw ('margin')
dev_set_color ('red')
dev_set_line_width (3)
gen_circle (Circle, [82,402], [228,223], [15,15])
dev_display (Circle)
Message := 'The mosaic image does not fit'
Message[1] := 'perfectly, if radial distortions'
Message[2] := 'are not taken into account.'
disp_message (WindowHandle, Message, 'image', 200, 300, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*
* Now, we will perform a projective matching that takes the radial
* distortions into account.
get_image_size (Image1, Width, Height)
proj_match_points_distortion_ransac (Image1, Image2, Rows1, Columns1, Rows2, Columns2, 'ncc', 10, 0, 0, Height, Width, 0, 0.5, 'gold_standard', 1, 42, HomMat2D, Kappa, Error, Points1, Points2)
* Construct camera parameters for the purpose of rectifying the images,
* i.e., to remove the radial distortions.
gen_cam_par_area_scan_telecentric_division (1.0, Kappa, 1.0, 1.0, 0.5 * (Width - 1), 0.5 * (Height - 1), Width, Height, CamParDist)
* Remove the radial distortions from the images.
change_radial_distortion_cam_par ('fixed', CamParDist, 0, CamPar)
change_radial_distortion_image (Image1, Image1, Image1Rect, CamParDist, CamPar)
change_radial_distortion_image (Image2, Image2, Image2Rect, CamParDist, CamPar)
* Construct a mosaic from the two rectified images. Note that the images
* fit together perfectly.
concat_obj (Image1Rect, Image2Rect, ImagesRect)
gen_projective_mosaic (ImagesRect, MosaicImage, 1, 1, 2, HomMat2D, 'default', 'false', MosaicMatrices2D)
*
* Display rectified results
get_image_size (MosaicImage, Width, Height)
dev_set_window_extents (-1, -1, Width, Height)
dev_clear_window ()
dev_display (MosaicImage)
* Display seam line
projective_trans_pixel (MosaicMatrices2D[9:17], [0,493], [0,0], RowTrans, ColumnTrans)
gen_contour_polygon_xld (Contour2, RowTrans, ColumnTrans)
set_line_style (WindowHandle, [1,5])
dev_set_line_width (1)
dev_set_color ('yellow')
dev_display (Contour2)
set_line_style (WindowHandle, [])
dev_set_draw ('margin')
dev_set_color ('green')
dev_set_line_width (3)
gen_circle (Circle, [124,496], [244,239], [15,15])
dev_display (Circle)
Message := 'The mosaic image fits perfectly,'
Message[1] := 'if radial distortions are taken'
Message[2] := 'into account.'
disp_message (WindowHandle, Message, 'image', 200, 300, 'black', 'true')

二.proj_match_points_distortion_ransac_guided(Image1, Image2 : : Rows1, Cols1,
Rows2, Cols2, GrayMatchMethod, MaskSize, HomMat2DGuide, KappaGuide, DistanceTolerance,
MatchThreshold, EstimationMethod, DistanceThreshold, RandSeed : HomMat2D, Kappa, Error, Points1, Points2)
和上面函数类似,此函数根据投影变换矩阵与径向畸变系数的已知近似,
求出点与点之间的对应关系,
计算出两幅图像之间的投影变换矩阵和径向畸变系数。

下面是halcon自带例程:
* This example shows how to use proj_match_points_distortion_ransac_guided
* to match two images in a mosaicking application.
*
* Initialization
dev_update_off ()
* Read and display the images
read_image (Image1, 'mosaic/building_01')
read_image (Image2, 'mosaic/building_02')
get_image_size (Image1, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_display (Image1)
disp_message (WindowHandle, 'Image 1 to be matched', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
dev_display (Image2)
disp_message (WindowHandle, 'Image 2 to be matched', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*
* Set the zoom factor by which the images are zoomed down to obtain
* initial estimates of the projective transformation and of the radial
* distortion coefficient.
Factor := 0.5
* Zoom down the images.
zoom_image_factor (Image1, Image1Zoomed, Factor, Factor, 'constant')
zoom_image_factor (Image2, Image2Zoomed, Factor, Factor, 'constant')
* Extract points to be matched from the low-resolution images.
points_foerstner (Image1Zoomed, 1, 2, 3, 50, 0.1, 'gauss', 'true', Rows1, Cols1, CoRRJunctions, CoRCJunctions, CoCCJunctions, RowArea, ColumnArea, CoRRArea, CoRCArea, CoCCArea)
points_foerstner (Image2Zoomed, 1, 2, 3, 50, 0.1, 'gauss', 'true', Rows2, Cols2, CoRRJunctions, CoRCJunctions, CoCCJunctions, RowArea, ColumnArea, CoRRArea, CoRCArea, CoCCArea)
* Read out the image size, which is only used here to make the search
* range for point matches as large as possible.
get_image_size (Image1Zoomed, Width, Height)
* Perform the initial point matching on the low-resolution images.
proj_match_points_distortion_ransac (Image1Zoomed, Image2Zoomed, Rows1, Cols1, Rows2, Cols2, 'ncc', 10, 0, 0, Height, Width, 0, 0.5, 'gold_standard', 1, 42, HomMat2D, Kappa, Error, Points1, Points2)
* Adapt the projective transformation to the original image resolution.
hom_mat2d_scale_local (HomMat2D, Factor, Factor, HomMat2DGuide)
hom_mat2d_scale (HomMat2DGuide, 1.0 / Factor, 1.0 / Factor, 0, 0, HomMat2DGuide)
* Adapt the radial distortion coefficient to the original image resolution.
KappaGuide := Kappa * Factor * Factor
* Extract points to be matched from the original images.
points_foerstner (Image1, 1, 2, 3, 50, 0.1, 'gauss', 'true', Rows1, Cols1, CoRRJunctions, CoRCJunctions, CoCCJunctions, RowArea, ColumnArea, CoRRArea, CoRCArea, CoCCArea)
points_foerstner (Image2, 1, 2, 3, 50, 0.1, 'gauss', 'true', Rows2, Cols2, CoRRJunctions, CoRCJunctions, CoCCJunctions, RowArea, ColumnArea, CoRRArea, CoRCArea, CoCCArea)
* Match the points in the original images using the estimates of the
* projective transformation and of the radial distortion coefficient as
* a guide.
proj_match_points_distortion_ransac_guided (Image1, Image2, Rows1, Cols1, Rows2, Cols2, 'ncc', 10, HomMat2DGuide, KappaGuide, 5, 0.5, 'gold_standard', 1, 42, HomMat2D, Kappa, Error, Points1, Points2)
* Construct camera parameters for the purpose of rectifying the images,
* i.e., to remove the radial distortions.
get_image_size (Image1, Width, Height)
gen_cam_par_area_scan_telecentric_division (1.0, Kappa, 1.0, 1.0, 0.5 * (Width - 1), 0.5 * (Height - 1), Width, Height, CamParDist)
* Remove the radial distortions from the images.
change_radial_distortion_cam_par ('fixed', CamParDist, 0, CamPar)
change_radial_distortion_image (Image1, Image1, Image1Rect, CamParDist, CamPar)
change_radial_distortion_image (Image2, Image2, Image2Rect, CamParDist, CamPar)
* Construct a mosaic from the two rectified images.  Note that the images
* fit together perfectly.
concat_obj (Image1Rect, Image2Rect, ImagesRect)
gen_projective_mosaic (ImagesRect, MosaicImage, 1, 1, 2, HomMat2D, 'default', 'false', MosaicMatrices2D)
get_image_size (MosaicImage, Width, Height)
dev_set_window_extents (-1, -1, Width, Height)
dev_clear_window ()
dev_display (MosaicImage)
disp_message (WindowHandle, 'Mosaic image', 'window', 12, 12, 'black', 'true')




  

halcon从自学到接项目视频教程,另外再赠送全网最全资源  

  

欢迎围观我录制的一套halcon自学视频教程(进入)



回复

使用道具 举报

快速回复 返回列表 客服中心 搜索