Halcon 1D Measurement (1): Measurement of the edge

First, to explain Hdevelop graphical display interface coordinates: coordinates and other environments are substantially the same as the image, the upper left corner is (0,0), X axis as the horizontal axis, the Y axis is the vertical axis refers to the row Row Y axis coordinates, it refers to a column column X coordinate

About gen_measure_rectangle2 :

It is a measure to get a handle

Row, Column is the center of the rectangle

Length1, Length2 long and a half width of the rectangular area, on the width of the region, as far as large as possible while the edge wrap provided, because too much noise provided

Rectangle Phi is the angle of rotation when rotating counter-clockwise is positive when clockwise rotation is represented by a negative

If you want to see whether proper ROI can use gen_rectangle2 View

Interpolation Interpolation representation because when the ROI is not vertical or horizontal, then his Projection (short equidistance line in the figure) through which it is not a complete pixel to calculate its average value as to Profile. It requires an interpolation value, see detailed solution_guide_iii_a_1d_measuring A-12

 

 

About measure_pairs :

measure_pairs applicable to the number of sets of edge images, if a set of edges, can also be used measure_pos , the following example will be first. When using their edge, if a straight line, the result contains a lot of information, each edge RowEdge represents the row coordinate edge, ColumnEdge represents the column coordinate, Amplitude indicates that the gradient Transition edges, IntraDistance showing an edge the distance between the groups, InterDistance represents the distance between the edge of the group, these two distances can be calculated height of the object and the calculated number of the object

Transition When a pixel value is represented by a high to low edge of the pixel values ​​in RowEdgeFirst, there is a reference direction, that is, the rotation angle is 0 Rectangle = 'negative', left to right direction

Sigma represents Gaussian filter parameters, the Gaussian filter is a graph of the Profile form, the Threshold is his threshold value of the first derivative is provided (bottom bold line is Profile Line through a pixel, a thin line of his first derivative, can it is seen that there are about three edges)

If you want to visualize edge, we need to use RowEdge, Column, Phi themselves determine the starting point and end point of the edge

Note that, since no camera calibration, 1D measurement object can obtain the number of pixels

*测量手机电池的高度有多少个像素
 
*
*读取图像
*
read_image (Image1, 'D:/picture/20130930153459.jpg')
get_image_size (Image1, Width, Height)
 
dev_close_window()
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
*设置所画的区域是一个面还是轮廓
dev_set_draw ('margin')
*设置画的颜色
dev_set_color ('black')
*因为我使用的彩色RGB图像,先使他变成灰度图像
decompose3 (Image1, red, green, blue)
rgb3_to_gray (red, green, blue, ImageGray)
dev_display(ImageGray)
 
*
*设置ROI
*
*注意row和column是矩形的中心
row := 238
column := 300
*这个是矩形旋转的角度,角度是正的按逆时针转,负的按顺时针转
angle := rad(90)
*在length1<length2的情况下,measure——pairs没有值,不管怎样调整参数???
*length1和length2是矩形的两个半轴的长,明白了这个下面在计算多边形轮廓的时候要用到
length1 := 200
length2 := 10
*注意这里的矩形框的参数是自己调整的
gen_rectangle2 (Rectangle, row, column, angle, length1, length2)
*这里的测量矩形框也就是上面显示的部分
gen_measure_rectangle2 (row, column, angle, length1, length2, Width, Height, 'nearest_neighbor', MeasureHandle)
 
disp_continue_message (WindowHandle, 'black', 'true')
 
stop()
 
 
*注意这个算子可以计算许多组边缘,然后将获得的边缘组放在RowEdgeFirst, ColumnEdgeFirst, RowEdgeSecond, ColumnEdgeSecond中
*根据Thansition的不同值,所获得的边缘组的先后顺序不同,例如为'negative'意思是将‘从白至黑’的边缘 中心点 的坐标放在RowEdgeFirst,注意这里的参考方向,是角度为0时,是从左往右方向 
*ColumnEdgeFirst中,‘从黑到白’放在RowEdgeSecond, ColumnEdgeSecond中
measure_pairs (Image1, MeasureHandle, 0.9, 30, 'negative', 'all', RowEdgeFirst, ColumnEdgeFirst, AmplitudeFirst, RowEdgeSecond, ColumnEdgeSecond, AmplitudeSecond, IntraDistance, InterDistance)
 
disp_continue_message (WindowHandle, 'black', 'true')
 
stop()
 
