zc_yxx
助理工程师
助理工程师
  • 最后登录2015-12-31
  • 发帖数17
  • 金币371枚
  • 贡献勋章0枚
阅读:29930回复:116

halcon+vb检测光学玻璃元件实例

楼主#
更多 发布于:2013-08-20 10:05

图片:tuxiang.jpg


自然光下的玻璃元件实图

图片:270.jpg


环型光源下的玻璃元件图
halcon 代码

open_framegrabber ('DirectShow', 1, 1, 0, 0, 0, 0, 'default', 8, 'gray', -1, 'false', 'default', 'Microvision MV-1400UC Digital Camera', 0, -1, AcqHandle)
*打开摄像头
grab_image_start (AcqHandle, -1)
*开始铺货图像
grab_image_async (Image, AcqHandle, -1)
*捕获第一帧图像
get_image_size (Image, Width, Height)
*获得图像大小
dev_open_window (0, 0, Width/6, Height/6, 'black', WindowHandle)
*打开适合大小的窗口,应为相机是1400万像素所以图想太大窗口被我缩小了。
while (true) *无限循环
    try
     grab_image_async (Image, AcqHandle, -1)
*捕获一帧图像
    dev_display (Image)
*显示图像
    smooth_image(Image, ImageSmooth, 'deriche2', 0.5)
*平滑图像
    threshold (ImageSmooth, Regions,125, 255)
*阈值处理

图片:yuzhi.jpg


*这个 表面有些灰尘呵呵不过可以当噪点过滤掉的
area_center (Regions, Area1, Row3, Column3)
*获得区域中心点
    gen_contour_region_xld(Regions, Contours, 'border_holes')
*将阈值处理后获得的区域转换成xld
    smooth_contours_xld(Contours, SmoothedContours, 5)
*平滑xld
    select_contours_xld (SmoothedContours, SelectedContours1, 'contour_length', 4000, 8000, -0.5, 0.5)
*选择长度在4000到8000像素之间的xld
    select_contours_xld (Contours, SelectedContours2, 'contour_length', 10000, 13000, -0.5, 0.5)
*选择长度在10000到13000之间的xld

图片:xld.jpg


*选择后得到的xld图形就是最长的几段xld 其余的全部过滤掉了
    segment_contours_xld (SelectedContours2, ContoursSplit, 'lines_ellipses', 0, 2, 2)
*对方性xld进行分割 分割主要是为了去除尖刺突出的xld

*选取横向角度上的多边形而尖刺突出部分就被忽略了
pi := acos(0)*2
Eps := pi*2 /7

select_contours_xld (ContoursSplit, SelectedContours3, 'direction',  pi/2 + Eps, pi/2 - Eps, -0.5, 0.5)
*选择横向角度的多边形
select_contours_xld (SelectedContours3, SelectedContoursFinal, 'length', 20, 200000, -0.5, 0.5)
*选择横向角度的长度在20到200000的多边形 觉得这一部多余  呵呵不过是多次测试参数后才发现多余  之后就懒得改了。

union_collinear_contours_xld(SelectedContoursFinal, ContourTu, 150, 5, 10, 0.70, 'attr_keep')
*联合所有备选横向方向上的xld间距在150个像素一下的
select_contours_xld (ContourTu, ContourTuFinal, 'length', 1500, 200000, -0.5, 0.5)
*选择1500到200000像素之间的xld 这个选择主要是选出 拟合出来的玻璃元件上下边缘的直线



sort_contours_xld(ContourTuFinal, ContourTuSort, 'upper_left', 'true', 'row')
*索引选择的xld

图片:henxian.jpg


fit_line_contour_xld(ContourTuSort, 'tukey', -1, 0, 5, 2, RowBegin2, ColBegin2, RowEnd2, ColEnd2, Nr2, Nc2, Dist2)
*修正所选取的直线 ,说实话这句代码我写到后面发现 也是多余的 ,被后面的代码重复执行了
tuple_length(RowBegin2, Length)
*这句代码 貌似无作用 length变量从来没用到过
    gen_contour_polygon_xld (line1, [RowBegin2[0],RowEnd2[0]], [ColBegin2[0],ColEnd2[0]])
    gen_contour_polygon_xld (line2, [RowBegin2[Length-1],RowEnd2[Length-1]], [ColBegin2[Length-1],ColEnd2[Length-1]])
*显示最上和最下的边缘的两条直线

    fit_line_contour_xld(line1, 'tukey', -1, 0, 5, 2, RowBeginline1, ColBeginline1, RowEndline1, ColEndline1, Nrline1, Ncline1, Distline1)
    fit_line_contour_xld(line2, 'tukey', -1, 0, 5, 2, RowBeginline2, ColBeginline2, RowEndline2, ColEndline2, Nrline2, Ncline2, Distline2)
*修复上下两条直线

    smallest_rectangle2_xld (line2, Row5, Column5, Phi2, Length13, Length23)
