必威体育Betway必威体育官网
当前位置:首页 > IT技术

Halcon---------align_measurements.hdev学习总结

时间:2019-10-03 05:41:06来源:IT技术作者:seo实验室小编阅读:71次「手机版」
 

measurements

* ------------------------------------------------------------------------------------------------
* This example program uses shape-based matching to align ROIs for the measure
* tool, which then inspects inpidual razor blades.
* The program can be run in two modes: 
* (1) with the full affine transformation
* (2) using translate_measure
* Modify the next line to switch between the modes.


USING_TRANSLATE_MEASURE := 0       //后面用到,但一直看不懂这是干啥的?????????????

* general configuration of HDevelop
dev_update_window ('off')
* image acquisition and window size
read_image (ModelImage, 'razors1')
get_image_pointer1 (ModelImage, Pointer, Type, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'white', Windowhandle)
dev_set_part (0, 0, Height - 1, Width - 1)
dev_display (ModelImage)
* colors and other settings for the visualization
dev_set_color ('cyan')
dev_set_draw ('margin')
dev_set_line_width (2)
stop ()
* -------------------  start of the APPlication  ----------------
* -> select the model object    //这一步在模板匹配前用XLD框出要用的模板
Row1 := 46
Column1 := 57
Row2 := 79
Column2 := 94
gen_rectangle1 (ROIPart1, Row1, Column1, Row2, Column2)
gen_rectangle1 (ROIPart2, Row1 + 364, Column1 + 13, Row2 + 364, Column2 + 13)
union2 (ROIPart1, ROIPart2, ModelROI)
area_center (ModelROI, Area, CenterROIRow, CenterROIColumn)
dev_display (ModelImage)
dev_display (ModelROI)
stop ()
* -> create the model
reduce_domain (ModelImage, ModelROI, ImageROI)//缩小图像定义域到感兴趣的区域
create_shape_model (ImageROI, 4, 0, 0, 'auto', 'none', 'use_polarity', 30, 10, ModelID)
//创建模板,金字塔的级数Numlevels值越大则找到物体的时间越少,AngleStart和AngleExtent决定可能的旋转范围,AngleStep指定角度范围搜索的步长;
inspect_shape_model (ImageROI, ShapeModelImage, ShapeModelRegion, 1, 30)
//检查参数,这一步返回一个原始模板图片和模板的特征轮廓线
get_shape_model_contours (ShapeModel, ModelID, 1)
//1.返回了一个清晰的重新生成的特征轮廓线
//2.坐标(0,0)
//3.是XLD轮廓
dev_clear_window ()
dev_set_color ('blue')
dev_display (ShapeModelRegion)
stop ()
* step 1: create variables describing  the measurement ROIs and display them
Rect1Row := 244
Rect1Col := 73
DistColRect1Rect2 := 17
Rect2Row := Rect1Row
Rect2Col := Rect1Col + DistColRect1Rect2
RectPhi := rad(90)
RectLength1 := 122
RectLength2 := 2
gen_rectangle2 (MeasureROI1, Rect1Row, Rect1Col, 0, RectLength2, RectLength1)
gen_rectangle2 (MeasureROI2, Rect2Row, Rect2Col, RectPhi, RectLength1, RectLength2)
//生成两条竖着的矩阵轮廓(region)
dev_display (ModelImage)
dev_set_color ('yellow')
dev_display (MeasureROI1)
dev_display (MeasureROI2)
* translate measurement ROIs to lie on XLD model (without clipping!)

get_system ('clip_region', OriginalClipRegion)
set_system ('clip_region', 'false')
*在移动区域之前,剪切必须关闭。

move_region (MeasureROI1, MeasureROI1Ref, -CenterROIRow, -CenterROIColumn)
move_region (MeasureROI2, MeasureROI2Ref, -CenterROIRow, -CenterROIColumn)
*给挪左上角去了,挪完了变成新的区域,MeasureROI1Ref,MeasureROI2Ref
//后面用模板中心坐标移动,所以现在把长条的框也用模板中心坐标移动
set_system ('clip_region', OriginalClipRegion)
DistRect1CenterRow := Rect1Row - CenterROIRow  //长条框中心点坐标
DistRect1CenterCol := Rect1Col - CenterROIColumn
DistRect2CenterRow := Rect2Row - CenterROIRow
DistRect2CenterCol := Rect2Col - CenterROIColumn
stop ()

if (USING_TRANSLATE_MEASURE != 0)
    * -> measure objects are created only once in advance and then translated later
    gen_measure_rectangle2 (Rect1Row, Rect1Col, RectPhi, RectLength1, RectLength2, Width, Height, 'bilinear', MeasureHandle1)
    gen_measure_rectangle2 (Rect2Row, Rect2Col, RectPhi, RectLength1, RectLength2, Width, Height, 'bilinear', MeasureHandle2)
endif
stop ()


