Halcon 缺陷检测

文章目录

闭运算,腐蚀膨胀的亮点问题

开运算先进行腐蚀操作再进行膨胀操作,通常可以消除小的亮点(暗点变多)区域并使边缘更加清晰,因此亮点通常会减少。而闭运算先进行膨胀操作再进行腐蚀操作,可以填补亮点中间的空洞并使其更加连通,因此亮点通常会增多(暗点减少)。
腐蚀和膨胀也是形态学处理中常用的基本操作,它们分别可以缩小或消除亮点区域(腐蚀),或扩大或连接亮点区域(膨胀)。在进行开运算时,腐蚀操作会消除较小的亮点区域,膨胀操作会尝试连接亮点并填充它们之间的空隙;在进行闭运算时,膨胀操作会连接较小的亮点区域并填充它们之间的空隙,腐蚀操作会消除边缘部分使得亮点更加连通和光滑。

灰度图像的开闭运算,腐蚀膨胀的亮点问题

对于灰度图像进行开闭运算时,腐蚀和膨胀操作的影响与二值图像略有不同。在灰度图像中,腐蚀和膨胀操作会改变亮度值而不仅仅是亮点的数量。

在开运算中,先进行腐蚀操作再进行膨胀操作。腐蚀操作会使亮区域的亮度值降低,从而减小亮点的尺寸和强度(暗点变多)。然后,膨胀操作会试图恢复亮区域的亮度值,但由于先前的腐蚀操作的影响,亮点可能不会完全恢复到原始的尺寸和强度。因此,在灰度图像中进行开运算可能会导致亮点数量减少,并且亮点的尺寸和强度也可能减小。

相反,在闭运算中,先进行膨胀操作再进行腐蚀操作。膨胀操作会使亮区域的亮度值增加,从而增大亮点的尺寸和强度(暗点减少)。然后,腐蚀操作会试图消除边缘部分并使亮点更加连通和光滑。因此,在灰度图像中进行闭运算通常会导致亮点数量增多,并且亮点的尺寸和强度也可能增加。

算子

二值化算子

Halcon blob+特征处理的方法检测缺陷

在这里插入图片描述

* This example demonstrates a quality inspection on hazelnut wavers.
* Using the morphology tools the waver is extracted and examined
* according to a few shape features like Rectangularity and AreaHoles.
* This program also shows the use of the operator area_holes.
* 
read_image (Image, 'food/hazelnut_wafer_01')
dev_close_window ()
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
dev_update_window ('off')
dev_set_line_width (3)
dev_set_draw ('margin')
set_display_font (WindowHandle, 20, 'mono', 'true', 'false')
* 
for Index := 1 to 24 by 1
    read_image (Image, 'food/hazelnut_wafer_' + Index$'.02')
    binary_threshold (Image, Foreground, 'smooth_histo', 'light', UsedThreshold)
    opening_circle (Foreground, FinalRegion, 8.5)
    * 求空洞面积
    area_holes (FinalRegion, AreaHoles)
    * 求矩形度量
    rectangularity (FinalRegion, Rectangularity)
    dev_display (Image)
    * 如果空洞面积>300 并且矩形度<0.92 不合格
    if (AreaHoles > 300 or Rectangularity < 0.92)
        dev_set_color ('red')
        Text := 'Not OK'
    else
        dev_set_color ('forest green')
        Text := 'OK'
    endif
    dev_display (FinalRegion)
    disp_message (WindowHandle, Text, 'window', 12, 12, '', 'false')
    if (Index < 24)
        disp_continue_message (WindowHandle, 'black', 'true')
        stop ()
    endif