*获取line2的phi2斜率角度。

vector_angle_to_rigid (Row3, Column3, Phi2, Row3, Column3,0, HomMat2D)
*生成HomMat2D数据枚举
affine_trans_image (ImageSmooth, Image2, HomMat2D, 'constant', 'false')
*根据程序头部阈值处理所得区域中心坐标 row3,column3 旋转 phi2 将图像摆正
*摆正后重新处理图像

图片:baizhen.jpg


*大家可以看一下 这个阈值处理后的图像和前面的图想有什么区别 斜角已经没了。下面的处理就是重复上方获得xld上下边缘线的程序了
    smooth_image(Image2, ImageSmooth, 'deriche2', 0.5)
    threshold (ImageSmooth, Regions,125, 255)
    gen_contour_region_xld(Regions, Contours, 'border_holes')
    smooth_contours_xld(Contours, SmoothedContours, 5)
    
    select_contours_xld (SmoothedContours, SelectedContours1, 'contour_length', 4000, 8000, -0.5, 0.5)
    select_contours_xld (Contours, SelectedContours2, 'contour_length', 10000,13000, -0.5, 0.5)
    segment_contours_xld (SelectedContours2, ContoursSplit, 'lines_ellipses', 0, 2, 2)
  
pi := acos(0)*2
Eps := pi*2 /7
select_contours_xld (ContoursSplit, SelectedContours3, 'direction',  pi/2 + Eps, pi/2 - Eps, -0.5, 0.5)
select_contours_xld (SelectedContours3, SelectedContoursFinal, 'length', 20, 200000, -0.5, 0.5)


union_collinear_contours_xld(SelectedContoursFinal, ContourTu, 150, 5, 10, 0.70, 'attr_keep')
select_contours_xld (ContourTu, ContourTuFinal, 'length', 1500, 200000, -0.5, 0.5)




sort_contours_xld(ContourTuFinal, ContourTuSort, 'upper_left', 'true', 'row')
fit_line_contour_xld(ContourTuSort, 'tukey', -1, 0, 5, 2, RowBegin2, ColBegin2, RowEnd2, ColEnd2, Nr2, Nc2, Dist2)
tuple_length(RowBegin2, Length)


    gen_contour_polygon_xld (line1, [RowBegin2[0],RowEnd2[0]], [ColBegin2[0],ColEnd2[0]])
    gen_contour_polygon_xld (line2, [RowBegin2[Length-1],RowEnd2[Length-1]], [ColBegin2[Length-1],ColEnd2[Length-1]])




    fit_line_contour_xld(line1, 'tukey', -1, 0, 5, 2, RowBeginline1, ColBeginline1, RowEndline1, ColEndline1, Nrline1, Ncline1, Distline1)
    fit_line_contour_xld(line2, 'tukey', -1, 0, 5, 2, RowBeginline2, ColBeginline2, RowEndline2, ColEndline2, Nrline2, Ncline2, Distline2)
  
    
    
*选取纵向上的多边形
*下面的程序 和上访选取上下边缘线的程序一样 只是这些是选取 左右边缘线的程序
pi := acos(0)*2
Eps := pi*2 /7
select_contours_xld (ContoursSplit, SelectedContours3, 'direction',  pi/2 - Eps, pi/2 + Eps, -0.5, 0.5)
select_contours_xld (SelectedContours3, SelectedContoursFinal, 'length', 20, 200000, -0.5, 0.5)


union_collinear_contours_xld(SelectedContoursFinal, ContourTu, 150, 5, 10, 0.20, 'attr_keep')
select_contours_xld (ContourTu, ContourTuFinal, 'length', 1500, 200000, -0.5, 0.5)




sort_contours_xld(ContourTuFinal, ContourTuSort, 'upper_left', 'true', 'column')
fit_line_contour_xld(ContourTuSort, 'tukey', -1, 0, 5, 2, RowBegin2, ColBegin2, RowEnd2, ColEnd2, Nr2, Nc2, Dist2)
tuple_length(RowBegin2, Length)


    gen_contour_polygon_xld (line1, [RowBegin2[0],RowEnd2[0]], [ColBegin2[0],ColEnd2[0]])
    gen_contour_polygon_xld (line2, [RowBegin2[Length-1],RowEnd2[Length-1]], [ColBegin2[Length-1],ColEnd2[Length-1]])


    fit_line_contour_xld(line1, 'tukey', -1, 0, 5, 2, RowBeginline3, ColBeginline3, RowEndline3, ColEndline3, Nrline3, Ncline3, Distline3)
    fit_line_contour_xld(line2, 'tukey', -1, 0, 5, 2, RowBeginline4, ColBeginline4, RowEndline4, ColEndline4, Nrline4, Ncline4, Distline4)

