Halcon图像投影映射

Halcon对四边形图案进行投影映射变为矩形的例子:

*图像投影映射例程
read_image(Image, 'datacode/ecc200/ecc200_to_preprocess_001')
*取出二维码区域
binary_threshold(Image, Region, 'max_separability', 'dark', UsedThreshold)
*将区域做凸包
shape_trans(Region, RegionTrans, 'convex')
*取出边界转轮廓
gen_contour_region_xld(RegionTrans, Contours, 'border')
*分割轮廓
segment_contours_xld(Contours, ContoursSplit, 'lines', 5, 4, 2)
*去掉最短的轮廓
contour_point_num_xld(ContoursSplit, Length)
select_contours_xld(ContoursSplit, SelectedContours, 'contour_length', min(Length)+1, max(Length)+1, -0.5, 0.5)
sort_contours_xld(SelectedContours, SortedContours, 'lower_left', 'true', 'row')
*对轮廓拟合直线
LineRowBegin := []
LineColBegin := []
LineRowEnd := []
LineColEnd := []
count_obj(SortedContours, Number)
for Index:=1 to Number by 1
    select_obj(SortedContours, ObjectSelected, Index)
    fit_line_contour_xld(ObjectSelected, 'tukey', -1, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)
    tuple_concat(LineRowBegin, RowBegin, LineRowBegin)
    tuple_concat(LineColBegin, ColBegin, LineColBegin)
    tuple_concat(LineRowEnd, RowEnd, LineRowEnd)
    tuple_concat(LineColEnd, ColEnd, LineColEnd)
endfor
*取4条直线的交点做成四边形
XCoordCorners := []
YCoordCorners := []
for Index:=1 to Number by 1
    if (Index == 1)
        Index1 := Number - 1
        Index2 := Index - 1
    else
        Index1 := Index - 2
        Index2 := Index - 1
    endif
    intersection_lines(LineRowBegin[Index1], LineColBegin[Index1], LineRowEnd[Index1], LineColEnd[Index1], \
                       LineRowBegin[Index2], LineColBegin[Index2], LineRowEnd[Index2], LineColEnd[Index2], \
                       Row, Col, IsOverlapping)
    tuple_concat(XCoordCorners, Row, XCoordCorners)
    tuple_concat(YCoordCorners, Col, YCoordCorners)
endfor
*将四边形的四个顶点按下图排序
*     1------2
*     |      |
*     3------4
XCoordCornersSorted := []
YCoordCornersSorted := []
tuple_sort_index(XCoordCorners, Indices)
TmpX := [XCoordCorners[Indices[0]], XCoordCorners[Indices[1]]]
TmpY := [YCoordCorners[Indices[0]], YCoordCorners[Indices[1]]]
tuple_sort_index(TmpY, Indices1)
tuple_concat(XCoordCornersSorted, [TmpX[Indices1[0]], TmpX[Indices1[1]]], XCoordCornersSorted)
tuple_concat(YCoordCornersSorted, [TmpY[Indices1[0]], TmpY[Indices1[1]]], YCoordCornersSorted)
TmpX := [XCoordCorners[Indices[2]], XCoordCorners[Indices[3]]]
TmpY := [YCoordCorners[Indices[2]], YCoordCorners[Indices[3]]]
tuple_sort_index(TmpY, Indices1)
tuple_concat(XCoordCornersSorted, [TmpX[Indices1[0]], TmpX[Indices1[1]]], XCoordCornersSorted)
tuple_concat(YCoordCornersSorted, [TmpY[Indices1[0]], TmpY[Indices1[1]]], YCoordCornersSorted)
*做投影变换
Px := XCoordCornersSorted
Py := YCoordCornersSorted
Qx := [70,70,270,270]
Qy := [100,300,100,300]
hom_vector_to_proj_hom_mat2d(Px, Py, [1,1,1,1], Qx, Qy, [1,1,1,1], 'normalized_dlt', HomMat2D)
projective_trans_image(Image, TransImage, HomMat2D, 'bilinear', 'false', 'false')

变换前的图案:

变换后的图案:

 

posted @ 2022-08-12 16:56  广阔之海  阅读(507)  评论(0编辑  收藏  举报