钢轨长度粗计算

学习时候的第一个halcon程序代码
钢轨长度粗计算_第1张图片
*思路计算尺寸,生产矩形就可以知道了。
*1.有精度要求的亚像素级别 的使用xld,没有的用region。
*2设置RIO,二值化,选择区域,生产矩形,得到尺寸。
*3 结果显示
read_image (Image, ‘钢轨尺寸.jpg’)
decompose3 (Image, Image1, Image2, Image3)
rgb1_to_gray (Image, GrayImage)
invert_image (GrayImage, ImageInvert)

dev_set_draw (‘margin’)
dev_display (ImageInvert)
*ROI测整个高度,中间宽度。
gen_rectangle1 (Rectangle4, 25, 215,521, 258)
reduce_domain (GrayImage, Rectangle4, ImageReduced2)
emphasize (ImageReduced2, ImageEmphasize1, 7, 7, 1)
mean_image (ImageEmphasize1, ImageMean2, 3, 3)
threshold_sub_pix (ImageMean2, Border, 152)
shape_trans_xld (Border, XLDTrans, ‘rectangle1’)
smallest_rectangle1_xld (XLDTrans, Row13, Column13, Row23, Column23)
select_shape_xld (XLDTrans, SelectedXLD, ‘area’, ‘and’, 150, 99999)
smallest_rectangle1_xld (SelectedXLD, Row14, Column14, Row24, Column24)
lm:=Column24-Column14
lk:=Row24-Row14

**上部,底部尺寸。
emphasize (ImageInvert, ImageEmphasize, 9, 9, 1)
add_image (ImageInvert, ImageEmphasize, ImageResult, 0.5,30)
edges_sub_pix (ImageResult, Edges, ‘deriche1_junctions’, 0.51, 50, 100)
sort_contours_xld (Edges, SortedContours1, ‘upper_left’, ‘true’, ‘row’)
shape_trans_xld (SortedContours1, XLDTrans1, ‘rectangle1’)
select_shape_xld (XLDTrans1, SelectedXLD1, ‘area’, ‘and’, 5500, 99999)
smallest_rectangle1_xld (SelectedXLD1, Row12, Column12, Row22, Column22)
*显示结果:
dev_display (ImageInvert)
l1:=Row22-Row12
w1:=Column22-Column12
dev_get_window (WindowHandle)
for i:=0 to |Row12|-1 by 1
*顶部,底部长
if(i=0)
disp_message (WindowHandle, ‘顶部长’+ w1[i], ‘image’, Row22[i], Column22[i], ‘green’, ‘true’)
gen_rectangle1 (Rectangle2, Row12[i], Column12[i], Row22[i]-200, Column22[i])
dev_display (Rectangle2)
else

    disp_message (WindowHandle, '底部长'+ w1[i], 'image', Row22[i], Column22[i], 'green', 'true')
    gen_rectangle1 (Rectangle2, Row12[i]+120, Column12[i], Row22[i], Column22[i])
    dev_display (Rectangle2)

endif
*高,中间宽度
disp_message (WindowHandle, ‘中间长’+lm[0], ‘image’, Row14[0], Column24[0], ‘green’, ‘true’)
disp_message (WindowHandle, ‘中间宽’+lk[0], ‘image’, Row14[0]+60, Column24[0]+5, ‘green’, ‘true’)
gen_rectangle1 (Rectangle5, Row14, Column14, Row24, Column24)

endfor

你可能感兴趣的:(算法)