*计算两条直线之间的距离
 distance_sl(RowBeginline1, ColBeginline1, RowEndline1, ColEndline1, RowBeginline2, ColBeginline2, RowEndline2, ColEndline2, HeighMin1, HeighMax1)
*上海下边线间的距离
distance_sl(RowBeginline3, ColBeginline3, RowEndline3, ColEndline3, RowBeginline4, ColBeginline4, RowEndline4, ColEndline4,WidthMin1, WidthMax1)
*左右边线间的距离

        *算四个角点坐标拍,下面是计算玻璃元件四个角的直角角度
angle_ll(ColBeginline1, RowBeginline1, ColEndline1, RowEndline1,ColBeginline3, RowBeginline3, ColEndline3, RowEndline3, Angle1)
*计算两条直线之间的角度
angle_ll(ColBeginline1, RowBeginline1, ColEndline1, RowEndline1,ColBeginline4, RowBeginline4, ColEndline4, RowEndline4, Angle2)
angle_ll(ColBeginline4, RowBeginline4, ColEndline4, RowEndline4,ColBeginline2, RowBeginline2, ColEndline2, RowEndline2, Angle3)
angle_ll(ColBeginline2, RowBeginline2, ColEndline2, RowEndline2,ColBeginline3, RowBeginline3, ColEndline3, RowEndline3, Angle4)
tuple_deg(Angle1,deg1)*将弧度转化成角度
tuple_deg(Angle2,deg2)
tuple_deg(Angle3,deg3)
tuple_deg(Angle4,deg4)
    *内径圆的各个值    
    dev_display(SelectedContours1)    
*前面选取的4000到8000像素其实就是内圆的xld
      fit_ellipse_contour_xld(SelectedContours1, 'fitzgibbon', -1, 0, 0, 200, 3, 2, Row4, Column4, Phi3, Radius1, Radius2, StartPhi1, EndPhi1, PointOrder2)
*修正xld椭圆 并获得 圆的最大半径,最小半径,圆心坐标
    *圆心距
      distance_pl(Row4[1], Column4[1], RowBeginline1, ColBeginline1, RowEndline1, ColEndline1, Distance1)
*圆心到四边直线的距离
      distance_pl(Row4[1], Column4[1], RowBeginline2, ColBeginline2, RowEndline2, ColEndline2, Distance2)
      distance_pl(Row4[1], Column4[1], RowBeginline3, ColBeginline3, RowEndline3, ColEndline3, Distance3)
      distance_pl(Row4[1], Column4[1], RowBeginline4, ColBeginline4, RowEndline4, ColEndline4, Distance4)
      gen_ellipse_contour_xld(ContEllipse, Row4[0], Column4[0], Phi3[0], Radius1[0], Radius2[0], 0, 6.28318, 'positive', 1.5)
      gen_ellipse_contour_xld(ContEllipse1, Row4[1], Column4[1], Phi3[1], Radius1[1], Radius2[1], 0, 6.28318, 'positive', 1.5)
      dev_display(ContEllipse)  
          dev_display(ContEllipse1)  
    catch (Exception)
    endtry
endwhile
close_framegrabber (AcqHandle)程序到此结束 ,本程序得到了 这个玻璃元件的四边长度,四个角的角度,圆心到四边的距离,内圆的半径,虽然是简单的检测应用,但是希望可以给各位做点参考 呵呵。记得顶哟

图片:zuizhong.jpg

韩国高超
资深人士
资深人士
  • 最后登录2017-12-05
  • 发帖数50
  • 金币861枚
  • 贡献勋章0枚
  • 社区居民
沙发#
发布于:2013-08-20 10:15
虽然看不太懂,但是感觉好厉害的样子!
maididedidi
技术员
技术员
  • 最后登录2016-07-01
  • 发帖数17
  • 金币179枚
  • 贡献勋章0枚
  • 社区居民
板凳#
发布于:2013-08-20 10:19
先学习一下 貌似挺牛的
halcon菜鸟
助理工程师
助理工程师
  • 最后登录2016-04-13
  • 发帖数3
  • 金币375枚
  • 贡献勋章0枚
  • 社区居民
  • 忠实会员
地板#
发布于:2013-08-20 10:22
顶!!!!!!!!!!!!!!!!!!
zc_yxx
助理工程师
助理工程师
  • 最后登录2015-12-31
  • 发帖数17
  • 金币371枚
  • 贡献勋章0枚
4楼#
发布于:2013-08-20 10:23
vb界面

图片:vbjiemian.jpg




我把导出的 action里的打开摄像头部分放在了 开始检测 按钮中