* step 2: find the objects in another image
read_image (SearchImage, 'razors2')
dev_display (SearchImage)
find_shape_model (SearchImage, ModelID, 0, 0, 0.8, 0, 0.5, 'least_squares', 0, 0.7, RowCheck, ColumnCheck, AngleCheck, Score)
if (|Score| > 0)
    for i := 0 to |Score| - 1 by 1
        //     |xxxxx|数组长度!!!!
        * step 3: determine the affine transformation
        vector_angle_to_rigid (0, 0, 0, RowCheck[i], ColumnCheck[i], AngleCheck[i], MovementOfObject) 
//变换矩阵,计算点和角度到另一个点和角度的仿射矩阵,根据矩阵计算点与点之间的变换关系。
        affine_trans_contour_xld (ShapeModel, ModelAtNewPosition, MovementOfObject)
//对XLD进行仿射变换
        dev_display (ModelAtNewPosition)
    endfor
        stop()

* step 4: measure width and distance of the teeth
        * -> display the moved ROIs
        affine_trans_region (MeasureROI1Ref, MeasureROI1AtNewPosition, MovementOfObject, 'constant')//对区域(region)进行仿射变换,变换关系依然是模板中心点对零点
        affine_trans_region (MeasureROI2Ref, MeasureROI2AtNewPosition, MovementOfObject, 'constant')
        dev_display (MeasureROI1AtNewPosition)
        dev_display (MeasureROI2AtNewPosition)
        affine_trans_pixel (MovementOfObject, DistRect1CenterRow, DistRect1CenterCol, Rect1RowCheck, Rect1ColCheck)
        *//对像素坐标应用任意仿射二维转换(把中心点从左上角原点移到测量区域中心点)
        affine_trans_pixel (MovementOfObject, DistRect2CenterRow, DistRect2CenterCol, Rect2RowCheck, Rect2ColCheck)
        if (USING_TRANSLATE_MEASURE != 0)
            * -> translate the already created measure objects
            translate_measure (MeasureHandle1, Rect1RowCheck, Rect1ColCheck)
            translate_measure (MeasureHandle2, Rect2RowCheck, Rect2ColCheck)
            measure_pairs (SearchImage, MeasureHandle1, 2, 25, 'negative', 'all', RowEdge11, ColEdge11, Amp11, RowEdge21, ColEdge21, Amp21, Width1, Distance1)
            measure_pairs (SearchImage, MeasureHandle2, 2, 25, 'negative', 'all', RowEdge12, ColEdge12, Amp12, RowEdge22, ColEdge22, Amp22, Width2, Distance2)


        else
            * -> create new measure objects and destroy them after the measurement
            RectPhiCheck := RectPhi + AngleCheck[i]
            gen_measure_rectangle2 (Rect1RowCheck, Rect1ColCheck, RectPhiCheck, RectLength1, RectLength2, Width, Height, 'bilinear', MeasureHandle1)
