我刚刚把你的图片都下载看了一下,是卡尺过度阈值的问题,所以我建议要么把Gray:=40过度阈值改成40,或者在这之前把RegionB使用region_to_bin转化成Bin图像用于测量就可以解决这个问题,代码如下:- list_files ('./Image', ['files','follow_links','recursive'], ImageFiles)
- tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
- for Index := 0 to |ImageFiles| - 1 by 1
- read_image (Image, ImageFiles[Index])
- *//提取目标区域
- threshold (Image, RegionB, 0, 100)
- connection (RegionB, RegionB)
- select_shape_std (RegionB, RegionB, 'max_area', 70)
-
- *//提取目标上下边缘
- smallest_rectangle2 (RegionB, RowB, ColB, PhiB, L1B, L2B)
- gen_rectangle2 (RectOut, RowB, ColB, PhiB, L1B+100, L2B-100)
- difference (RegionB, RectOut, RegionB)
- connection (RegionB, RegionB)
- sort_region (RegionB, RegionB, 'first_point', 'true', 'row')
- select_obj (RegionB, RegionBU, 1)
- select_obj (RegionB, RegionBD, 2)
-
- get_image_size (Image, Width, Height)
- threshold (Image, RegionBA, 0, 100)
- region_to_bin (RegionBA, BinImage, 255, 0, Width, Height)
-
- *间隔点数
- N:=4
- *卡尺阈值
- Gray:=40
- *//上边缘
- smallest_rectangle2 (RegionBU, RowBU, ColBU, PhiBU, L1BU, L2BU)
- get_rect2_vertex (RowBU, ColBU, PhiBU, L1BU-10, L2BU, RowTL, ColTL, RowTR, ColTR, RowBL, ColBL, RowBR, ColBR)
- ElementsU:=round(L1BU/4)
- rake (BinImage, RegionRakeU, ElementsU, 40, N, 1, Gray, 'all', 'first', RowTL, ColTL, RowTR, ColTR, RowsU, ColsU)
- gen_cross_contour_xld (CrossU, RowsU, ColsU, 6, PhiBU)
-
- *//下边缘
- smallest_rectangle2 (RegionBD, RowBD, ColBD, PhiBD, L1BD, L2BD)
- get_rect2_vertex (RowBD, ColBD, PhiBD, L1BD-10, L2BD, RowTL, ColTL, RowTR, ColTR, RowBL, ColBL, RowBR, ColBR)
- ElementsD:=round(L1BD/4)
- rake (BinImage, RegionRakeD, ElementsD, 40, N, 1, Gray, 'all', 'first', RowBL, ColBL, RowBR, ColBR, RowsD, ColsD)
- gen_cross_contour_xld (CrossD, RowsD, ColsD, 6, PhiBU)
-
- *//以少点的一边为基准计算距离
- if(|RowsU|<=|RowsD|)
- *上
- tuple_select (RowsD, [0:|RowsU|-1], RowsD)
- tuple_select (ColsD, [0:|RowsU|-1], ColsD)
- else
- *下
- tuple_select (RowsU, [0:|RowsD|-1], RowsU)
- tuple_select (ColsU, [0:|RowsD|-1], ColsU)
- endif
- distance_pp (RowsU, ColsU, RowsD, ColsD, DisRes)
- *//找最大/最小/平均
- tuple_max (DisRes, Max)
- tuple_find_first (DisRes, Max, MaxIdx)
- gen_arrow_contour_xld (ArrowMax, RowsU[MaxIdx], ColsU[MaxIdx], RowsD[MaxIdx], ColsD[MaxIdx], 30, 30)
-
- tuple_min (DisRes, Min)
- tuple_find_first (DisRes, Min, MinIdx)
- gen_arrow_contour_xld (ArrowMin, RowsU[MinIdx], ColsU[MinIdx], RowsD[MinIdx], ColsD[MinIdx], 30, 30)
-
- tuple_mean (DisRes, Mean)
-
- *//可视化
- dev_clear_window ()
- dev_display (Image)
- dev_set_color ('red')
- dev_display (CrossU)
- dev_display (CrossD)
- dev_set_color ('blue')
- dev_display (ArrowMin)
- dev_set_color ('green')
- dev_display (ArrowMax)
- disp_message (200000, 'DisMin: '+Min$'.2f'+' pix', 'window', 10, 10, 'blue', 'false')
- disp_message (200000, 'DisMax: '+Max$'.2f'+' pix', 'window', 25, 10, 'green', 'false')
- disp_message (200000, 'DisMean: '+Mean$'.2f'+' pix', 'window', 40, 10, 'red', 'false')
-
-
- stop ()
- endfor
复制代码 |