Private Sub Command1_Click()
'On Error Resume Next
Start = True
  Call InitHalcon
  
  


  Call Op.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "gray", _
      -1, "false", "default", "Microvision MV-1400UC Digital Camera", 0, -1, hv_AcqHandle)
  Call Op.GrabImageStart(hv_AcqHandle, -1)
  Call Op.GrabImageAsync(ho_Image, hv_AcqHandle, -1)
  Call Op.GetImageSize(ho_Image, hv_Width, hv_Height)
  Call Op.SetWindowAttr("background_color", "black")
  Call Op.OpenWindow(0, 0, hv_Width / 6, hv_Height / 6, 0, "", "", hv_WindowHandle)
  COMExpWinHandleStack.Push (hv_WindowHandle)
  
  
Timer1.Enabled = True
  
End Sub


然后放了一个timer控件,每1.5秒执行一次检测

Private Sub Timer1_Timer()


  On Error Resume Next
    Call action
      
    txthv_HeighMax1.Text = (hv_HeighMax1 + hv_HeighMin1) / 2 * (zongxiangCD / zongxiang)
    txthv_WidthMax1.Text = (hv_WidthMax1 + hv_WidthMin1) / 2 * (hengxiangCD / hengxiang)
    txthv_Radius1.Text = (hv_Radius1(0) + hv_Radius1(1)) / 2 * (hengxiangCD / hengxiang)
    txthv_PointLineD1.Text = hv_PointLineD1 * (hengxiangCD / hengxiang)
    txthv_PointLineD2.Text = hv_PointLineD2 * (hengxiangCD / hengxiang)
    txthv_PointLineD3.Text = hv_PointLineD3 * (zongxiangCD / zongxiang)
    txthv_PointLineD4.Text = hv_PointLineD4 * (zongxiangCD / zongxiang)
    txthv_deg1.Text = hv_deg1
    txthv_deg2.Text = hv_deg2
    txthv_deg3.Text = hv_deg3
    txthv_deg4.Text = hv_deg4
    
    Command6_Click
    
      '   check
End Sub




action函数里自行主要的halcon检测程序
Public Sub action()
On Error Resume Next


  '
  'This example program demonstrates the use of union_collinear_contours_xld for connecting
  'collinear line segments.  This is the new and recommended operator for this task.
  'In addition, the program shows how to use the old operator union_straight_contours_xld for
  'connecting contours that are not collinear but have neighboring end points.
  '
