新手C#常見技能-視覺芯片引腳間距測(cè)量

大家好,我是華山自控編程朱老師
今天跟大家分享一個(gè)用C#來(lái)處理視覺間距測(cè)量的技能,下面我們講解測(cè)量芯片引腳間距的一個(gè)實(shí)例,希望能幫助到大家。
程序運(yùn)行截圖如下:
? ? ? ? ? ? ? ? ? ? ?

下面我們就將該程序涉及的知識(shí)點(diǎn)進(jìn)行介紹:
該程序引用了halcon庫(kù)的相關(guān)視覺測(cè)量api。
具體過(guò)程如下:
1)我們需要加載原始圖片:該步驟涉及到halocn的HImage類,實(shí)現(xiàn)代碼如下:var image = new HImage(Path.GetFileName("pin.bmp"))。
2) 第二步,我們需要將RGB圖像轉(zhuǎn)換為灰度圖像。具體為將第一步的image做個(gè)轉(zhuǎn)換,實(shí)現(xiàn)代碼如下:HImage _image = image.Rgb1ToGray();
3)第三步,我們就可以將該灰度圖像顯示到窗體上了。具體實(shí)現(xiàn)代碼如下:
??????????????? HWindow _window = hWindowControl1.HalconWindow;
??????????????? _image.DispImage(_window);
4)? 第四步,我們需要獲取圖像的大小,也就是寬度和高度。具體實(shí)現(xiàn)代碼如下:_image.GetImageSize(out int width, out int height);
5) 第五步(算子核心代碼)。我們將獲取到的寬度width和高度height。用來(lái)提取垂直于輪廓線的直邊
??????????????? var measureObject = new HMeasure();
??????????????? measureObject.GenMeasureRectangle2(155.5, 319.5, 0.0760436, 105.304, 8.0, width, height, "nearest_neighbor");
6)第六步(算子核心代碼)我們?cè)谔崛〈怪庇诰匦位蛘攮h(huán)形弧的直邊。具體實(shí)現(xiàn)代碼如下:measureObject.MeasurePos(_image, 1, 30, "all", "all", out HTuple rowEdge, out HTuple columnEdge, out HTuple amplitude, out HTuple distance);
7)第七步,我們將尋到的中心點(diǎn)坐標(biāo)循環(huán)輸出到界面上:輸出中心點(diǎn)直線:
? _window.DispLine(rowEdge[index].D, columnEdge[index].D, rowEdge[index + 1].D, columnEdge[index + 1].D);
8)第八步,計(jì)算第七步的直線長(zhǎng)度:
var dist = HMisc.DistancePp(rowEdge[index].D, columnEdge[index].D, rowEdge[index + 1].D, columnEdge[index + 1].D);
9)在直線附近輸出第八步的長(zhǎng)度到界面上:
_window.SetTposition(Convert.ToInt32(rowEdge[index].D + 5), Convert.ToInt32(columnEdge[index].D));
?_window.WriteString(dist.ToString("N2"));
?
以上就是halcon測(cè)試芯片引腳間距的一個(gè)簡(jiǎn)單實(shí)例。關(guān)于第五步和第六步的兩個(gè)算子gen_measure_rectangle2
和measure_pos用法,這里在詳細(xì)的說(shuō)明一下:
?
1)GenMeasureRectangle2原型
gen_measure_rectangle2( : : Row, Column, Phi, Length1, Length2, Width, Height, Interpolation : MeasureHandle)
功能
準(zhǔn)備提取垂直于輪廓線的直邊
?
參數(shù)列表
Row (input_control):矩形中心點(diǎn)的行坐標(biāo)
Column (input_control):矩形中心點(diǎn)的列坐標(biāo)
Phi (input_control):輪廓線與水平方向的夾角(弧度制)
Length1 (input_control):矩形長(zhǎng)軸的一半
Length2 (input_control):矩形短軸的一半
Width (input_control) :待處理圖像的寬度
Height (input_control) :待處理圖像的高度
Interpolation (input_control) :插值方式(‘bicubic’, ‘bilinear’, ‘nearest_neighbor’)
MeasureHandle (output_control) :測(cè)量對(duì)象句柄
?
2)MeasurePos原型
measure_pos(Image : : MeasureHandle, Sigma, Threshold, Transition, Select : RowEdge, ColumnEdge, Amplitude, Distance)
?
功能
提取垂直于矩形或者環(huán)形弧的直邊。
?
參數(shù)列表
Image (input_object) :輸入圖像
MeasureHandle (input_control) :測(cè)量對(duì)象句柄
Sigma (input_control) :高斯濾波參數(shù)
Threshold (input_control) :邊緣強(qiáng)度控制閾值
Transition (input_control) :邊界從白到黑還是從黑到白(分別對(duì)應(yīng)’negative’, ‘positive’,也可以選擇‘a(chǎn)ll’)
Select (input_control) :選擇尋點(diǎn)的模式( 'all’輸出所有點(diǎn), 'first’輸出第一個(gè)點(diǎn), 'last’輸出最后一個(gè)點(diǎn))
RowEdge (output_control) :尋到直邊的中心點(diǎn)行坐標(biāo)
ColumnEdge (output_control) :尋到直邊的中心點(diǎn)列坐標(biāo)
Amplitude (output_control) :尋到直邊的邊緣強(qiáng)度值
Distance (output_control) :連續(xù)邊之間的距離
?