halcon缺陷檢測

lujun1632發表於2019-04-02

dev_close_window ()
dev_update_off ()
read_image (Image3, 'C:/Users/Administrator/Desktop/藥品檢測.png')
get_image_size (Image3, Width, Height)
dev_open_window_fit_image (Image3, 0, 0, -1, -1, WindowHandle)
dev_set_draw ('fill')
dev_set_line_width (3)
dev_display (Image3)
*處理圖片
threshold (Image3, Regions, 5, 62)
connection (Regions, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 227.96, 79863.2)
union1 (SelectedRegions, RegionUnion)
orientation_region (RegionUnion, Phi)
area_center (RegionUnion, Area, Row, Column)
vector_angle_to_rigid (Row, Column,Phi, Row, Column,rad(0), HomMat2D)
affine_trans_image (Image3, ImageAffineTrans, HomMat2D, 'constant', 'false')
affine_trans_region (RegionUnion, RegionAffineTrans, HomMat2D, 'nearest_neighbor')
dev_display (ImageAffineTrans)
gen_empty_obj (Chambers)
for I:=0 to 1 by 1
    Rows :=191+I*102
     for J:=0 to 5 by 1
         columns :=166.3+J*80
        gen_circle (circle,  Rows, columns, 30.991)
        concat_obj (Chambers, circle, Chambers)
        dev_display (ImageAffineTrans)
     endfor
endfor
difference (RegionAffineTrans, Chambers, RegionDifference)
union1 (Chambers, ChambersUnion)
orientation_region (ChambersUnion, PhiRef)
PhiRef1 := rad(180) + PhiRef
area_center (ChambersUnion, AreaRef, RowRef, ColumnRef)
read_image (Image3, 'C:/Users/Administrator/Desktop/藥品檢測.png')
dev_display(Image3)
*預處理
threshold (Image3, Regions1, 9, 90)
connection (Regions1, ConnectedRegions1)
select_shape (ConnectedRegions1, SelectedRegions1, 'area', 'and', 227.96, 91109.4)
union1 (SelectedRegions1, RegionUnion1)
fill_up (RegionUnion1, RegionFillUp)
orientation_region (RegionFillUp, Phi1)
area_center (RegionFillUp, Area1, Row1, Column1)
vector_angle_to_rigid (Row1, Column1, Phi1, RowRef, ColumnRef, PhiRef1, HomMat2D1)
affine_trans_image (Image3, ImageAffineTrans1, HomMat2D1, 'constant', 'false')
reduce_domain (ImageAffineTrans1,  ChambersUnion, ImageReduced)
*4進行影像分割(blob)
threshold (ImageReduced, Regions2, 3, 109)
connection (Regions2, ConnectedRegions2)
select_shape (ConnectedRegions2, SelectedRegions2, ['area','circularity'], 'and', [727.96,0.7682], [2000,0.9962])
count_obj (Chambers, Number)
sort_region (SelectedRegions2, SortedRegions, 'character', 'true', 'row')
 gen_empty_obj (WrongPill)
 gen_empty_obj (MissingPill)
 gen_empty_obj (CrackPill)
 for Index :=1 to Number by 1
     select_obj (Chambers, charms, Index)
     intersection (charms,SortedRegions, pill)
     area_center (pill, Area2, Row2, Column2)
     area_holes (pill, AreaHoles)
     if(AreaHoles>0)
         concat_obj (CrackPill, pill, CrackPill)

      else
         
        if(Area2>0)
            if(Area2<1400 or Area2>1600)
            concat_obj (WrongPill, pill, WrongPill) 
            else
             concat_obj (MissingPill, pill, MissingPill)   
         
             endif
    
         endif
      endif
     
   
     
 endfor
 *顯示
 
 count_obj (SortedRegions, Number1)
 count_obj (MissingPill, NumberMP)
 count_obj (CrackPill, NumberCP)
 count_obj (WrongPill, NumberWP)
 *顯示
 if (NumberWP > 0)
     disp_message (WindowHandle, '錯誤藥片'+NumberWP, 'image', 0, 0, 'red', 'true')
 endif
 if(NumberMP >0)
      disp_message (WindowHandle, '好藥片'+NumberMP, 'image', 20, 0, 'red', 'true')
 endif
 if(NumberCP>0)
     disp_message (WindowHandle, '破碎藥片'+NumberCP, 'image', 40, 0, 'red', 'true')
 endif
        



相關文章