'  If COMExpWinHandleStack.IsOpen() Then
'    'dev_close_window ()
'  End If




    Trigger = True
    If Trigger And Not issave Then
    ' try(); only in hdevelop
      Call Op.GrabImageAsync(ho_Image, hv_AcqHandle, -1)
      If COMExpWinHandleStack.IsOpen() Then
        Call Op.DispObj(ho_Image, COMExpWinHandleStack.GetActive())
      End If
      Call Op.SmoothImage(ho_Image, ho_ImageSmooth, "deriche2", 0.5)
      Call Op.Threshold(ho_ImageSmooth, ho_Regions, 125, 255)
      Call Op.AreaCenter(ho_Regions, hv_Area1, hv_Row3, hv_Column3)
      Call Op.GenContourRegionXld(ho_Regions, ho_Contours, "border_holes")
      Call Op.SmoothContoursXld(ho_Contours, ho_SmoothedContours, 5)
      Call Op.SelectContoursXld(ho_SmoothedContours, ho_SelectedContours1, "contour_length", _
          4000, 8000, -0.5, 0.5)
      Call Op.SelectContoursXld(ho_Contours, ho_SelectedContours2, "contour_length", _
          10000, 13000, -0.5, 0.5)
      Call Op.SegmentContoursXld(ho_SelectedContours2, ho_ContoursSplit, "lines_ellipses", _
          0, 2, 2)
      '选取横向上的多边形
      hv_pi = Tuple.TupleMult(Tuple.TupleAcos(0), 2)
      hv_Eps = Tuple.TupleDiv(Tuple.TupleMult(hv_pi, 2), 7)
      Call Op.SelectContoursXld(ho_ContoursSplit, ho_SelectedContours3, "direction", _
          Tuple.TupleAdd(Tuple.TupleDiv(hv_pi, 2), hv_Eps), Tuple.TupleSub(Tuple.TupleDiv( _
          hv_pi, 2), hv_Eps), -0.5, 0.5)
      Call Op.SelectContoursXld(ho_SelectedContours3, ho_SelectedContoursFinal, "length", _
          20, 200000, -0.5, 0.5)


      Call Op.UnionCollinearContoursXld(ho_SelectedContoursFinal, ho_ContourTu, 150, _
          5, 10, 0.7, "attr_keep")
      Call Op.SelectContoursXld(ho_ContourTu, ho_ContourTuFinal, "length", 1500, _
          200000, -0.5, 0.5)




      Call Op.SortContoursXld(ho_ContourTuFinal, ho_ContourTuSort, "upper_left", _
          "true", "row")
      Call Op.FitLineContourXld(ho_ContourTuSort, "tukey", -1, 0, 5, 2, hv_RowBegin2, _
          hv_ColBegin2, hv_RowEnd2, hv_ColEnd2, hv_Nr2, hv_Nc2, hv_Dist2)
      Call Op.TupleLength(hv_RowBegin2, hv_Length)


      Call Op.GenContourPolygonXld(ho_line1, Tuple.TupleConcat(Tuple.TupleSelect( _
          hv_RowBegin2, 0), Tuple.TupleSelect(hv_RowEnd2, 0)), Tuple.TupleConcat(Tuple.TupleSelect( _
          hv_ColBegin2, 0), Tuple.TupleSelect(hv_ColEnd2, 0)))
      Call Op.GenContourPolygonXld(ho_line2, Tuple.TupleConcat(Tuple.TupleSelect( _
          hv_RowBegin2, Tuple.TupleSub(hv_Length, 1)), Tuple.TupleSelect(hv_RowEnd2, _
          Tuple.TupleSub(hv_Length, 1))), Tuple.TupleConcat(Tuple.TupleSelect(hv_ColBegin2, _
          Tuple.TupleSub(hv_Length, 1)), Tuple.TupleSelect(hv_ColEnd2, Tuple.TupleSub( _
          hv_Length, 1))))


      Call Op.FitLineContourXld(ho_line1, "tukey", -1, 0, 5, 2, hv_RowBeginline1, _
          hv_ColBeginline1, hv_RowEndline1, hv_ColEndline1, hv_Nrline1, hv_Ncline1, _
          hv_Distline1)
      Call Op.FitLineContourXld(ho_line2, "tukey", -1, 0, 5, 2, hv_RowBeginline2, _
          hv_ColBeginline2, hv_RowEndline2, hv_ColEndline2, hv_Nrline2, hv_Ncline2, _
          hv_Distline2)


      Call Op.SmallestRectangle2Xld(ho_line2, hv_Row5, hv_Column5, hv_Phi2, hv_Length13, _
          hv_Length23)
      Call Op.VectorAngleToRigid(hv_Row3, hv_Column3, hv_Phi2, hv_Row3, hv_Column3, _
          0, hv_HomMat2D)
      Call Op.AffineTransImage(ho_ImageSmooth, ho_Image2, hv_HomMat2D, "constant", _
          "false")
      Call Op.SmoothImage(ho_Image2, ho_ImageSmooth, "deriche2", 0.5)
      Call Op.Threshold(ho_ImageSmooth, ho_Regions, 125, 255)
      Call Op.GenContourRegionXld(ho_Regions, ho_Contours, "border_holes")
      Call Op.SmoothContoursXld(ho_Contours, ho_SmoothedContours, 5)


      Call Op.SelectContoursXld(ho_SmoothedContours, ho_SelectedContours1, "contour_length", _
          4000, 8000, -0.5, 0.5)
      Call Op.SelectContoursXld(ho_Contours, ho_SelectedContours2, "contour_length", _
          10000, 13000, -0.5, 0.5)
      Call Op.SegmentContoursXld(ho_SelectedContours2, ho_ContoursSplit, "lines_ellipses", _
          0, 2, 2)


      hv_pi = Tuple.TupleMult(Tuple.TupleAcos(0), 2)
      hv_Eps = Tuple.TupleDiv(Tuple.TupleMult(hv_pi, 2), 7)
      Call Op.SelectContoursXld(ho_ContoursSplit, ho_SelectedContours3, "direction", _
          Tuple.TupleAdd(Tuple.TupleDiv(hv_pi, 2), hv_Eps), Tuple.TupleSub(Tuple.TupleDiv( _
          hv_pi, 2), hv_Eps), -0.5, 0.5)
      Call Op.SelectContoursXld(ho_SelectedContours3, ho_SelectedContoursFinal, "length", _
          20, 200000, -0.5, 0.5)


      Call Op.UnionCollinearContoursXld(ho_SelectedContoursFinal, ho_ContourTu, 150, _
          5, 10, 0.7, "attr_keep")
      Call Op.SelectContoursXld(ho_ContourTu, ho_ContourTuFinal, "length", 1500, _
          200000, -0.5, 0.5)




      Call Op.SortContoursXld(ho_ContourTuFinal, ho_ContourTuSort, "upper_left", _
          "true", "row")
      Call Op.FitLineContourXld(ho_ContourTuSort, "tukey", -1, 0, 5, 2, hv_RowBegin2, _
          hv_ColBegin2, hv_RowEnd2, hv_ColEnd2, hv_Nr2, hv_Nc2, hv_Dist2)
      Call Op.TupleLength(hv_RowBegin2, hv_Length)


      Call Op.GenContourPolygonXld(ho_line1, Tuple.TupleConcat(Tuple.TupleSelect( _
          hv_RowBegin2, 0), Tuple.TupleSelect(hv_RowEnd2, 0)), Tuple.TupleConcat(Tuple.TupleSelect( _
          hv_ColBegin2, 0), Tuple.TupleSelect(hv_ColEnd2, 0)))
      Call Op.GenContourPolygonXld(ho_line2, Tuple.TupleConcat(Tuple.TupleSelect( _
          hv_RowBegin2, Tuple.TupleSub(hv_Length, 1)), Tuple.TupleSelect(hv_RowEnd2, _
          Tuple.TupleSub(hv_Length, 1))), Tuple.TupleConcat(Tuple.TupleSelect(hv_ColBegin2, _
          Tuple.TupleSub(hv_Length, 1)), Tuple.TupleSelect(hv_ColEnd2, Tuple.TupleSub( _
          hv_Length, 1))))




      Call Op.FitLineContourXld(ho_line1, "tukey", -1, 0, 5, 2, hv_RowBeginline1, _
          hv_ColBeginline1, hv_RowEndline1, hv_ColEndline1, hv_Nrline1, hv_Ncline1, _
          hv_Distline1)
      Call Op.FitLineContourXld(ho_line2, "tukey", -1, 0, 5, 2, hv_RowBeginline2, _
          hv_ColBeginline2, hv_RowEndline2, hv_ColEndline2, hv_Nrline2, hv_Ncline2, _
          hv_Distline2)






      '选取纵向上的多边形
      hv_pi = Tuple.TupleMult(Tuple.TupleAcos(0), 2)
      hv_Eps = Tuple.TupleDiv(Tuple.TupleMult(hv_pi, 2), 7)
      Call Op.SelectContoursXld(ho_ContoursSplit, ho_SelectedContours3, "direction", _
          Tuple.TupleSub(Tuple.TupleDiv(hv_pi, 2), hv_Eps), Tuple.TupleAdd(Tuple.TupleDiv( _
          hv_pi, 2), hv_Eps), -0.5, 0.5)
      Call Op.SelectContoursXld(ho_SelectedContours3, ho_SelectedContoursFinal, "length", _
          20, 200000, -0.5, 0.5)


      Call Op.UnionCollinearContoursXld(ho_SelectedContoursFinal, ho_ContourTu, 150, _
          5, 10, 0.2, "attr_keep")
      Call Op.SelectContoursXld(ho_ContourTu, ho_ContourTuFinal, "length", 1500, _
          200000, -0.5, 0.5)




      Call Op.SortContoursXld(ho_ContourTuFinal, ho_ContourTuSort, "upper_left", _
          "true", "column")
      Call Op.FitLineContourXld(ho_ContourTuSort, "tukey", -1, 0, 5, 2, hv_RowBegin2, _
          hv_ColBegin2, hv_RowEnd2, hv_ColEnd2, hv_Nr2, hv_Nc2, hv_Dist2)
      Call Op.TupleLength(hv_RowBegin2, hv_Length)


      Call Op.GenContourPolygonXld(ho_line1, Tuple.TupleConcat(Tuple.TupleSelect( _
          hv_RowBegin2, 0), Tuple.TupleSelect(hv_RowEnd2, 0)), Tuple.TupleConcat(Tuple.TupleSelect( _
          hv_ColBegin2, 0), Tuple.TupleSelect(hv_ColEnd2, 0)))
      Call Op.GenContourPolygonXld(ho_line2, Tuple.TupleConcat(Tuple.TupleSelect( _
          hv_RowBegin2, Tuple.TupleSub(hv_Length, 1)), Tuple.TupleSelect(hv_RowEnd2, _
          Tuple.TupleSub(hv_Length, 1))), Tuple.TupleConcat(Tuple.TupleSelect(hv_ColBegin2, _
          Tuple.TupleSub(hv_Length, 1)), Tuple.TupleSelect(hv_ColEnd2, Tuple.TupleSub( _
          hv_Length, 1))))


      Call Op.FitLineContourXld(ho_line1, "tukey", -1, 0, 5, 2, hv_RowBeginline3, _
          hv_ColBeginline3, hv_RowEndline3, hv_ColEndline3, hv_Nrline3, hv_Ncline3, _
          hv_Distline3)
      Call Op.FitLineContourXld(ho_line2, "tukey", -1, 0, 5, 2, hv_RowBeginline4, _
          hv_ColBeginline4, hv_RowEndline4, hv_ColEndline4, hv_Nrline4, hv_Ncline4, _
          hv_Distline4)


      Call Op.DistanceSl(hv_RowBeginline1, hv_ColBeginline1, hv_RowEndline1, hv_ColEndline1, _
          hv_RowBeginline2, hv_ColBeginline2, hv_RowEndline2, hv_ColEndline2, hv_HeighMin1, _
          hv_HeighMax1)
      Call Op.DistanceSl(hv_RowBeginline3, hv_ColBeginline3, hv_RowEndline3, hv_ColEndline3, _
          hv_RowBeginline4, hv_ColBeginline4, hv_RowEndline4, hv_ColEndline4, hv_WidthMin1, _
          hv_WidthMax1)
      'Do something


      '算四个角点坐标拍
      Call Op.AngleLl(hv_ColBeginline1, hv_RowBeginline1, hv_ColEndline1, hv_RowEndline1, _
          hv_ColBeginline3, hv_RowBeginline3, hv_ColEndline3, hv_RowEndline3, hv_Angle1)
      Call Op.AngleLl(hv_ColBeginline1, hv_RowBeginline1, hv_ColEndline1, hv_RowEndline1, _
          hv_ColBeginline4, hv_RowBeginline4, hv_ColEndline4, hv_RowEndline4, hv_Angle2)
      Call Op.AngleLl(hv_ColBeginline4, hv_RowBeginline4, hv_ColEndline4, hv_RowEndline4, _
          hv_ColBeginline2, hv_RowBeginline2, hv_ColEndline2, hv_RowEndline2, hv_Angle3)
      Call Op.AngleLl(hv_ColBeginline2, hv_RowBeginline2, hv_ColEndline2, hv_RowEndline2, _
          hv_ColBeginline3, hv_RowBeginline3, hv_ColEndline3, hv_RowEndline3, hv_Angle4)
      Call Op.TupleDeg(hv_Angle1, hv_deg1)
      Call Op.TupleDeg(hv_Angle2, hv_deg2)
      Call Op.TupleDeg(hv_Angle3, hv_deg3)
      Call Op.TupleDeg(hv_Angle4, hv_deg4)
      '内径圆的各个值
      If COMExpWinHandleStack.IsOpen() Then
        Call Op.DispObj(ho_SelectedContours1, COMExpWinHandleStack.GetActive())
      End If
      'segment_contours_xld (SelectedContours1, Contourscircal, 'lines_ellipses', 0, 2, 2)
      Call Op.FitEllipseContourXld(ho_SelectedContours1, "fitzgibbon", -1, 0, 0, _
          200, 3, 2, hv_Row4, hv_Column4, hv_Phi3, hv_Radius1, hv_Radius2, hv_StartPhi1, _
          hv_EndPhi1, hv_PointOrder2)
      '圆心距
      Call Op.DistancePl(Tuple.TupleSelect(hv_Row4, 1), Tuple.TupleSelect(hv_Column4, _
          1), hv_RowBeginline1, hv_ColBeginline1, hv_RowEndline1, hv_ColEndline1, _
          hv_PointLineD1)
      Call Op.DistancePl(Tuple.TupleSelect(hv_Row4, 1), Tuple.TupleSelect(hv_Column4, _
          1), hv_RowBeginline2, hv_ColBeginline2, hv_RowEndline2, hv_ColEndline2, _
          hv_PointLineD2)
      Call Op.DistancePl(Tuple.TupleSelect(hv_Row4, 1), Tuple.TupleSelect(hv_Column4, _
          1), hv_RowBeginline3, hv_ColBeginline3, hv_RowEndline3, hv_ColEndline3, _
          hv_PointLineD3)
      Call Op.DistancePl(Tuple.TupleSelect(hv_Row4, 1), Tuple.TupleSelect(hv_Column4, _
          1), hv_RowBeginline4, hv_ColBeginline4, hv_RowEndline4, hv_ColEndline4, _
          hv_PointLineD4)
      Call Op.GenEllipseContourXld(ho_ContEllipse, Tuple.TupleSelect(hv_Row4, 0), _
          Tuple.TupleSelect(hv_Column4, 0), Tuple.TupleSelect(hv_Phi3, 0), Tuple.TupleSelect( _
          hv_Radius1, 0), Tuple.TupleSelect(hv_Radius2, 0), 0, 6.28318, "positive", _
          1.5)
      Call Op.GenEllipseContourXld(ho_ContEllipse1, Tuple.TupleSelect(hv_Row4, 1), _
          Tuple.TupleSelect(hv_Column4, 1), Tuple.TupleSelect(hv_Phi3, 1), Tuple.TupleSelect( _
          hv_Radius1, 1), Tuple.TupleSelect(hv_Radius2, 1), 0, 6.28318, "positive", _
          1.5)
      If COMExpWinHandleStack.IsOpen() Then
        Call Op.DispObj(ho_ContEllipse, COMExpWinHandleStack.GetActive())
      End If
      If COMExpWinHandleStack.IsOpen() Then
        Call Op.DispObj(ho_ContEllipse1, COMExpWinHandleStack.GetActive())
      End If
    


        
        
        