//
            gen_measure_rectangle2 (Rect2RowCheck, Rect2ColCheck, RectPhiCheck, RectLength1, RectLength2, Width, Height, 'bilinear', MeasureHandle2)
            * step 5: perform the measurement
            measure_pairs (SearchImage, MeasureHandle1, 2, 25, 'negative', 'all', RowEdge11, ColEdge11, Amp11, RowEdge21, ColEdge21, Amp21, Width1, Distance1)
            measure_pairs (SearchImage, MeasureHandle2, 2, 25, 'negative', 'all', RowEdge12, ColEdge12, Amp12, RowEdge22, ColEdge22, Amp22, Width2, Distance2)
            close_measure (MeasureHandle1)
            close_measure (MeasureHandle2)

             for q:=0 to |Width1|-1by 1     //自己添加的显示边缘对
                row1uL:=RowEdge11[q]
                col1uL:=ColEdge11[q]-5
                row1ur:=RowEdge11[q]
                col1ur:=ColEdge11[q]+5
                
                row1dL:=RowEdge21[q]
                col1dL:=ColEdge21[q]-5
                row1dr:=RowEdge21[q]
                col1dr:=ColEdge21[q]+5
        
                gen_contour_polygon_xld (Contour1u, [row1uL,row1ur], [col1uL,col1ur])
                gen_contour_polygon_xld (Contour1d, [row1dL,row1dr], [col1dL,col1dr])
                dev_set_color ('green')
                dev_display(Contour1u)
                dev_display(Contour1d)    
            endfor
            for s:=|Width2|-1 to 0 by -1
                row2uL:=RowEdge12[s]
                col2uL:=ColEdge12[s]-5
                row2ur:=RowEdge12[s]
                col2ur:=ColEdge12[s]+5
                
                row2dL:=RowEdge22[s]
                col2dL:=ColEdge22[s]-5
                row2dr:=RowEdge22[s]
                col2dr:=ColEdge22[s]+5
        
                gen_contour_polygon_xld (Contour2u, [row2uL,row2ur], [col2uL,col2ur])
                gen_contour_polygon_xld (Contour2d, [row2dL,row2dr], [col2dL,col2dr])
                dev_set_color ('green')
                dev_display(Contour2u)
                dev_display(Contour2d)
            endfor
        
        endif

 * step 6: check for too short or missing teeth
        NumberTeeth1 := |Width1|
        NumberTeeth2 := |Width2|
        dev_set_color ('red')
        if (NumberTeeth1 < 37)
            for j := 0 to NumberTeeth1-2  by 1   //width比distance多1,又是从0计数
                if (Distance1[j] > 4.0)
                    RowFault := round(0.5 * (RowEdge11[j + 1] + RowEdge21[j]))    //j从下往上计数,11是下面的线,21是上面的线
                    ColFault := round(0.5 * (ColEdge11[j + 1] + ColEdge21[j]))
                    disp_rectangle2 (WindowHandle, RowEdge11[j]-8, ColEdge11[j], 0, 4, 4)//这边改了下手动设置坐标也行应该
                    dev_open_window (0,  Width +20, 80, 80, 'black', WindowHandleZoom)//前两个参数是窗体出现位置坐标
                    dev_set_part (RowEdge11[j]-8 - 10, ColEdge11[j] - 10, RowEdge11[j]-8 + 10, ColEdge11[j] + 10)//显示区域,根据窗体大小自动缩放
                    dev_display (SearchImage)
                    disp_rectangle2 (WindowHandleZoom, RowEdge11[j]-8,  ColEdge11[j], 0, 4, 4)
                    stop ()
                    dev_close_window ()
                    dev_set_part (0, 0, Height - 1, Width - 1)
                endif
            endfor
        endif
        if (NumberTeeth2 < 37)
            for j := 0 to NumberTeeth2 - 2 by 1
                if (Distance2[j] > 4.0)
                    RowFault := round(0.5 * (RowEdge12[j + 1] + RowEdge22[j]))
                    ColFault := round(0.5 * (ColEdge12[j + 1] + ColEdge22[j]))
                    disp_rectangle2 (WindowHandle, RowFault, ColFault, 0, 4, 4)
                    dev_open_window (0, Width + 20, 80, 80, 'black', WindowHandleZoom)
                    dev_set_part (RowFault - 10, ColFault - 10, RowFault + 10, ColFault + 10)
                    dev_display (SearchImage)
                    disp_rectangle2 (WindowHandleZoom, RowFault, ColFault, 0, 4, 4)
                    stop ()
                    dev_close_window ()
                    dev_set_part (0, 0, Height - 1, Width - 1)
                endif
            endfor
        endif
        dev_set_color ('yellow')
        stop ()
    endfor
endif
if (USING_TRANSLATE_MEASURE != 0)
    close_measure (MeasureHandle1)
    close_measure (MeasureHandle2)
endif
dev_update_window ('on')
clear_shape_model (ModelID)

目前还看不懂    USING_TRANSLATE_MEASURE   的用处。。。

总结一下,感觉这个示例就是讲模板匹配,比起VisionPro来说麻烦不少,主要麻烦在了这些 ‘辅助线’ 上

先生成个框,把想要进行模板匹配的部分圈起来进行reduce_domain

然后create_shape_model、find_shape_model,中间为了后续坐标点,显示等用了很多其他算子

通过模板匹配生成的坐标与寻找边缘对的区域框的坐标捆绑

再用gen_measure_rectangle2、 measure_pairs寻找边缘对,边缘对之间距离的数量就是齿数

剩下的就是运用一些逻辑找到缺失的齿

相关阅读

STM32学习笔记一一UCOSII(1)

1.简介 UCOSII 是一个可以基于 ROM 运行的、可裁减的、抢占式、实时多任务内核,具有高度可移植性,特别适合于微处理器和控制器,是和

Stateflow学习笔记之状态图的内部转移(inner transiti

  处理内部转移的事件 考虑下图所示的状态图。 状态A有一个内部转移。转移动作为A_two。 状态B有一个指向自身的外部转移(自循

机器学习 - 无监督学习-多元高斯模型

(一)异常检测---基于高斯(正态)分布 m个训练样本,每个样本有n个features 即m个样本的每个属性集都呈现高斯分布,因此有以下计算: 例

学习笔记--霍夫曼树与霍夫曼编码解码

先摘一下百科的说法“哈夫曼编码(Huffman Coding),又称霍夫曼编码,是一种编码方式,哈夫曼编码是可变字长编码(VLC)的一种。Huffman于

Oracle DBA学习基础篇(一) Oracle体系结构 学习笔记

这里是我通过Oracle DBA实战这本书学习的 Oracle体系结构,大部分是我对书中的内容做的总结,还有遇到不理解的知识点,通过查询资料做

分享到:

栏目导航

推荐阅读

热门阅读