C++ Halcon 条码识别

void action()
{

  // Local iconic variables
  HObject  ho_Image, ho_ImageReduced, ho_Region;
  HObject  ho_ImageEmphasize, ho_SymbolRegions, ho_Contours;
  HObject  ho_Image1, ho_BarCodeRegion, ho_ObjectSelected;
  HObject  ho_Region1, ho_ConnectedRegions, ho_RegionDilation;
  HObject  ho_ConnectedRegions1, ho_SelectedRegions, ho_ImageReduced1;

  // Local control variables
  HTuple  hv_Width, hv_Height, hv_WindowHandle;
  HTuple  hv_BarCodeHandle, hv_DecodedDataStrings, hv_Value;
  HTuple  hv_Value1, hv_Num, hv_Number, hv_index, hv_Number1;
  HTuple  hv_Number2;

  //Image Acquisition 01: Code generated by Image Acquisition 01
  if (HDevWindowStack::IsOpen())
    CloseWindow(HDevWindowStack::Pop());

  ReadImage(&ho_Image, //'F:/项目文档资料/条码/3.bmp'
      "F:/\317\356\304\277\316\304\265\265\327\312\301\317/\314\365\302\353/3.bmp");
  GetImageSize(ho_Image, &hv_Width, &hv_Height);
  SetWindowAttr("background_color","black");
  OpenWindow(0,0,hv_Width,hv_Height,0,"visible","",&hv_WindowHandle);
  HDevWindowStack::Push(hv_WindowHandle);


  //gen_rectangle1 (ROI_0, 551.257, 179.703, 855.422, 805.716)
  //* reduce_domain (Image, ROI_0, ImageReduced)
  //* var_threshold (ImageReduced, Region, 20, 20, 0.2, 12, 'dark')

  //dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
  //create_bar_code_model ([], [], BarCodeHandle)
  //emphasize (Image, ImageEmphasize, 3, 3, 1)

  //* find_bar_code (ImageEmphasize, SymbolRegions, BarCodeHandle, 'auto', DecodedDataStrings)
  //* gen_contour_region_xld (SymbolRegions, Contours, 'border')
  //* region_features (SymbolRegions, 'row1', Value)
  //* region_features (SymbolRegions, 'column1', Value1)
  //tuple_sub (Value, 50, Value)
  //* disp_obj (Image, WindowHandle)
  //* disp_obj (Contours, WindowHandle)
  //* set_display_font (WindowHandle, 56, 'mono', 'true', 'false')
  //* disp_message (WindowHandle, DecodedDataStrings, 'window', Value, Value1, 'green', 'false')
  //* dump_window_image (Image1, WindowHandle)
  //dev_close_window ()
  //* write_image (Image1, 'bmp', 0, 'result3')

  Threshold(ho_Image, &ho_Region, 240, 255);
  FillUp(ho_Region, &ho_Region);
  Connection(ho_Region, &ho_Region);
  SelectShape(ho_Region, &ho_Region, "area", "and", 20000, 99999999);
  CreateBarCodeModel(HTuple(), HTuple(), &hv_BarCodeHandle);
  hv_Num = 0;
  GenEmptyObj(&ho_BarCodeRegion);
  CountObj(ho_Region, &hv_Number);
  {
  HTuple end_val37 = hv_Number;
  HTuple step_val37 = 1;
  for (hv_index=1; hv_index.Continue(end_val37, step_val37); hv_index += step_val37)
  {
    SelectObj(ho_Region, &ho_ObjectSelected, hv_index);
    ReduceDomain(ho_Image, ho_ObjectSelected, &ho_ImageReduced);
    VarThreshold(ho_ImageReduced, &ho_Region1, 15, 15, 0.2, 6, "dark");
    Connection(ho_Region1, &ho_ConnectedRegions);
    SelectShape(ho_ConnectedRegions, &ho_ConnectedRegions, "rectangularity", "and", 
        0.8, 1);
    SelectShape(ho_ConnectedRegions, &ho_ConnectedRegions, "area", "and", 50, 99999);
    CountObj(ho_ConnectedRegions, &hv_Number1);
    if (0 != (hv_Number1>10))
    {
      Union1(ho_ConnectedRegions, &ho_ConnectedRegions);
      DilationRectangle1(ho_ConnectedRegions, &ho_RegionDilation, 41, 41);
      Connection(ho_RegionDilation, &ho_ConnectedRegions1);
      SelectShape(ho_ConnectedRegions1, &ho_SelectedRegions, "area", "and", 30000, 
          999999999);
      CountObj(ho_SelectedRegions, &hv_Number2);
      if (0 != (hv_Number2>=1))
      {
        hv_Num += hv_Number2;
        ConcatObj(ho_BarCodeRegion, ho_SelectedRegions, &ho_BarCodeRegion);
      }
    }
  }
  }

  Union1(ho_BarCodeRegion, &ho_BarCodeRegion);
  ReduceDomain(ho_Image, ho_BarCodeRegion, &ho_ImageReduced1);
  Emphasize(ho_ImageReduced1, &ho_ImageEmphasize, 3, 3, 2);
  FindBarCode(ho_ImageEmphasize, &ho_SymbolRegions, hv_BarCodeHandle, "auto", &hv_DecodedDataStrings);
  GenContourRegionXld(ho_SymbolRegions, &ho_Contours, "border");
  RegionFeatures(ho_SymbolRegions, "row1", &hv_Value);
  RegionFeatures(ho_SymbolRegions, "column1", &hv_Value1);
  TupleSub(hv_Value, 50, &hv_Value);
  DispObj(ho_Image, hv_WindowHandle);
  DispObj(ho_Contours, hv_WindowHandle);
  set_display_font(hv_WindowHandle, 16, "mono", "true", "false");
  disp_message(hv_WindowHandle, hv_DecodedDataStrings, "window", hv_Value, hv_Value1, 
      "green", "false");
  DumpWindowImage(&ho_Image1, hv_WindowHandle);
  if (HDevWindowStack::IsOpen())
    CloseWindow(HDevWindowStack::Pop());
  WriteImage(ho_Image1, "bmp", 0, "result3");

}