''        If rs.State <> 0 Then rs.Close
''        rs.Open "select * from 检测数据 where 1=2", conn, 1, 3
''
''        rs.AddNew
''        rs.Fields("批号").Value = txtpihao.Text
''        rs.Fields("罩号").Value = txtzhaohao.Text
''        rs.Fields("长度").Value = Val(txthv_WidthMax1.Text)
''        rs.Fields("高度").Value = Val(txthv_HeighMax1.Text)
''        rs.Fields("角度1").Value = Val(txthv_deg1.Text)
''        rs.Fields("角度2").Value = Val(txthv_deg2.Text)
''        rs.Fields("角度3").Value = Val(txthv_deg3.Text)
''        rs.Fields("角度4").Value = Val(txthv_deg4.Text)
''        rs.Fields("内圆半径").Value = Val(txthv_Radius1.Text)
''        rs.Fields("圆心边距1").Value = Val(txthv_PointLineD1.Text)
''        rs.Fields("圆心边距2").Value = Val(txthv_PointLineD1.Text)
''        rs.Fields("圆心边距3").Value = Val(txthv_PointLineD1.Text)
''        rs.Fields("圆心边距4").Value = Val(txthv_PointLineD1.Text)
''        rs.Update
''        rs.Close
''        rs.Open "select * from 检测数据 where 罩号='" & txtzhaohao.Text & "'"
       'Set MSHFlexGrid1.DataSource = rs
       ' issave = True
        Trigger = False
    End If
      'set_tposition (WindowHandle, 1, 1)
      'write_string (WindowHandle, 'Longmin: '+DistanceMin2+' pix Longmax:'+DistanceMax2+'pix')
      'set_tposition (WindowHandle, 20, 1)
      'write_string (WindowHandle, 'Heighmin: '+DistanceMin1+' pix Heighmax:'+DistanceMax1+'pix')
    ' catch(); only in hdevelop
    ' endtry(); only in hdevelop
    #If USE_DO_EVENTS Then
    ' Please note: The call of DoEvents() is only a hack to
    ' enable VB to react on events. Please change the code
    ' so that it can handle events in a standard way.
    DoEvents
    #End If
    DoEvents


