本文已参与「新人创作礼」活动,一起开启掘金创作之路。
图像的采集以及曝光的设定
// Local iconic variables
HObject ho_Image=null;
// Local control variables
HTuple hv_AcqHandle = null;
// Initialize local and output iconic variables
HOperatorSet.GenEmptyObj(out ho_Image);
//Image Acquisition 01: Code generated by Image Acquisition 01
//Image Acquisition 01: Attention: The initialization may fail in case parameters need to
//Image Acquisition 01: be set in a specific order (e.g., image resolution vs. offset).
HOperatorSet.OpenFramegrabber("GigEVision2", 0, 0, 0, 0, 0, 0, "progressive",
-1, "default", -1, "false", "default", "GP130000016", 0, -1, out hv_AcqHandle);
HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "Width", 1280);
HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "Height", 1016);
HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "OffsetX", 0);
HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "PixelFormat", "Mono8");
HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "TriggerLoopPeriod", 1000000);
HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "ExposureTime", 4998.06);
HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
while ((int)(1) != 0)
{
ho_Image.Dispose();
HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);
//Image Acquisition 01: Do something
}
HOperatorSet.CloseFramegrabber(hv_AcqHandle);
ho_Image.Dispose();
铁钩位置的判断
C# 代码
//
// File generated by HDevelop for HALCON/.NET (C#) Version 18.11.0.1
// Non-ASCII strings in this file are encoded in local-8-bit encoding (cp936).
//
// Please note that non-ASCII characters in string constants are exported
// as octal codes in order to guarantee that the strings are correctly
// created on all systems, independent on any compiler settings.
//
// Source files with different encoding should not be mixed in one project.
//
using HalconDotNet;
public partial class HDevelopExport
{
#if !(NO_EXPORT_MAIN || NO_EXPORT_APP_MAIN)
public HDevelopExport()
{
// Default settings used in HDevelop
HOperatorSet.SetSystem("width", 512);
HOperatorSet.SetSystem("height", 512);
if (HalconAPI.isWindows)
HOperatorSet.SetSystem("use_window_thread","true");
action();
}
#endif
// Procedures
// Chapter: Develop
// Short Description: Open a new graphics window that preserves the aspect ratio of the given image size.
public void dev_open_window_fit_size (HTuple hv_Row, HTuple hv_Column, HTuple hv_Width,
HTuple hv_Height, HTuple hv_WidthLimit, HTuple hv_HeightLimit, out HTuple hv_WindowHandle)
{
// Local iconic variables
// Local control variables
HTuple hv_MinWidth = new HTuple(), hv_MaxWidth = new HTuple();
HTuple hv_MinHeight = new HTuple(), hv_MaxHeight = new HTuple();
HTuple hv_ResizeFactor = new HTuple(), hv_TempWidth = new HTuple();
HTuple hv_TempHeight = new HTuple(), hv_WindowWidth = new HTuple();
HTuple hv_WindowHeight = new HTuple();
// Initialize local and output iconic variables
hv_WindowHandle = new HTuple();
//This procedure open a new graphic window
//such that it fits into the limits specified by WidthLimit
//and HeightLimit, but also maintains the correct aspect ratio
//given by Width and Height.
//
//If it is impossible to match the minimum and maximum extent requirements
//at the same time (f.e. if the image is very long but narrow),
//the maximum value gets a higher priority.
//
//Parse input tuple WidthLimit
if ((int)((new HTuple((new HTuple(hv_WidthLimit.TupleLength())).TupleEqual(0))).TupleOr(
new HTuple(hv_WidthLimit.TupleLess(0)))) != 0)
{
hv_MinWidth.Dispose();
hv_MinWidth = 500;
hv_MaxWidth.Dispose();
hv_MaxWidth = 800;
}
else if ((int)(new HTuple((new HTuple(hv_WidthLimit.TupleLength())).TupleEqual(
1))) != 0)
{
hv_MinWidth.Dispose();
hv_MinWidth = 0;
hv_MaxWidth.Dispose();
hv_MaxWidth = new HTuple(hv_WidthLimit);
}
else
{
hv_MinWidth.Dispose();
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
hv_MinWidth = hv_WidthLimit.TupleSelect(
0);
}
hv_MaxWidth.Dispose();
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
hv_MaxWidth = hv_WidthLimit.TupleSelect(
1);
}
}
//Parse input tuple HeightLimit
if ((int)((new HTuple((new HTuple(hv_HeightLimit.TupleLength())).TupleEqual(0))).TupleOr(
new HTuple(hv_HeightLimit.TupleLess(0)))) != 0)
{
hv_MinHeight.Dispose();
hv_MinHeight = 400;
hv_MaxHeight.Dispose();
hv_MaxHeight = 600;
}
else if ((int)(new HTuple((new HTuple(hv_HeightLimit.TupleLength())).TupleEqual(
1))) != 0)
{
hv_MinHeight.Dispose();
hv_MinHeight = 0;
hv_MaxHeight.Dispose();
hv_MaxHeight = new HTuple(hv_HeightLimit);
}
else
{
hv_MinHeight.Dispose();
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
hv_MinHeight = hv_HeightLimit.TupleSelect(
0);
}
hv_MaxHeight.Dispose();
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
hv_MaxHeight = hv_HeightLimit.TupleSelect(
1);
}
}
//
//Test, if window size has to be changed.
hv_ResizeFactor.Dispose();
hv_ResizeFactor = 1;
//First, expand window to the minimum extents (if necessary).
if ((int)((new HTuple(hv_MinWidth.TupleGreater(hv_Width))).TupleOr(new HTuple(hv_MinHeight.TupleGreater(
hv_Height)))) != 0)
{
hv_ResizeFactor.Dispose();
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
hv_ResizeFactor = (((((hv_MinWidth.TupleReal()
)/hv_Width)).TupleConcat((hv_MinHeight.TupleReal())/hv_Height))).TupleMax()
;
}
}
hv_TempWidth.Dispose();
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
hv_TempWidth = hv_Width*hv_ResizeFactor;
}
hv_TempHeight.Dispose();
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
hv_TempHeight = hv_Height*hv_ResizeFactor;
}
//Then, shrink window to maximum extents (if necessary).
if ((int)((new HTuple(hv_MaxWidth.TupleLess(hv_TempWidth))).TupleOr(new HTuple(hv_MaxHeight.TupleLess(
hv_TempHeight)))) != 0)
{
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
{
HTuple
ExpTmpLocalVar_ResizeFactor = hv_ResizeFactor*((((((hv_MaxWidth.TupleReal()
)/hv_TempWidth)).TupleConcat((hv_MaxHeight.TupleReal())/hv_TempHeight))).TupleMin()
);
hv_ResizeFactor.Dispose();
hv_ResizeFactor = ExpTmpLocalVar_ResizeFactor;
}
}
}
hv_WindowWidth.Dispose();
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
hv_WindowWidth = hv_Width*hv_ResizeFactor;
}
hv_WindowHeight.Dispose();
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
hv_WindowHeight = hv_Height*hv_ResizeFactor;
}
//Resize window
HOperatorSet.SetWindowAttr("background_color","black");
HOperatorSet.OpenWindow(hv_Row,hv_Column,hv_WindowWidth,hv_WindowHeight,0,"visible","",out hv_WindowHandle);
HDevWindowStack.Push(hv_WindowHandle);
if (HDevWindowStack.IsOpen())
{
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
HOperatorSet.SetPart(HDevWindowStack.GetActive(), 0, 0, hv_Height-1, hv_Width-1);
}
}
hv_MinWidth.Dispose();
hv_MaxWidth.Dispose();
hv_MinHeight.Dispose();
hv_MaxHeight.Dispose();
hv_ResizeFactor.Dispose();
hv_TempWidth.Dispose();
hv_TempHeight.Dispose();
hv_WindowWidth.Dispose();
hv_WindowHeight.Dispose();
return;
}
#if !NO_EXPORT_MAIN
// Main procedure
private void action()
{
// Local iconic variables
HObject ho_Image=null, ho_ImageSub=null, ho_Regions=null;
HObject ho_ImageReduced=null, ho_ImageScaled=null, ho_RegionDilation=null;
HObject ho_RegionErosion=null, ho_RegionFillUp=null, ho_ImageMedian=null;
HObject ho_ImageGauss=null, ho_ImageScaled1=null, ho_ConnectedRegions=null;
HObject ho_SelectedRegions=null, ho_ConnectedRegions1=null;
HObject ho_Region=null, ho_ObjectSelected=null, ho_Cross=null;
// Local control variables
HTuple hv_Files = new HTuple(), hv_Index1 = new HTuple();
HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
HTuple hv_WindowHandle = new HTuple(), hv_Area = new HTuple();
HTuple hv_Row = new HTuple(), hv_Column = new HTuple();
HTuple hv_UsedThreshold = new HTuple(), hv_Rows1 = new HTuple();
HTuple hv_Columns1 = new HTuple(), hv_Length = new HTuple();
HTuple hv_Sorted = new HTuple(), hv_Indices = new HTuple();
HTuple hv_tuple = new HTuple(), hv_Index = new HTuple();
HTuple hv_Max = new HTuple(), hv_Min = new HTuple(), hv_tiegou = new HTuple();
HTuple hv_Inverted = new HTuple(), hv_tiegou1 = new HTuple();
// Initialize local and output iconic variables
HOperatorSet.GenEmptyObj(out ho_Image);
HOperatorSet.GenEmptyObj(out ho_ImageSub);
HOperatorSet.GenEmptyObj(out ho_Regions);
HOperatorSet.GenEmptyObj(out ho_ImageReduced);
HOperatorSet.GenEmptyObj(out ho_ImageScaled);
HOperatorSet.GenEmptyObj(out ho_RegionDilation);
HOperatorSet.GenEmptyObj(out ho_RegionErosion);
HOperatorSet.GenEmptyObj(out ho_RegionFillUp);
HOperatorSet.GenEmptyObj(out ho_ImageMedian);
HOperatorSet.GenEmptyObj(out ho_ImageGauss);
HOperatorSet.GenEmptyObj(out ho_ImageScaled1);
HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1);
HOperatorSet.GenEmptyObj(out ho_Region);
HOperatorSet.GenEmptyObj(out ho_ObjectSelected);
HOperatorSet.GenEmptyObj(out ho_Cross);
hv_Files.Dispose();
HOperatorSet.ListFiles("D:/资料/车钩资料/车钩/newimage", "files", out hv_Files);
for (hv_Index1=1; (int)hv_Index1<=(int)((new HTuple(hv_Files.TupleLength()))-1); hv_Index1 = (int)hv_Index1 + 1)
{
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
ho_Image.Dispose();
HOperatorSet.ReadImage(out ho_Image, hv_Files.TupleSelect(hv_Index1));
}
hv_Width.Dispose();hv_Height.Dispose();
HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
if (HDevWindowStack.IsOpen())
{
HOperatorSet.CloseWindow(HDevWindowStack.Pop());
}
hv_WindowHandle.Dispose();
dev_open_window_fit_size(0, 0, hv_Width, hv_Width, -1, -1, out hv_WindowHandle);
ho_ImageSub.Dispose();
HOperatorSet.SubImage(ho_Image, ho_Image, out ho_ImageSub, 0, 25);
hv_Width.Dispose();hv_Height.Dispose();
HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
if (HDevWindowStack.IsOpen())
{
HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
}
ho_Regions.Dispose();
HOperatorSet.AutoThreshold(ho_Image, out ho_Regions, 10);
ho_ImageReduced.Dispose();
HOperatorSet.ReduceDomain(ho_Image, ho_Regions, out ho_ImageReduced);
//图像增强
ho_ImageScaled.Dispose();
HOperatorSet.ScaleImage(ho_ImageReduced, out ho_ImageScaled, 0, 0.0);
//将目标区域进行膨胀处理
ho_RegionDilation.Dispose();
HOperatorSet.DilationCircle(ho_Regions, out ho_RegionDilation, 2);
//将目标区域进行腐蚀处理
ho_RegionErosion.Dispose();
HOperatorSet.ErosionCircle(ho_RegionDilation, out ho_RegionErosion, 5);
//填充孔洞
ho_RegionFillUp.Dispose();
HOperatorSet.FillUp(ho_RegionErosion, out ho_RegionFillUp);
ho_ImageMedian.Dispose();
HOperatorSet.MedianImage(ho_ImageScaled, out ho_ImageMedian, "circle", 1, "mirrored");
ho_ImageGauss.Dispose();
HOperatorSet.GaussFilter(ho_ImageMedian, out ho_ImageGauss, 5);
ho_ImageScaled1.Dispose();
HOperatorSet.ScaleImage(ho_ImageGauss, out ho_ImageScaled1, 1, 1);
ho_ConnectedRegions.Dispose();
HOperatorSet.Connection(ho_RegionFillUp, out ho_ConnectedRegions);
ho_SelectedRegions.Dispose();
HOperatorSet.SelectShape(ho_RegionFillUp, out ho_SelectedRegions, "area", "and",
500000, 3000000);
ho_ConnectedRegions1.Dispose();
HOperatorSet.Connection(ho_SelectedRegions, out ho_ConnectedRegions1);
hv_Area.Dispose();hv_Row.Dispose();hv_Column.Dispose();
HOperatorSet.AreaCenter(ho_ConnectedRegions1, out hv_Area, out hv_Row, out hv_Column);
if ((int)((new HTuple(hv_Row.TupleGreater(600))).TupleAnd(new HTuple(hv_Column.TupleGreater(
1500)))) != 0)
{
if (HDevWindowStack.IsOpen())
{
HOperatorSet.DispObj(ho_ConnectedRegions1, HDevWindowStack.GetActive());
}
ho_Region.Dispose();hv_UsedThreshold.Dispose();
HOperatorSet.BinaryThreshold(ho_Image, out ho_Region, "max_separability",
"dark", out hv_UsedThreshold);
ho_ConnectedRegions.Dispose();
HOperatorSet.Connection(ho_Region, out ho_ConnectedRegions);
if (HDevWindowStack.IsOpen())
{
HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 3);
}
//提取线条
ho_ObjectSelected.Dispose();
HOperatorSet.SelectObj(ho_ConnectedRegions, out ho_ObjectSelected, 1);
hv_Rows1.Dispose();hv_Columns1.Dispose();
HOperatorSet.GetRegionPolygon(ho_ObjectSelected, 5, out hv_Rows1, out hv_Columns1);
//显示线条上的拐点
ho_Cross.Dispose();
HOperatorSet.GenCrossContourXld(out ho_Cross, hv_Rows1, hv_Columns1, 30,
0.785398);
hv_Length.Dispose();
HOperatorSet.TupleLength(hv_Rows1, out hv_Length);
if ((int)(new HTuple(hv_Length.TupleGreater(14))) != 0)
{
//数组升序
hv_Sorted.Dispose();
HOperatorSet.TupleSort(hv_Columns1, out hv_Sorted);
hv_Indices.Dispose();
HOperatorSet.TupleSortIndex(hv_Columns1, out hv_Indices);
hv_tuple.Dispose();
hv_tuple = new HTuple();
for (hv_Index=1; (int)hv_Index<=13; hv_Index = (int)hv_Index + 1)
{
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
{
HTuple
ExpTmpLocalVar_tuple = hv_tuple.TupleConcat(
hv_Rows1.TupleSelect(hv_Indices.TupleSelect(hv_Index)));
hv_tuple.Dispose();
hv_tuple = ExpTmpLocalVar_tuple;
}
}
}
hv_Max.Dispose();
HOperatorSet.TupleMax(hv_tuple, out hv_Max);
hv_Min.Dispose();
HOperatorSet.TupleMin(hv_tuple, out hv_Min);
hv_tiegou.Dispose();
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
hv_tiegou = ((hv_Max-hv_Min)/2)+hv_Min;
}
}
}
else if ((int)((new HTuple(hv_Row.TupleGreater(600))).TupleAnd(new HTuple(hv_Column.TupleLess(
1000)))) != 0)
{
ho_Region.Dispose();hv_UsedThreshold.Dispose();
HOperatorSet.BinaryThreshold(ho_Image, out ho_Region, "max_separability",
"dark", out hv_UsedThreshold);
ho_ConnectedRegions.Dispose();
HOperatorSet.Connection(ho_Region, out ho_ConnectedRegions);
if (HDevWindowStack.IsOpen())
{
HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 3);
}
//提取线条
ho_ObjectSelected.Dispose();
HOperatorSet.SelectObj(ho_ConnectedRegions, out ho_ObjectSelected, 1);
hv_Rows1.Dispose();hv_Columns1.Dispose();
HOperatorSet.GetRegionPolygon(ho_ObjectSelected, 5, out hv_Rows1, out hv_Columns1);
//显示线条上的拐点
ho_Cross.Dispose();
HOperatorSet.GenCrossContourXld(out ho_Cross, hv_Rows1, hv_Columns1, 30,
0.785398);
hv_Length.Dispose();
HOperatorSet.TupleLength(hv_Rows1, out hv_Length);
if ((int)(new HTuple(hv_Length.TupleGreater(17))) != 0)
{
//数组升序
hv_Sorted.Dispose();
HOperatorSet.TupleSort(hv_Columns1, out hv_Sorted);
hv_Indices.Dispose();
HOperatorSet.TupleSortIndex(hv_Columns1, out hv_Indices);
hv_Inverted.Dispose();
HOperatorSet.TupleInverse(hv_Indices, out hv_Inverted);
hv_tuple.Dispose();
hv_tuple = new HTuple();
for (hv_Index=1; (int)hv_Index<=16; hv_Index = (int)hv_Index + 1)
{
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
{
HTuple
ExpTmpLocalVar_tuple = hv_tuple.TupleConcat(
hv_Rows1.TupleSelect(hv_Inverted.TupleSelect(hv_Index)));
hv_tuple.Dispose();
hv_tuple = ExpTmpLocalVar_tuple;
}
}
}
hv_Max.Dispose();
HOperatorSet.TupleMax(hv_tuple, out hv_Max);
hv_Min.Dispose();
HOperatorSet.TupleMin(hv_tuple, out hv_Min);
hv_tiegou1.Dispose();
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
hv_tiegou1 = ((hv_Max-hv_Min)/2)+hv_Min;
}
}
}
}
ho_Image.Dispose();
ho_ImageSub.Dispose();
ho_Regions.Dispose();
ho_ImageReduced.Dispose();
ho_ImageScaled.Dispose();
ho_RegionDilation.Dispose();
ho_RegionErosion.Dispose();
ho_RegionFillUp.Dispose();
ho_ImageMedian.Dispose();
ho_ImageGauss.Dispose();
ho_ImageScaled1.Dispose();
ho_ConnectedRegions.Dispose();
ho_SelectedRegions.Dispose();
ho_ConnectedRegions1.Dispose();
ho_Region.Dispose();
ho_ObjectSelected.Dispose();
ho_Cross.Dispose();
hv_Files.Dispose();
hv_Index1.Dispose();
hv_Width.Dispose();
hv_Height.Dispose();
hv_WindowHandle.Dispose();
hv_Area.Dispose();
hv_Row.Dispose();
hv_Column.Dispose();
hv_UsedThreshold.Dispose();
hv_Rows1.Dispose();
hv_Columns1.Dispose();
hv_Length.Dispose();
hv_Sorted.Dispose();
hv_Indices.Dispose();
hv_tuple.Dispose();
hv_Index.Dispose();
hv_Max.Dispose();
hv_Min.Dispose();
hv_tiegou.Dispose();
hv_Inverted.Dispose();
hv_tiegou1.Dispose();
}
#endif
}
#if !(NO_EXPORT_MAIN || NO_EXPORT_APP_MAIN)
public class HDevelopExportApp
{
static void Main(string[] args)
{
new HDevelopExport();
}
}
#endif
halcon 代码
list_files ('D:/1--------------DAS-------------------工作/车钩资料/车钩图片', 'files', Files)
for Index1 := 1 to |Files| - 1 by 1
dev_close_window()
read_image (Image, Files[Index1])
get_image_size(Image,Width,Height)
dev_open_window_fit_size(0,0,Width,Width,-1, -1, WindowHandle)
dev_display (Image)
dev_set_draw ('margin')
dev_set_color ('red')
set_tposition (WindowHandle, 300, 100)
set_display_font (WindowHandle, 50, 'mono', 'true', 'false')
*此地开始检测
draw_line (WindowHandle, Row1, Column1, Row2, Column2)
LineParams := [Row1, Column1, Row2, Column2]
draw_line (WindowHandle, Row11, Column11, Row21, Column21)
try
* 找直线
findallline (Image, Contours, Cross, Contour, Width, Height, LineParams, MetrologyHandle, Index, Row1, Column1, Parameter)
row1:=Parameter[0]
col1:=Parameter[1]
row2:=Parameter[2]
col2:=Parameter[3]
intersection_lines (Row11, Column11, Row21, Column21, row1, col1, row2, col2, Row3, Column3, IsOverlapping)
line:=[Row3,Column3-40,Row3,Column3+40]
findallline (Image, Contours, Cross, Contour, Width, Height, line, MetrologyHandle, Index, Row2, Column2, Parameter)
tuple_length (Row2, Length)
tuple_length (Row1, Length1)
if(Length<20)
sum12:=Row2
sum13:=Row2
for Index2 := 0 to Length-1 by 1
if (Row2[Index2]>Row1[Length1/2])
Row2[Index2]:=0
endif
endfor
tuple_intersection (Row2, sum13, Intersection)
tuple_mean (Intersection, Mean)
for Index3 := 0 to Length-1 by 1
if (sum12[Index3]<Row1[Length1/2])
sum12[Index3]:=0
endif
endfor
tuple_intersection (sum12, sum13, Intersection1)
tuple_mean (Intersection1, Mean1)
dis:=Height-(((Mean1-Mean)/2)+Mean)
write_string (WindowHandle, '距离为:'+dis)
endif
catch (Exception)
write_string (WindowHandle, '图片未找到合适的结果')
endtry
endfor