endfor

 
 

    在这里插入图片描述

    Halcon Blob+特征+差分的方法检测缺陷

    闭运算是先进行膨胀操作,再进行腐蚀操作,通常用于填充图像中的小孔洞或连接断开的区域。闭运算可以平滑和闭合图像中的对象边界。
    开运算是先进行腐蚀操作,再进行膨胀操作,通常用于去除图像中的小尖峰或细小的对象。开运算可以平滑和消除图像中的噪点。

    在这里插入图片描述

    * fin.hdev: Detection of a fin
    * 
    *  采集
    dev_update_window ('off')
    read_image (Fins, 'fin' + [1:3])
    get_image_size (Fins, Width, Height)
    dev_close_window ()
    dev_open_window (0, 0, Width[0], Height[0], 'black', WindowID)
    set_display_font (WindowID, 14, 'mono', 'true', 'false')
    for I := 1 to 3 by 1
        select_obj (Fins, Fin, I)
        dev_display (Fin)
        * 分割
        *二值化
        binary_threshold (Fin, Background, 'max_separability', 'light', UsedThreshold)
        dev_set_color ('blue')
        dev_set_draw ('margin')
        dev_set_line_width (4)
        dev_display (Background)
        disp_continue_message (WindowID, 'black', 'true')
        stop ()
    
    <span class="token operator">*</span> 使用闭运算把圆形填充,先膨胀在腐蚀
    <span class="token function">closing_circle</span> <span class="token punctuation">(</span>Background<span class="token punctuation">,</span> ClosedBackground<span class="token punctuation">,</span> <span class="token number">250</span><span class="token punctuation">)</span>
    <span class="token function">dev_set_color</span> <span class="token punctuation">(</span><span class="token char">'green'</span><span class="token punctuation">)</span>
    <span class="token function">dev_display</span> <span class="token punctuation">(</span>ClosedBackground<span class="token punctuation">)</span>
    <span class="token function">disp_continue_message</span> <span class="token punctuation">(</span>WindowID<span class="token punctuation">,</span> <span class="token char">'black'</span><span class="token punctuation">,</span> <span class="token char">'true'</span><span class="token punctuation">)</span>
    <span class="token function">stop</span> <span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token operator">*</span> 将闭运算得到的图像和原图像做差
    <span class="token function">difference</span> <span class="token punctuation">(</span>ClosedBackground<span class="token punctuation">,</span> Background<span class="token punctuation">,</span> RegionDifference<span class="token punctuation">)</span>
    <span class="token operator">*</span> 差值区域做开运算去除噪点,先腐蚀在鹏展
    <span class="token function">opening_rectangle1</span> <span class="token punctuation">(</span>RegionDifference<span class="token punctuation">,</span> FinRegion<span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span>
    <span class="token function">dev_display</span> <span class="token punctuation">(</span>Fin<span class="token punctuation">)</span>
    <span class="token function">dev_set_color</span> <span class="token punctuation">(</span><span class="token char">'red'</span><span class="token punctuation">)</span>
    <span class="token function">dev_display</span> <span class="token punctuation">(</span>FinRegion<span class="token punctuation">)</span>
    <span class="token operator">*</span> 特征提取
    <span class="token operator">*</span> 获取中心区域的面积,和行列坐标
    <span class="token function">area_center</span> <span class="token punctuation">(</span>FinRegion<span class="token punctuation">,</span> FinArea<span class="token punctuation">,</span> Row<span class="token punctuation">,</span> Column<span class="token punctuation">)</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>I <span class="token operator">&lt;</span> <span class="token number">3</span><span class="token punctuation">)</span>
        <span class="token function">disp_continue_message</span> <span class="token punctuation">(</span>WindowID<span class="token punctuation">,</span> <span class="token char">'black'</span><span class="token punctuation">,</span> <span class="token char">'true'</span><span class="token punctuation">)</span>
        <span class="token function">stop</span> <span class="token punctuation">(</span><span class="token punctuation">)</span>
    endif
    

    endfor

      在这里插入图片描述

      Halcon 极坐标变换(环形先转换坐标)+blob+特征

      Halcon 圆环上orc字体识别
      在这里插入图片描述

      * This example checks bottle necks for defects.
      * First, the bottle is detected with basic morphology,
      * edge detection and circle fitting.
      * Then, the neck area is transformed with a polar transformation.
      * After that, in the transformed image a dynamic threshold is used
      * to detect defects. Finally, the results are displayed.
      * 
      * 
      * tuning parameters
      SmoothX := 501
      ThresholdOffset := 25
      MinDefectSize := 50
      * 
      * initialization
      PolarResolution := 640
      RingSize := 70
      get_system ('store_empty_region', StoreEmptyRegion)
      set_system ('store_empty_region', 'false')
      read_image (Image, 'bottles/bottle_mouth_01')
      dev_update_off ()
      dev_close_window ()
      dev_close_window ()
      dev_open_window_fit_image (Image, 0, 0, 640, 512, WindowHandle1)
      set_display_font (WindowHandle1, 16, 'mono', 'true', 'false')
      dev_display (Image)
      dev_set_draw ('margin')
      dev_set_line_width (3)
      dev_open_window_fit_size (0, 648, RingSize, PolarResolution, 150, 512, WindowHandle)
      dev_set_draw ('margin')
      dev_set_line_width (3)
      dev_set_color ('red')
      * 
      * Main loop
      * 
      * Detect defects in bottle necks
      for Index := 1 to 16 by 1
          read_image (Image, 'bottles/bottle_mouth_' + Index$'.02')
          * 
          * Part 1: Use basic morphology to detect bottle
          auto_threshold (Image, Regions, 2)
          select_obj (Regions, DarkRegion, 1)
          opening_circle (DarkRegion, RegionOpening, 3.5)
          closing_circle (RegionOpening, RegionClosing, 25.5)
          fill_up (RegionClosing, RegionFillUp)
          boundary (RegionFillUp, RegionBorder, 'outer')
          dilation_circle (RegionBorder, RegionDilation, 3.5)
          reduce_domain (Image, RegionDilation, ImageReduced)
          * 
          * Find the bottle center by fitting a circle to extracted edges
          edges_sub_pix (ImageReduced, Edges, 'canny', 0.5, 20, 40)
          segment_contours_xld (Edges, ContoursSplit, 'lines_circles', 5, 4, 2)
          union_cocircular_contours_xld (ContoursSplit, UnionContours, 0.9, 0.5, 0.5, 200, 50, 50, 'true', 1)
          length_xld (UnionContours, Length)
          select_obj (UnionContours, LongestContour, sort_index(Length)[|Length| - 1] + 1)
          fit_circle_contour_xld (LongestContour, 'ahuber', -1, 0, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
          * 
          * Part 2: Transform the ring-shaped bottle neck region to a rectangle
          gen_circle (Circle, Row, Column, Radius)
          dilation_circle (Circle, RegionDilation, 5)
          erosion_circle (Circle, RegionErosion, RingSize - 5)
          difference (RegionDilation, RegionErosion, RegionDifference)
          reduce_domain (Image, RegionDifference, ImageReduced)
          polar_trans_image_ext (ImageReduced, ImagePolar, Row, Column, 0, rad(360), Radius - RingSize, Radius, PolarResolution, RingSize, 'nearest_neighbor')
          * 
          * Part 3: Find defects with a dynamic threshold
          * Note the strong smoothing in x-direction in the transformed image.
          scale_image_max (ImagePolar, ImageScaleMax)
          mean_image (ImageScaleMax, ImageMean, SmoothX, 3)
          dyn_threshold (ImageScaleMax, ImageMean, Regions1, 55, 'not_equal')
          connection (Regions1, Connection)
          select_shape (Connection, SelectedRegions, 'height', 'and', 9, 99999)
          * ignore noise regions
          closing_rectangle1 (SelectedRegions, RegionClosing1, 10, 20)
          union1 (RegionClosing1, RegionUnion)
          * re-transform defect regions for visualization
          polar_trans_region_inv (RegionUnion, XYTransRegion, Row, Column, 0, rad(360), Radius - RingSize, Radius, PolarResolution, RingSize, 1280, 1024, 'nearest_neighbor')
          * 
          * Part 4: Display results
          * display original image with results
          dev_set_window (WindowHandle1)
          dev_display (Image)
          dev_set_color ('blue')
          dev_display (RegionDifference)
          dev_set_color ('red')
          dev_display (XYTransRegion)
          * display polar transformed inspected region with results
          * The image and resulting region are rotated by 90 degrees
          * only for visualization purposes! (I.e. to fit better on the screen)
          * The rotation is NOT necessary for the detection algorithm.
          dev_set_window (WindowHandle)
          rotate_image (ImagePolar, ImageRotate, 90, 'constant')
          dev_display (ImageRotate)
          count_obj (RegionUnion, Number)
          if (Number > 0)
              mirror_region (RegionUnion, RegionMirror, 'diagonal', PolarResolution)
              mirror_region (RegionMirror, RegionMirror, 'row', PolarResolution)
              dev_display (RegionMirror)
              disp_message (WindowHandle1, 'Not OK', 'window', 12, 12, 'red', 'false')
          else
              disp_message (WindowHandle1, 'OK', 'window', 12, 12, 'forest green', 'false')
          endif
          if (Index < 16)
              disp_continue_message (WindowHandle1, 'black', 'true')
              stop ()
          endif
      endfor
      * Reset system parameters
      set_system ('store_empty_region', StoreEmptyRegion)
      
       
       

        在这里插入图片描述

        Halcon Blob+局部二值化+特征提取缺陷检测

        在这里插入图片描述

        在这里插入图片描述

        * The task of this example is to detect defects on a
        * web using the operator dyn_threshold. In this way,
        * the operator can be used to find textures that
        * differ from the rest of the image.
        *1.采集图像
        dev_update_window ('off')
        read_image (Image, 'plastic_mesh/plastic_mesh_01')
        dev_close_window ()
        get_image_size (Image, Width, Height)
        dev_open_window_fit_image (Image, 0, 0, Width, Height, WindowHandle)
        set_display_font (WindowHandle, 18, 'mono', 'true', 'false')
        dev_set_draw ('margin')
        dev_set_line_width (3)
        * Each of the images is read and smoothed. Subsequently
        * dyn_threshold is performed and connected regions are
        * looked for. The parameter 'area' of the operator select_shape
        * makes it possible to find regions that differ in the area
        * size. Found errors are finally counted and displayed.
        for J := 1 to 14 by 1
            *2.图像分割(局部阈值法)
            read_image (Image, 'plastic_mesh/plastic_mesh_' + J$'02')
            mean_image (Image, ImageMean, 49, 49)
            dyn_threshold (Image, ImageMean, RegionDynThresh, 5, 'dark')
            *3.特征提取
            *形成不同的连通域
            connection (RegionDynThresh, ConnectedRegions)
            *选择面积特征如果面积特征大于500 则有缺陷
            select_shape (ConnectedRegions, ErrorRegions, 'area', 'and', 500, 99999)
            count_obj (ErrorRegions, NumErrors)
            dev_display (Image)
            dev_set_color ('red')
            dev_display (ErrorRegions)
            * If the number of errors exceeds zero, the message 'Mesh not
            * OK' is displayed. Otherwise the web is undamaged
            * and 'Mesh OK' is displayed.
            if (NumErrors > 0)
                disp_message (WindowHandle, 'Mesh not OK', 'window', 24, 12, 'black', 'true')
            else
                disp_message (WindowHandle, 'Mesh OK', 'window', 24, 12, 'black', 'true')
            endif
            * If the sequence number of the image to be inspected is
            * lower than 14, the request to press 'Run' to continue appears.
            * If the last image is read, pressing 'Run' will clear the SVM.
            if (J < 14)
                disp_continue_message (WindowHandle, 'black', 'true')
                stop ()
            endif
        endfor
        
         
         

          在这里插入图片描述

          Halcon blob+灰度差分+特征缺陷检测案例

          在这里插入图片描述

          在这里插入图片描述

          read_image (Image, 'pcb')
          dev_close_window ()
          get_image_size (Image, Width, Height)
          dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
          dev_display (Image)
          * detect defects ...
          * 灰度开运算暗点增多
          gray_opening_shape (Image, ImageOpening, 7, 7, 'octagon')
          * 灰度闭运算暗点增多
          gray_closing_shape (Image, ImageClosing, 7, 7, 'octagon')
          * 二值化亮暗点都提取出来,两个图像相减
          dyn_threshold (ImageOpening, ImageClosing, RegionDynThresh, 75, 'not_equal')
          dev_display (Image)
          dev_set_color ('red')
          dev_set_draw ('margin')
          dev_display (RegionDynThresh)
          
           
           

            在这里插入图片描述

            Halcon 定位(blob求角度)+特征

            思路:先读取图片,读取一个没有缺陷的图片,进行图片的仿射运算,获取每一个药片的位置,在读取一个可能有缺陷的图片,进行仿射运算,获取每一个药片的位置,之后进行相交,面积小于0(有缺陷),面积大于0判断灰度值的区间,在区间内有缺陷,在区间外没有缺陷
            在这里插入图片描述

            * This example demonstrates an application from the pharmaceutical
            * industry. The task is to check the content of automatically filled
            * blisters. The first image (reference) is used to locate the chambers
            * within a blister shape as a reference model, which is then used to
            * realign the subsequent images along to this reference shape. Using
            * blob analysis the content of each chamber is segmented and finally
            * classified by a few shape features.
            * 1.采集图像
            dev_close_window ()
            dev_update_off ()
            read_image (ImageOrig, 'blister/blister_reference')
            dev_open_window_fit_image (ImageOrig, 0, 0, -1, -1, WindowHandle)
            set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
            dev_set_draw ('margin')
            dev_set_line_width (3)
            * 
            * In the first step, we create a pattern to cut out the chambers in the
            * subsequent blister images easily.
            * 2.定位求标准位置(blob分析)
            *获取一个bgr通道
            access_channel (ImageOrig, Image1, 1)
            *二值化
            threshold (Image1, Region, 90, 255)
            *凸性转换
            shape_trans (Region, Blister, 'convex')
            *求区域角度
            orientation_region (Blister, Phi)
            *求区域的中心点行列坐标面积
            area_center (Blister, Area1, Row, Column)
            *形成仿射变换矩阵
            vector_angle_to_rigid (Row, Column, Phi, Row, Column, 0, HomMat2D)
            *对图像求仿射变换
            affine_trans_image (ImageOrig, Image2, HomMat2D, 'constant', 'false')
            *产生一个空的物体
            gen_empty_obj (Chambers)
            for I := 0 to 4 by 1
                *行
                Row := 88 + I * 70
                for J := 0 to 2 by 1
                    *列
                    Column := 163 + J * 150
                    *形成一个空的矩形区域
                    gen_rectangle2 (Rectangle, Row, Column, 0, 64, 30)
                    *存储图像
                    concat_obj (Chambers, Rectangle, Chambers)
                endfor
            endfor
            *对区域求仿射变换
            affine_trans_region (Blister, Blister, HomMat2D, 'nearest_neighbor')
            *区域求差(药板和药片求差) 药板-药片=药板的骨架部分
            difference (Blister, Chambers, Pattern)
            *将药片合并
            union1 (Chambers, ChambersUnion)
            *获取药板区域的角度
            orientation_region (Blister, PhiRef)
            PhiRef := rad(180) + PhiRef
            *获取药板区域的面积,行列坐标
            area_center (Blister, Area2, RowRef, ColumnRef)
            * 
            * 
            * Each image read will be aligned to this pattern and reduced to the area of interest,
            * which is the chambers of the blister
            * 3.缺陷检测,在标准的位置抠药片和原来的区域数组求交集来判断
            Count := 6
            for Index := 1 to Count by 1
                read_image (Image, 'blister/blister_' + Index$'02')
                threshold (Image, Region, 90, 255)
                connection (Region, ConnectedRegions)
                select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 5000, 9999999)
                shape_trans (SelectedRegions, RegionTrans, 'convex')
                * 
                * Align pattern along blister of image
                * 仿射变换将位置摆正
                orientation_region (RegionTrans, Phi)
                area_center (RegionTrans, Area3, Row, Column)
                vector_angle_to_rigid (Row, Column, Phi, RowRef, ColumnRef, PhiRef, HomMat2D)
                affine_trans_image (Image, ImageAffineTrans, HomMat2D, 'constant', 'false')
                * 
                * Segment pills
                * 将仿射变换的区域和药片数组的区域裁剪出来
                reduce_domain (ImageAffineTrans, ChambersUnion, ImageReduced)
                * 获取三色通道
                decompose3 (ImageReduced, ImageR, ImageG, ImageB)
                * 均值和方差二值化
                var_threshold (ImageB, Region, 7, 7, 0.2, 2, 'dark')
                * 连接单个区域
                connection (Region, ConnectedRegions0)
                *闭运算
                closing_rectangle1 (ConnectedRegions0, ConnectedRegions, 3, 3)
                *填充
                fill_up (ConnectedRegions, RegionFillUp)
                *特征提取选择区域面积
                select_shape (RegionFillUp, SelectedRegions, 'area', 'and', 1000, 99999)
                *开运算
                opening_circle (SelectedRegions, RegionOpening, 4.5)
                * 将开运算的区域精选连接
                connection (RegionOpening, ConnectedRegions)
                *特征提取筛选出面积
                select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 1000, 99999)
                *将筛选出的特征的图像进行凸性处理
                shape_trans (SelectedRegions, Pills, 'convex')
                * 
                * Classify segmentation results and display statistics
                *获取药板中的药片数量
                count_obj (Chambers, Number)
                *产生一个药片缺陷的容器
                gen_empty_obj (WrongPill)
                *产生一个没有药片的容器
                gen_empty_obj (MissingPill)
                for I := 1 to Number by 1
                    *选择标准药片
                    select_obj (Chambers, Chamber, I)
                    *标准药片和读取的药片求交集
                    intersection (Chamber, Pills, Pill)
                    *获取相交药片的行列坐标,面积
                    area_center (Pill, Area, Row1, Column1)
                    *如果面积大于0
                    if (Area > 0)
                        *求最大最小灰度值
                        min_max_gray (Pill, ImageB, 0, Min, Max, Range)
                        *如果灰度值小于3800大于60
                        if (Area < 3800 or Min < 60)
                            *错误的药片
                            concat_obj (WrongPill, Pill, WrongPill)
                        endif
                    else
                        *如果面积小于0 没有药片
                        concat_obj (MissingPill, Chamber, MissingPill)
                    endif
                endfor
                *  显示
                dev_clear_window ()
                dev_display (ImageAffineTrans)
                dev_set_color ('forest green')
                count_obj (Pills, NumberP)
                count_obj (WrongPill, NumberWP)
                count_obj (MissingPill, NumberMP)
                dev_display (Pills)
                if (NumberMP > 0 or NumberWP > 0)
                    disp_message (WindowHandle, 'Not OK', 'window', 12, 12 + 600, 'red', 'true')
                else
                    disp_message (WindowHandle, 'OK', 'window', 12, 12 + 600, 'forest green', 'true')
                endif
                * 
                Message := '# Correct pills: ' + (NumberP - NumberWP)
                Message[1] := '# Wrong pills  :  ' + NumberWP
                Message[2] := '# Missing pills:  ' + NumberMP
                * 
                Colors := gen_tuple_const(3,'black')
                if (NumberWP > 0)
                    Colors[1] := 'red'
                endif
                if (NumberMP > 0)
                    Colors[2] := 'red'
                endif
                disp_message (WindowHandle, Message, 'window', 12, 12, Colors, 'true')
                dev_set_color ('red')
                dev_display (WrongPill)
                dev_display (MissingPill)
                if (Index < Count)
                    disp_continue_message (WindowHandle, 'black', 'true')
                endif
                stop ()
            endfor
            
             
             

              在这里插入图片描述

              Halcon 定位(模板匹配)+测量

              在这里插入图片描述

              * In this example a fill level check for the
              * pharmaceutical industry is demonstrated. The task is
              * to check for the fill level of each single nose drop ampoule.
              * To do so, we first locate each ampoule head by applying
              * shape-based matching, then we find the fill level
              * by measuring the gray level change using a 1D Measuring.
              * 
              * 1.采集图像
              dev_close_window ()
              dev_update_off ()
              read_image (Image, 'ampoules/ampoules_01')
              get_image_size (Image, Width, Height)
              dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
              dev_set_line_width (2)
              dev_set_draw ('margin')
              set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
              * 2.定位找到参考线
              * Create a model for the ampoule head to align the measure handle
              *产生一个矩形
              gen_rectangle1 (Rectangle, 230, 280, 317, 330)
              * 矩形裁剪
              reduce_domain (Image, Rectangle, ImageModel)
              * 创建模板
              create_shape_model (ImageModel, 'auto', 0, 0, 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID)
              * 
              * Initialize the measure handle
              * 初始化测量矩形
              gen_measure_rectangle2 (0, 0, rad(90), 75, 20, Width, Height, 'bilinear', MeasureHandle)
              Tolerance := 15
              * 
              * Determine the fill level
              NumImages := 8
              for Index := 1 to NumImages by 1
                  * 读取图片
                  read_image (Image, 'ampoules/ampoules_' + Index$'.2d')
                  ColumnEdges := []
                  FillLevelHeight := []
                  * 查找模板
                  find_shape_model (Image, ModelID, 0, 0, 0.7, 0, 0.1, 'least_squares', 0, 0.9, Row, Column, Angle, Score)
                  * 平均行
                  MeanRow := mean(Row)
                  * 参考线 =平均水平坐标-160
                  RefLevel := MeanRow - 160
                  * Display tolerance area
                  dev_display (Image)
                  dev_set_line_width (1)
                  dev_set_color ('white')
                  * 产生一个参考矩形,参考矩形的中间的线为参考线
                  gen_rectangle2 (AcceptLevel, RefLevel, mean(Column), 0, 30 + (max(Column) - min(Column)) / 2, Tolerance)
                  * 绘制矩形
                  dev_display (AcceptLevel)
                  dev_set_line_width (2)
                  *  3.形成测量矩形找液面边缘
                  *  Determine fill level of each ampoule
                  Errors := 0
                  for Idx := 0 to |Score| - 1 by 1
                      * 移动到测量点,行=参考的基准-135,列模板匹配到的列
                      translate_measure (MeasureHandle, MeanRow - 135, Column[Idx])
                      * Search for the topmost edge
                      * 测量边
                      measure_pos (Image, MeasureHandle, 2, 7, 'all', 'first', RowEdge, ColumnEdge, Amplitude, Distance)
                      FillLevelHeight := [FillLevelHeight,RowEdge]
                      ColumnEdges := [ColumnEdges,ColumnEdge]
                      * 生成亚像素轮廓
                      gen_cross_contour_xld (Cross, RowEdge, ColumnEdge, 15, 0)
                      * 绘制矩形
                      gen_rectangle2 (FillLevel, RowEdge, ColumnEdge, 0, 28, 20)
                      * 4.计算参考线和边缘之间的距离
                      * 判断是否合格,水平高度 - 矩形的高度 >= 15 不合格
                      if (abs(FillLevelHeight[Idx] - RefLevel) >= Tolerance)
                          * 产生一个矩形
                          gen_rectangle2 (ChamberSingle, MeanRow - 133, Column[Idx], 0, 35, 90)
                          * 产生十字
                          gen_cross_contour_xld (Cross, FillLevelHeight[Idx], ColumnEdges[Idx], 15, 0)
                          gen_rectangle2 (FillLevel, FillLevelHeight[Idx], ColumnEdges[Idx], 0, 28, 20)
                          Errors := Errors + 1
                          dev_set_color ('red')
                          dev_display (ChamberSingle)
                          disp_message (WindowHandle, 'NG', 'image', FillLevelHeight[Idx] - 50, ColumnEdges[Idx] - 10, 'red', 'false')
                      else
                          disp_message (WindowHandle, 'OK', 'image', FillLevelHeight[Idx] - 50, ColumnEdges[Idx] - 10, 'green', 'false')
                          dev_set_color ('green')
                      endif
                      dev_display (FillLevel)
                      dev_display (Cross)
                  endfor
                  * 
                  * Check, whether the fill level is within the allowed range - does not deviate too much
                  * from average fill level
                  * 
                  * Display statistics
                  *显示
                  if (Errors > 0)
                      disp_message (WindowHandle, Errors + ' BAD', 'window', 10, 12, 'red', 'true')
                  else
                      disp_message (WindowHandle, 'All OK', 'window', 10, 12, 'forest green', 'true')
                  endif
                  if (Index < NumImages)
                      disp_continue_message (WindowHandle, 'black', 'true')
                      stop ()
                  endif
              endfor
              
               
               

                在这里插入图片描述

                Halcon 拟合方法的求缺陷

                在这里插入图片描述

                * This example program shows how fit_rectangle2_contour_xld can be used to
                * detect manufacturing errors of punched holes in a metal part.  The errors
                * show up as small protrusions of the metal into the hole.  They can be detected
                * by fitting rectangles to the edges of the hole robustly (i.e., with outlier
                * suppression) and the calculating the distances of the edges to the rectangle
                * sides using dist_rectangle2_contour_points_xld.  Since the corners of the
                * holes are slightly rounded, some extra processing must be performed to
                * disregard the corners in the check for errors.
                * 1.图像的采集
                dev_update_off ()
                read_image (Image, 'punched_holes')
                get_image_size (Image, Width, Height)
                dev_close_window ()
                dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
                set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
                dev_display (Image)
                * Since the metal part is backlit, the processing speed can be increased
                * significantly by constructing a ROI for the subpixel-precise edge extraction
                * that is as small as possible.  This can easily be achieved by thresholding and
                * morphology.
                * 2.预处理(逼近边缘图像,进行抠图1.手画 2.blob分析)
                fast_threshold (Image, Region, 128, 255, 10)
                * 求区域内边界
                boundary (Region, Border, 'inner')
                * 矩形膨胀
                dilation_rectangle1 (Border, EdgeROI, 7, 7)
                * 抠图
                reduce_domain (Image, EdgeROI, ImageReduced)
                * Perform the edge extraction.
                * 边缘亚像素处理
                edges_sub_pix (ImageReduced, Edges, 'canny', 1.7, 40, 120)
                * Remove edge fragments that are too short.
                * 选择需要的区域周长 500以上
                select_shape_xld (Edges, RectangleEdges, 'contlength', 'and', 500, 100000)
                * Fit rectangles to the holes' edges using the outlier suppression of Tukey.
                * 拟合最小外接矩形
                fit_rectangle2_contour_xld (RectangleEdges, 'tukey', -1, 0, 0, 3, 2, Row, Column, Phi, Length1, Length2, PointOrder)
                * Create rectangles with the fitted parameters for visualization purposes.
                * 形成一个拟合的矩形区域
                gen_rectangle2_contour_xld (Rectangles, Row, Column, Phi, Length1, Length2)
                dev_set_color ('yellow')
                dev_display (Rectangles)
                * Check whether the holes are OK.
                * 求轮廓上的点和最小外接矩形上的点之间的距离
                count_obj (RectangleEdges, Number)
                for I := 0 to Number - 1 by 1
                    *选择获取轮廓
                    select_obj (RectangleEdges, RectangleEdge, I + 1)
                    * Get the contour's coordinates.
                    * 获取亚像素轮廓上的点的坐标
                    get_contour_xld (RectangleEdge, Rows, Cols)
                    * Create a rectangle with the appropriate rectangle parameters.
                    *产生一个亚像素轮廓矩形
                    gen_rectangle2_contour_xld (Rect, Row[I], Column[I], Phi[I], Length1[I], Length2[I])
                    * Get the coordinates of the rectangle's corners.
                    *获取产生亚像素轮廓点的坐标 (获取坐标点的四个顶点,最后一个点回到原点,一共5个点)
                    get_contour_xld (Rect, RowC, ColC)
                    * Calculate the distances of all the contour points to the four corners of the
                    * rectangle.
                    * 勾股定理求两点之间的距离
                    D1 := sqrt((Rows - RowC[0]) * (Rows - RowC[0]) + (Cols - ColC[0]) * (Cols - ColC[0]))
                    D2 := sqrt((Rows - RowC[1]) * (Rows - RowC[1]) + (Cols - ColC[1]) * (Cols - ColC[1]))
                    D3 := sqrt((Rows - RowC[2]) * (Rows - RowC[2]) + (Cols - ColC[2]) * (Cols - ColC[2]))
                    D4 := sqrt((Rows - RowC[3]) * (Rows - RowC[3]) + (Cols - ColC[3]) * (Cols - ColC[3]))
                    * The distance of the contour points to the corners of the rectangle is given
                    * by the minimum of the four distances.  This distance is used to exclude
                    * contour points that are too close to the corners from the check for errors.
                    *点到轮廓的最小距离(点到四个顶点的距离)
                    DistCorner := min2(min2(D1,D2),min2(D3,D4))
                    * Calculate the distances of the contour points of the rectangle.
                    * 求轮廓上的点到最小外接矩形边的距离
                    dist_rectangle2_contour_points_xld (RectangleEdge, 0, Row[I], Column[I], Phi[I], Length1[I], Length2[I], Dist)
                    * Check whether the hole is OK by examining the distances of the contour
                    * points to the rectangle.  A hole is OK if all points that lie more than seven
                    * pixels from the corners have a distance of less than one pixel to the fitted
                    * rectangle.  To do so, we could use the following code:
                *     RectangleOK := true
                *     for J := 0 to |Dist| - 1 by 1
                *         if (DistCorner[J] > 7.0 and Dist[J] > 1.0)
                *             RectangleOK := false
                *             break
                *         endif
                *     endfor
                    * A much faster way to do this in HDevelop is to generate a mask that
                    * contains 0 for all points that should not be taken into account and 1
                    * otherwise.  To do so, we subtract the minimum distance of 7.0 from the
                    * distances to the corners and take the maximum of 0.0 and the resulting
                    * values.  This sets all the distances that are too close to the corners to 0.
                    * To set all other values to 1, we can simply take the sign of the values.
                    *去除矩形的四个角上的点
                    *sgn为符号函数将DistCorner - 7.00的值设置为1 ,如果没有>0的就为0-7.0 为了排除拐角的距离
                    Mask := sgn(max2(DistCorner - 7.0,0.0))
                    * We can now multiply the distances to the rectangle with the mask and
                    * check whether the maximum distance is smaller than the maximum allowed
                    * distance of 1.0.
                    * 如果max(Dist * Mask)<= 1.0则为正常,否则有缺陷
                    RectangleOK := max(Dist * Mask) <= 1.0
                    * Display whether the hole is OK.
                    if (RectangleOK)
                        dev_set_color ('green')
                        get_string_extents (WindowHandle, 'OK', Ascent, Descent, Width, Height)
                        set_tposition (WindowHandle, Row[I] - Height / 2, Column[I] - Width / 2)
                        write_string (WindowHandle, 'OK')
                    else
                        dev_set_color ('red')
                        get_string_extents (WindowHandle, 'Not OK', Ascent, Descent, Width, Height)
                        set_tposition (WindowHandle, Row[I] - Height / 2, Column[I] - Width / 2)
                        write_string (WindowHandle, 'Not OK')
                    endif
                endfor
                
                 
                 

                  在这里插入图片描述
                  在这里插入图片描述

                  Mask := sgn(max2(DistCorner - 7.0,0.0))排除拐角的距离
                  在这里插入图片描述

                  Halcon OCV光学字符检测

                  OCV 模板创建

                  在这里插入图片描述

                  * Saving an OCV tool to file
                  * 1.采集图像
                  read_image (Image, 'a01')
                  * 2.提取字符
                  threshold (Image, Region, 0, 100)
                  shape_trans (Region, RegionTrans, 'rectangle1')
                  dilation_rectangle1 (RegionTrans, RegionDilation, 10, 10)
                  * Reduce the domain of an image.
                  reduce_domain (Image, RegionDilation, ImageReduced)
                  * Create a new OCV tool based on gray value projections
                  * 3.训练
                  * 创建ocv模型
                  create_ocv_proj ('A', OCVHandle)
                  * 训练模型
                  traind_ocv_proj (ImageReduced, OCVHandle, 'A', 'single')
                  * 保存ocv文件
                  write_ocv (OCVHandle, 'test_ocv.ocv')
                  OCVHandle := []
                  stop ()
                  read_ocv ('test_ocv.ocv', OCVHandle)
                  
                   
                   

                    在这里插入图片描述

                    OCV 字符质量缺陷识别

                    在这里插入图片描述

                    * 1.读取图像
                    read_image (Image, 'fonts/arial_a1')
                    get_image_size (Image, Width, Height)
                    dev_close_window ()
                    dev_open_window (0, 0, Width / 2, Height / 2 + 42, 'black', WindowHandle)
                    dev_set_part (-84, 0, Height - 1, Width - 1)
                    set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
                    dev_display (Image)
                    dev_set_draw ('margin')
                    gen_rectangle1 (Rectangle, 37, 69, 115, 141)
                    reduce_domain (Image, Rectangle, ImageReduced)
                    * 2.创建模板
                    create_ocv_proj ('A', OCVHandle)
                    * 训练
                    traind_ocv_proj (ImageReduced, OCVHandle, 'A', 'single')
                    dev_set_color ('red')
                    for I := 1 to 9 by 1
                        read_image (Image, 'fonts/arial_a' + I)
                        binary_threshold (Image, Region, 'max_separability', 'dark', UsedThreshold)
                        connection (Region, ConnectedRegions)
                        select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 150, 99999)
                        sort_region (SelectedRegions, SortedRegions, 'character', 'true', 'row')
                        select_obj (SortedRegions, ObjectSelected, 1)
                        shape_trans (ObjectSelected, RegionTrans, 'rectangle1')
                        dilation_rectangle1 (RegionTrans, RegionDilation, 15, 15)
                        reduce_domain (Image, RegionDilation, ImageReduced)
                        * Quality 获取字符质量
                        do_ocv_simple (ImageReduced, OCVHandle, 'A', 'true', 'true', 'true', 'true', 5, Quality)
                        * Display quality  质量大于0.9合格
                        if (Quality > 0.9)
                            Color := 'green'
                        elseif (Quality > 0.7)
                            Color := 'yellow'
                        else
                            Color := 'red'
                        endif
                        dev_display (Image)
                        dev_set_color (Color)
                        dev_set_line_width (2)
                        dev_display (RegionDilation)
                        disp_message (WindowHandle, 'Check print quality of \'A\' (Image  ' + I + ' of 9)', 'window', 12, 12, 'black', 'true')
                        disp_message (WindowHandle, 'Quality = ' + Quality$'.2f', 'image', 120, 12, 'black', Color)
                        if (I < 9)
                            disp_continue_message (WindowHandle, 'black', 'true')
                            stop ()
                        endif
                    endfor
                    
                     
                     

                      在这里插入图片描述

                      OCV 字符缺陷检测展示案例

                      在这里插入图片描述

                      read_image (Image, 'a01')
                      get_image_size (Image, Width, Height)
                      dev_close_window ()
                      dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
                      dev_display (Image)
                      threshold (Image, Region, 0, 100)
                      shape_trans (Region, RegionTrans, 'rectangle1')
                      dilation_rectangle1 (RegionTrans, RegionDilation, 10, 10)
                      reduce_domain (Image, RegionDilation, ImageReduced)
                      create_ocv_proj ('A', OCVHandle)
                      traind_ocv_proj (ImageReduced, OCVHandle, 'A', 'single')
                      dev_set_color ('red')
                      for add := -20 to 20 by 1
                          scale_image (Image, ImageScaled, 1, add)
                          reduce_domain (ImageScaled, RegionDilation, ImageReduced)
                          do_ocv_simple (ImageReduced, OCVHandle, 'A', 'true', 'true', 'true', 'true', -1, Quality)
                          set_tposition (WindowHandle, 24, 12)
                          write_string (WindowHandle, 'Intensity change = ' + add + '       Quality = ' + Quality)
                          stop ()
                      endfor
                      for scale := 0.8 to 1.2 by 0.025
                          scale_image (Image, ImageScaled, scale, 0)
                          reduce_domain (ImageScaled, RegionDilation, ImageReduced)
                          do_ocv_simple (ImageReduced, OCVHandle, 'A', 'true', 'true', 'true', 'true', -1, Quality)
                          set_tposition (WindowHandle, 24, 12)
                          write_string (WindowHandle, 'Intensity scale = ' + scale + '       Quality = ' + Quality)
                          stop ()
                      endfor
                      for Length := 1 to 17 by 1
                          gen_rectangle1 (Rectangle, 150, 145, 150 + 5, 145 + Length)
                          paint_region (Rectangle, Image, ImageError, 210, 'fill')
                          reduce_domain (ImageError, RegionDilation, ImageReduced)
                          do_ocv_simple (ImageReduced, OCVHandle, 'A', 'true', 'true', 'true', 'true', -1, Quality)
                          set_tposition (WindowHandle, 24, 12)
                          write_string (WindowHandle, 'Length of rectangle = ' + Length + '   Quality = ' + Quality)
                          stop ()
                      endfor
                      
                       
                       
                        转载自:https://blog.csdn.net/weixin_45672157/article/details/135960479
                        Halcon是一种常用的图像处理软件,可以用于各种缺陷检测任务。在Halcon中,可以使用不同的算法和技术来进行缺陷检测。根据引用\[1\]中提供的实例,Halcon可以用于背景网格产品刮伤缺陷检测、不均匀表面刮伤检测、产品表面三角缺陷检测、产品毛刺检测、产品上凹坑检测、产品周围缺口检测、电路板短路、断路检测、找出所有网格顶点的位置、化妆品标签褶皱检测、皮革纹理表面缺陷检测、手机摄像头图像表面的轻微缺陷检测、网状产品表面破损检测、铣刀刀口破损缺陷检测和检测印刷数字是否完整等任务。 根据引用\[2\]中的描述,缺陷检测是一项具有挑战性的任务,需要保证稳定性和精度。传统的算法检测缺陷通常需要进行复杂的调试和参数调整,而且容易出现检测不稳定和误测的情况。机器学习和深度学习成为缺陷检测领域的重要技术难点。机器学习方法通常使用类似MLP的神经网络对缺陷特征进行训练和分类。深度学习方法则需要大量的缺陷样本进行训练,并且需要手动标注缺陷位置,训练周期较长。迁移学习法是一种新兴的方法,可以利用已经训练好的网络模型进行缺陷检测。 在日常工程应用中,Halcon通常使用形状匹配进行定位,但当待匹配物体有较大变形时,形状匹配的结果可能不准确,特别是在塑料产品成形时变形或纺织产品因褶皱而变形的情况下。这时需要采用其他方法来获得精确的定位结果。 综上所述,Halcon可以应用于各种缺陷检测任务,并且可以根据具体情况选择合适的算法和技术来实现精确的检测结果。 #### 引用[.reference_title] - *1* [【愚公系列】2023年04月 Halcon机器视觉-15种常用缺陷检测实例](https://blog.csdn.net/aa2528877987/article/details/129943049)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [基于halcon缺陷检测常用方法与示例总结](https://blog.csdn.net/weixin_50016546/article/details/124981131)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
                        评论
                        添加红包

                        请填写红包祝福语或标题

                        红包个数最小为10个

                        红包金额最低5元

                        当前余额3.43前往充值 >
                        需支付:10.00
                        成就一亿技术人!
                        领取后你会自动成为博主和红包主的粉丝 规则
                        hope_wisdom
                        发出的红包
                        实付
                        使用余额支付
                        点击重新获取
                        扫码支付
                        钱包余额 0

                        抵扣说明:

                        1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
                        2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

                        余额充值