End Sub
小马_xiao
VIP会员
VIP会员
  • 最后登录2017-07-07
  • 发帖数133
  • 金币1763枚
  • 贡献勋章0枚
  • 社区居民
  • 忠实会员
5楼#
发布于:2013-08-20 10:26
好麻烦额~~~~~~~~~~
HALCON实战讲解系列课程http://www.ihalcon.com/read-305.html
Jason_Pan
助理工程师
助理工程师
  • 最后登录2014-04-04
  • 发帖数58
  • 金币583枚
  • 贡献勋章0枚
  • 社区居民
  • 忠实会员
6楼#
发布于:2013-08-20 11:34
收藏了,留着备用~~~~~~~~~
Jason_Pan
助理工程师
助理工程师
  • 最后登录2014-04-04
  • 发帖数58
  • 金币583枚
  • 贡献勋章0枚
  • 社区居民
  • 忠实会员
7楼#
发布于:2013-08-20 11:37
好麻烦啊,还是用LabVIEW好了,就是不知道精度和这个比有什么区别
红颜sad
VIP会员
VIP会员
  • 最后登录2014-05-14
  • 发帖数45
  • 金币870枚
  • 贡献勋章0枚
8楼#
发布于:2013-08-20 14:12
不错,值得学习呀 可以试试  ,鼓励多多的写代码
sxbjcft
工程师
工程师
  • 最后登录2017-12-12
  • 发帖数140
  • 金币856枚
  • 贡献勋章1枚
  • 社区居民
  • 忠实会员