*
*可视化结果
*
*根据分组的个数,来画线,这个个数就是RowEdgeFirst组的个数,也就是边缘的个数
for i := 0 to |RowEdgeFirst|-1 by 1    
    *这个算子是画出多边形的亚像素轮廓,其中第二个和第三个参数可以是元素,即表示有多个点,两个点组成一条直线
    *至于每一个点是怎么计算的,我们已经知道了旋转的角度和每一条边的中心点,这个大家自己就可以在纸上用三角函数得出来每一个开始结束点
    *的坐标了
    gen_contour_polygon_xld (EdgeFirst, [RowEdgeFirst[i] - sin(rad(90) - angle)*length2,RowEdgeFirst[i] + sin(rad(90) - angle)*length2], [ColumnEdgeFirst[i] - cos(rad(90) - angle)*length2,ColumnEdgeFirst[i] + cos(rad(90) - angle)*length2])
    gen_contour_polygon_xld (EdgeSecond, [RowEdgeSecond[i] - sin(rad(90) - angle)*length2,RowEdgeSecond[i] + sin(rad(90) - angle)*length2], [ColumnEdgeFirst[i] - cos(rad(90) - angle)*length2,ColumnEdgeFirst[i] + cos(rad(90) - angle)*length2])
    dev_set_color ('cyan')
    dev_display (EdgeFirst)
    dev_set_color ('red')
    dev_display (EdgeSecond)
    dev_set_color ('blue')
    *这是设置文本在那里显示
    if (i = 0)
        set_tposition (WindowHandle, RowEdgeFirst[i] + 5, ColumnEdgeFirst[i]+20)
    else
        set_tposition (WindowHandle, RowEdgeFirst[i] - 40, ColumnEdgeFirst[i]+20)
    endif
    *从文本显示的地方写入字符串,写出像素
    write_string (WindowHandle, 'width: '+IntraDistance[i]+' pix')
endfor
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* 销毁
* 
close_measure (MeasureHandle)
dev_update_window ('on')
dev_clear_window ()
 

FIG final results are as follows:

 

*测量刻度尺
 
dev_close_window()
 
read_image (Image, 'D:/picture/20131001152907.jpeg')
decompose3 (Image, Red, Green, Blue)
rgb3_to_gray (Red, Green, Blue, ImageGray)
get_image_size (Image, Width, Height)
 
 
dev_close_window()
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_set_draw ('margin')
dev_display (ImageGray)
 
Row := 169
Column := 636
*这里为什么要旋转-90度,自己想想去
Phi := rad(-90)
Length1 := 105
Length2 := 3
 
 
gen_rectangle2 (Rectangle, Row, Column, Phi, Length1, Length2)
gen_measure_rectangle2 (Row, Column, Phi, Length1, Length2, Width, Height, 'bicubic', MeasureHandle)
 
Sigma := 1.0
Threshold := 10
*旋转了-90度,表示顺时针旋转了90度,也就是表示从上至下,从像素值高到像素值低的放在RowEdgeFirst中
Transition := 'negative'
Select := 'all'
measure_pairs (ImageGray, MeasureHandle, 1, 30, Transition, Select, RowEdgeFirst, ColumnEdgeFirst, AmplitudeFirst, RowEdgeSecond, ColumnEdgeSecond, AmplitudeSecond, IntraDistance, InterDistance)
 
disp_line (WindowHandle, RowEdgeFirst , ColumnEdgeFirst- Length2, RowEdgeFirst, ColumnEdgeSecond + Length2)
 
avgLeadWidth := sum(IntraDistance)/|IntraDistance|
avgLeadDistance := sum(InterDistance)/|InterDistance|
numLeads := |IntraDistance|
 
disp_message (WindowHandle, '刻度个数: '+numLeads, 'window', 100, 200, 'black', 'false')
disp_message (WindowHandle, '平均宽度:  '+avgLeadWidth$'.2f', 'window',130, 200, 'black', 'false')
disp_message (WindowHandle, '平均距离:  '+avgLeadDistance$'.2f', 'window',160, 200, 'black', 'false')
stop()
 
*将之部分放大,首先定义一个区域
Row1 := 100
Column1 := 580
Row2 := 200
Column2 := 680
dev_set_color ('blue')
disp_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2)
disp_continue_message (WindowHandle, 'black', 'true')
stop()
 
*将这个区域放大,然后显示
dev_set_part (Row1, Column1, Row2, Column2)
dev_display (Image)
dev_set_color ('black')
dev_display (Rectangle)
*画出每一个边缘
NumRows := |RowEdgeFirst|
NumCols := |ColumnEdgeFirst|
Num := min([NumRows, NumCols])
 