Halcon代码

dev_close_window ()

read_image (Image, 'F:/项目文档资料/条码/3.bmp')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
 

threshold (Image, Region, 240, 255)
fill_up (Region, Region)
connection (Region, Region)
select_shape (Region, Region, 'area', 'and', 20000, 99999999)
create_bar_code_model ([], [], BarCodeHandle)
Num:=0
gen_empty_obj (BarCodeRegion)
count_obj (Region, Number)
for index:=1 to Number by 1
    select_obj (Region, ObjectSelected, index)
    reduce_domain (Image, ObjectSelected, ImageReduced)
    var_threshold (ImageReduced, Region1, 15, 15, 0.2, 6, 'dark')
    connection (Region1, ConnectedRegions)
    select_shape (ConnectedRegions, ConnectedRegions, 'rectangularity', 'and', 0.8, 1)
    select_shape (ConnectedRegions, ConnectedRegions, 'area', 'and', 50, 99999)
    count_obj (ConnectedRegions, Number1)
    if(Number1>10)
         union1 (ConnectedRegions, ConnectedRegions)
         dilation_rectangle1 (ConnectedRegions, RegionDilation, 41, 41)
         connection (RegionDilation, ConnectedRegions1)
         select_shape (ConnectedRegions1, SelectedRegions, 'area', 'and', 30000, 999999999)
         count_obj (SelectedRegions, Number2)
         if(Number2>=1)
             Num:=Num+Number2
             concat_obj (BarCodeRegion, SelectedRegions, BarCodeRegion)
         endif
    endif
endfor

union1 (BarCodeRegion, BarCodeRegion)
reduce_domain (Image, BarCodeRegion, ImageReduced1)
emphasize (ImageReduced1, ImageEmphasize, 3, 3, 2)
find_bar_code (ImageEmphasize, SymbolRegions, BarCodeHandle, 'auto', DecodedDataStrings)
gen_contour_region_xld (SymbolRegions, Contours, 'border')
region_features (SymbolRegions, 'row1', Value)
region_features (SymbolRegions, 'column1', Value1)
tuple_sub (Value, 50, Value)
disp_obj (Image, WindowHandle)
disp_obj (Contours, WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
disp_message (WindowHandle, DecodedDataStrings, 'window', Value, Value1, 'green', 'false')
dump_window_image (Image1, WindowHandle)
dev_close_window ()
write_image (Image1, 'bmp', 0, 'result3')

你可能感兴趣的:(c++)