9楼#
发布于:2013-08-20 16:24
刚刚开始学习,看不懂,但想来一定很牛B,顶
levizhang
高级工程师
高级工程师
  • 最后登录2017-09-30
  • 发帖数81
  • 金币1484枚
  • 贡献勋章0枚
  • 社区居民
  • 忠实会员
10楼#
发布于:2013-08-20 17:14
挺好,只是重复性的东西太多了,或许可以优化下
ARM+FPGA+DSP远离PC
li83126
初学者
初学者
  • 最后登录2014-05-26
  • 发帖数5
  • 金币31枚
  • 贡献勋章0枚
11楼#
发布于:2013-08-23 21:36
楼主最好把VB源代码发出来,大家学习下
jxl_1982
初学者
初学者
  • 最后登录2015-06-28
  • 发帖数11
  • 金币59枚
  • 贡献勋章0枚
12楼#
发布于:2013-08-24 12:24
好贴子,楼主奉献精神值得赞赏。。。
xakann
工程师
工程师
  • 最后登录2017-10-14
  • 发帖数33
  • 金币727枚
  • 贡献勋章0枚
  • 社区居民
  • 忠实会员
13楼#
发布于:2013-08-25 00:51
好详细的halcon+VB的实际程序说明
seesee
助理工程师
助理工程师
  • 最后登录2015-12-09
  • 发帖数80
  • 金币544枚
  • 贡献勋章0枚
  • 社区居民
  • 忠实会员
14楼#
发布于:2013-08-27 17:19
写的这么详细啊,好贴子
上一页
游客

返回顶部