halcon之com组件的使用
By
admin
at 2018-04-02 • 0人收藏 • 1816人看过
操作图片com组件有两种方式,
第一种接口方式:
var HOperatorSetX = com.CreateObject("{6ebd90e2-d219-11d2-ade5-0000c00f4ef9}")
读取图片,并获取大小尺寸
var filepath = com.Variant("res/double_circle.png"); var Image = HOperatorSetX.ReadImage(filepath) var width,height = HOperatorSetX.GetImageSize(Image); console.log(width,height)
第二种接口方式:
var HImageX = com.CreateObject("{6ebd90e7-d219-11d2-ade5-0000c00f4ef9}")
同样的读取图片和大小
var que = com.Variant("res/double_circle.png"); HImageX.ReadImage(que); var w,l = HImageX.GetImageSize(); console.varDump(w) console.varDump(l)
因为平时用到的程序书写方式,大多数时候都上第一种(函数中带返回值和参数)
所以,后续基本不在介绍第二种接口方式, 但是应该知道这两种方式都是可行的.
请看下图: halcon手册函数对照表
2 个回复 | 最后更新于 2018-04-02
下面先上一个完整的示例:
还是先前那个halcon_circle的测试COM组件版本:
这一次,效果终于和halcon的ide效果一致了.
原来那个C语言调用版本,引用到T_开头的函数时候就不能正常进行下去了,用COM版本没有这个限制了.Fighting !
mainForm.button.oncommand = function(id,event){ //var filepath = com.Variant("res/double_circle.png"); //console.varDump(filepath) var Image = HOperatorSetX.ReadImage("res/double_circle.png"); var width,height = HOperatorSetX.GetImageSize(Image); console.log(width,height) //var fatherw = com.Variant(mainForm.picturebox.hwnd) var WindowHandle = HOperatorSetX.OpenWindow(0,0,width,height,mainForm.picturebox.hwnd,"transparent",""); console.varDump(WindowHandle) //HOperatorSetX.DispObj(Image,WindowHandle); var Region = HOperatorSetX.FastThreshold(Image,0,120,7) //HOperatorSetX.DispObj(Region,WindowHandle); var RegionBorder = HOperatorSetX.Boundary(Region,'inner') //HOperatorSetX.DispObj(RegionBorder,WindowHandle); var RegionClipped = HOperatorSetX.ClipRegionRel(RegionBorder, 5, 5, 5, 5) var RegionDilation = HOperatorSetX.DilationCircle(RegionClipped, 2.5) var ImageReduced = HOperatorSetX.ReduceDomain(Image, RegionDilation) var Edges = HOperatorSetX.EdgesSubPix(ImageReduced, 'canny', 2, 20, 60) var ContoursSplit = HOperatorSetX.SegmentContoursXld(Edges, 'lines_circles', 5, 4, 3) var Number = HOperatorSetX.CountObj(ContoursSplit) HOperatorSetX.DispObj(Image,WindowHandle); HOperatorSetX.SetDraw(WindowHandle,"margin"); HOperatorSetX.SetColor(WindowHandle,"white"); for(i=1;Number;1){ var ObjectSelected = HOperatorSetX.SelectObj(ContoursSplit, i) var Attrib = HOperatorSetX.GetContourGlobalAttribXld(ObjectSelected, 'cont_approx') if(Attrib>0){ var Row, Column, Radius, StartPhi, EndPhi, PointOrder = HOperatorSetX.FitCircleContourXld(ObjectSelected, 'ahuber', -1, 2, 0, 3, 2) var ContCircle = HOperatorSetX.GenCircleContourXld( Row, Column, Radius, 0, math.rad(360), 'positive', 1.0) HOperatorSetX.DispObj(ContCircle,WindowHandle); } } HOperatorSetX.SetColored(WindowHandle,12); HOperatorSetX.SetLineWidth(WindowHandle,3); HOperatorSetX.DispObj(ContoursSplit,WindowHandle); }
登录后方可回帖
2.显示图片到窗体
从上面我们能得到一个猜测:
看到上面的openwindow中的父窗口句柄了吗, 我本来是用
//var fatherw = com.Variant(mainForm.picturebox.hwnd)
这个来实现的,因为函数中我看到这个参数是variant格式,
但是,我直接写也没有出错, 那么说明一点, 这个变体参数, 可以灵活使用.
因此, 我们上面的读取图片那个就可以改为
直接写图片地址进去!