for i:= 0 to Num - 1 by 1
    *先得到每一条边缘的中心点
    RowCoorFirst := RowEdgeFirst[i]
    ColCoorFirst := ColumnEdgeFirst[i]
    
    RowCoorSecond := RowEdgeSecond[i]
    ColCoorSecond := ColumnEdgeSecond[i]
    *得到边缘的开始点与结束点
    RowStartFirst := RowCoorFirst - sin(rad(90) - Phi)*Length2
    RowEndFirst := RowCoorFirst + sin(rad(90) - Phi)*Length2
    ColStartFirst := ColCoorFirst - cos(rad(90) - Phi)*Length2
    ColEndFirst := ColCoorFirst + cos(rad(90) - Phi)*Length2
    
    RowStartSecond := RowCoorSecond - sin(rad(90) - Phi)*Length2
    RowEndSecond := RowCoorSecond + sin(rad(90) - Phi)*Length2
    ColStartSecond := ColCoorSecond - cos(rad(90) - Phi)*Length2
    ColEndSecond := ColCoorSecond + cos(rad(90) - Phi)*Length2
    *画出边缘线
    gen_contour_polygon_xld(EdgeFirst,[RowStartFirst,RowEndFirst],[ColStartFirst,ColEndFirst])
    gen_contour_polygon_xld(EdgeSecond,[RowStartSecond,RowEndSecond],[ColStartSecond,ColEndSecond])
    dev_set_color ('red')
    dev_set_line_width (2)
    dev_display (EdgeFirst)
endfor
close_measure (MeasureHandle)
*重置参数
dev_set_part (0, 0, Height-1, Width-1)
dev_set_draw ('fill')
dev_set_line_width (1)

The results are as follows:

 

 

* Measure 01: Code generated by Measure 01
* Measure 01: Prepare measurement
AmplitudeThreshold := 20
RoiWidthLen2 := 5.5
set_system ('int_zooming', 'true')
* Measure 01: Coordinates for line Measure 01 [0]
LineRowStart_Measure_01_0 := 350.406
LineColumnStart_Measure_01_0 := 349.582
LineRowEnd_Measure_01_0 := 436.109
LineColumnEnd_Measure_01_0 := 400.394
* Measure 01: Convert coordinates to rectangle2 type
TmpCtrl_Row := 0.5*(LineRowStart_Measure_01_0+LineRowEnd_Measure_01_0)
TmpCtrl_Column := 0.5*(LineColumnStart_Measure_01_0+LineColumnEnd_Measure_01_0)
TmpCtrl_Dr := LineRowStart_Measure_01_0-LineRowEnd_Measure_01_0
TmpCtrl_Dc := LineColumnEnd_Measure_01_0-LineColumnStart_Measure_01_0
TmpCtrl_Phi := atan2(TmpCtrl_Dr, TmpCtrl_Dc)
TmpCtrl_Len1 := 0.5*sqrt(TmpCtrl_Dr*TmpCtrl_Dr + TmpCtrl_Dc*TmpCtrl_Dc)
TmpCtrl_Len2 := RoiWidthLen2
* Measure 01: Create measure for line Measure 01 [0]
* Measure 01: Attention: This assumes all images have the same size!
gen_measure_rectangle2 (TmpCtrl_Row, TmpCtrl_Column, TmpCtrl_Phi, TmpCtrl_Len1, TmpCtrl_Len2, 640, 480, 'nearest_neighbor', MsrHandle_Measure_01_0)
* Measure 01: ***************************************************************
* Measure 01: * The code which follows is to be executed once / measurement *
* Measure 01: ***************************************************************
* Measure 01: The image is assumed to be made available in the
* Measure 01: variable last displayed in the graphics window
copy_obj (Image, Image, 1, 1)
* Measure 01: Execute measurements
measure_pairs (Image, MsrHandle_Measure_01_0, 2, AmplitudeThreshold, 'all', 'all', Row1_Measure_01_0, Column1_Measure_01_0, Amplitude1_Measure_01_0, Row2_Measure_01_0, Column2_Measure_01_0, Amplitude2_Measure_01_0, Width_Measure_01_0, Distance_Measure_01_0)
* Measure 01: Do something with the results
* Measure 01: Clear measure when done
close_measure (MsrHandle_Measure_01_0)

--------------------- 
Author: Fred_Yang2013 
Source: CSDN 
Original: https: //blog.csdn.net/fred_yang2013/article/details/12205925 
Disclaimer: This article as a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin blog.csdn.net/qingzhuyuxian/article/details/93487023