工控编程吧
标题:
上位机通过CCD视觉测量孔径
[打印本页]
作者:
qq263946146
时间:
2017-5-30 16:14
标题:
上位机通过CCD视觉测量孔径
上位机通过CCD视觉测量孔径
(, 下载次数: 6)
上传
点击文件名下载附件
(, 下载次数: 5)
上传
点击文件名下载附件
用摄像机检测物体的孔径,很多场合都用的到,这个例程实现这一功能,结构单位为像素;
可事先自己标定或其他方式,得出像素与mm等长度单位关系
下面是关键代码
void CMy123View::OnButton1()
{
HObject ho_Image, ho_Rims, ho_Holes, ho_Hole;
HTuple hv_Row, hv_Column, hv_Radius, hv_Width;
HTuple hv_Height, hv_hWnd, hv_Number, hv_i, hv_MinDistance;
HTuple hv_MaxDistance, hv_SVal, hv_Ascent, hv_Descent;
HTuple hv_Width1, hv_Height1;
ReadImage(&ho_Image, "pic/rim.png");
if (HDevWindowStack::IsOpen())
HalconCpp::CloseWindow(HDevWindowStack::Pop());
EdgesSubPix(ho_Image, &ho_Rims, "canny", 4, 20, 40);
SelectShapeXld(ho_Rims, &ho_Holes, "circularity", "and", 0.7, 1.0);
SortContoursXld(ho_Holes, &ho_Holes, "upper_left", "true", "row");
SmallestCircleXld(ho_Holes, &hv_Row, &hv_Column, &hv_Radius);
GetImageSize(ho_Image, &hv_Width, &hv_Height);
SetWindowAttr("background_color","black");
OpenWindow(0,0,hv_Width,hv_Height,(Hlong)m_Dlg.m_hWnd,"","",&hv_hWnd);
m_Dlg.SetWindowPos(NULL,0,0,hv_Width.I(),hv_Height.I(),SWP_NOZORDER|SWP_SHOWWINDOW);
HDevWindowStack::Push(hv_hWnd);
if (!HDevWindowStack::IsOpen())
return;
DispObj(ho_Image, HDevWindowStack::GetActive());
SetColor(HDevWindowStack::GetActive(),"green");
CountObj(ho_Holes, &hv_Number);
HTuple end_val11 = hv_Number;
HTuple step_val11 = 1;
for (hv_i=1; hv_i.Continue(end_val11, step_val11); hv_i += step_val11)
{
SelectObj(ho_Holes, &ho_Hole, hv_i);
DistancePc(ho_Hole, HTuple(hv_Row[hv_i-1]), HTuple(hv_Column[hv_i-1]), &hv_MinDistance, &hv_MaxDistance);
DispObj(ho_Hole, HDevWindowStack::GetActive());
DispCross(hv_hWnd, HTuple(hv_Row[hv_i-1]), HTuple(hv_Column[hv_i-1]), 6, 0);
TupleString(hv_MinDistance, ".3f", &hv_SVal);
GetStringExtents(hv_hWnd, "min: "+hv_SVal, &hv_Ascent, &hv_Descent, &hv_Width1, &hv_Height1);
disp_message(hv_hWnd, "min: "+hv_SVal, "image", HTuple(hv_Row[hv_i-1])-(2*hv_Ascent),
((HTuple(hv_Column[hv_i-1])-HTuple(hv_Radius[hv_i-1]))-hv_Width1)-10, "black", "true");
disp_message(hv_hWnd, "max: "+(hv_MaxDistance.TupleString(".3f")), "image", HTuple(hv_Row[hv_i-1]),
((HTuple(hv_Column[hv_i-1])-HTuple(hv_Radius[hv_i-1]))-hv_Width1)-10, "black", "true");
}
}
复制代码
void disp_message (HTuple hv_WindowHandle, HTuple hv_String, HTuple hv_CoordSystem,
HTuple hv_Row, HTuple hv_Column, HTuple hv_Color, HTuple hv_Box)
{
HTuple hv_Red, hv_Green, hv_Blue, hv_Row1Part;
HTuple hv_Column1Part, hv_Row2Part, hv_Column2Part, hv_RowWin;
HTuple hv_ColumnWin, hv_WidthWin, hv_HeightWin, hv_MaxAscent;
HTuple hv_MaxDescent, hv_MaxWidth, hv_MaxHeight, hv_R1;
HTuple hv_C1, hv_FactorRow, hv_FactorColumn, hv_UseShadow;
HTuple hv_ShadowColor, hv_Exception, hv_Width, hv_Index;
HTuple hv_Ascent, hv_Descent, hv_W, hv_H, hv_FrameHeight;
HTuple hv_FrameWidth, hv_R2, hv_C2, hv_DrawMode, hv_CurrentColor;
GetRgb(hv_WindowHandle, &hv_Red, &hv_Green, &hv_Blue);
GetPart(hv_WindowHandle, &hv_Row1Part, &hv_Column1Part, &hv_Row2Part, &hv_Column2Part);
GetWindowExtents(hv_WindowHandle, &hv_RowWin, &hv_ColumnWin, &hv_WidthWin, &hv_HeightWin);
SetPart(hv_WindowHandle, 0, 0, hv_HeightWin-1, hv_WidthWin-1);
if (hv_Row==-1)
hv_Row = 12;
if (hv_Column==-1)
hv_Column = 12;
if (hv_Color==HTuple())
hv_Color = "";
hv_String = ((""+hv_String)+"").TupleSplit("\n");
GetFontExtents(hv_WindowHandle, &hv_MaxAscent, &hv_MaxDescent, &hv_MaxWidth, &hv_MaxHeight);
if (0 != (hv_CoordSystem==HTuple("window")))
{
hv_R1 = hv_Row;
hv_C1 = hv_Column;
}
else
{
//Transform image to window coordinates
hv_FactorRow = (1.*hv_HeightWin)/((hv_Row2Part-hv_Row1Part)+1);
hv_FactorColumn = (1.*hv_WidthWin)/((hv_Column2Part-hv_Column1Part)+1);
hv_R1 = ((hv_Row-hv_Row1Part)+0.5)*hv_FactorRow;
hv_C1 = ((hv_Column-hv_Column1Part)+0.5)*hv_FactorColumn;
}
//
//Display text box depending on text size
hv_UseShadow = 1;
hv_ShadowColor = "gray";
if (0 != (HTuple(hv_Box[0])==HTuple("true")))
{
hv_Box[0] = "#fce9d4";
hv_ShadowColor = "#f28d26";
}
if (0 != ((hv_Box.TupleLength())>1))
{
if (0 != (HTuple(hv_Box[1])==HTuple("true")))
{
//Use default ShadowColor set above
}
else if (0 != (HTuple(hv_Box[1])==HTuple("false")))
{
hv_UseShadow = 0;
}
else
{
hv_ShadowColor = ((const HTuple&)hv_Box)[1];
//Valid color?
try
{
SetColor(hv_WindowHandle, HTuple(hv_Box[1]));
}
// catch (Exception)
catch (HalconCpp::HException &HDevExpDefaultException)
{
HDevExpDefaultException.ToHTuple(&hv_Exception);
hv_Exception = "Wrong value of control parameter Box[1] (must be a 'true', 'false', or a valid color string)";
throw HalconCpp::HException(hv_Exception);
}
}
}
if (HTuple(hv_Box[0])!=HTuple("false"))
{
try
{
SetColor(hv_WindowHandle, HTuple(hv_Box[0]));
}
catch (HalconCpp::HException &HDevExpDefaultException)
{
HDevExpDefaultException.ToHTuple(&hv_Exception);
hv_Exception = "Wrong value of control parameter Box[0] (must be a 'true', 'false', or a valid color string)";
throw HalconCpp::HException(hv_Exception);
}
hv_String = (" "+hv_String)+" ";
hv_Width = HTuple();
HTuple end_val93 = (hv_String.TupleLength())-1;
HTuple step_val93 = 1;
for (hv_Index=0; hv_Index.Continue(end_val93, step_val93); hv_Index += step_val93)
{
GetStringExtents(hv_WindowHandle, HTuple(hv_String[hv_Index]), &hv_Ascent,
&hv_Descent, &hv_W, &hv_H);
hv_Width = hv_Width.TupleConcat(hv_W);
}
hv_FrameHeight = hv_MaxHeight*(hv_String.TupleLength());
hv_FrameWidth = (HTuple(0).TupleConcat(hv_Width)).TupleMax();
hv_R2 = hv_R1+hv_FrameHeight;
hv_C2 = hv_C1+hv_FrameWidth;
GetDraw(hv_WindowHandle, &hv_DrawMode);
SetDraw(hv_WindowHandle, "fill");
SetColor(hv_WindowHandle, hv_ShadowColor);
if (0 != hv_UseShadow)
{
DispRectangle1(hv_WindowHandle, hv_R1+1, hv_C1+1, hv_R2+1, hv_C2+1);
}
SetColor(hv_WindowHandle, HTuple(hv_Box[0]));
DispRectangle1(hv_WindowHandle, hv_R1, hv_C1, hv_R2, hv_C2);
SetDraw(hv_WindowHandle, hv_DrawMode);
}
HTuple end_val115 = (hv_String.TupleLength())-1;
HTuple step_val115 = 1;
for (hv_Index=0; hv_Index.Continue(end_val115, step_val115); hv_Index += step_val115)
{
hv_CurrentColor = ((const HTuple&)hv_Color)[hv_Index%(hv_Color.TupleLength())];
if (0 != (HTuple(hv_CurrentColor!=HTuple("")).TupleAnd(hv_CurrentColor!=HTuple("auto"))))
{
SetColor(hv_WindowHandle, hv_CurrentColor);
}
else
{
SetRgb(hv_WindowHandle, hv_Red, hv_Green, hv_Blue);
}
hv_Row = hv_R1+(hv_MaxHeight*hv_Index);
SetTposition(hv_WindowHandle, hv_Row, hv_C1);
WriteString(hv_WindowHandle, HTuple(hv_String[hv_Index]));
}
SetRgb(hv_WindowHandle, hv_Red, hv_Green, hv_Blue);
SetPart(hv_WindowHandle, hv_Row1Part, hv_Column1Part, hv_Row2Part, hv_Column2Part);
}
复制代码
百度云附件:上位机通过CCD视觉测量孔径.rar
[weixinlianxi]1[/weixinlianxi]
作者:
荒唐的兔子
时间:
2019-4-15 22:55
学习..ghbxbxfjjxshnvhjvxvbc
欢迎光临 工控编程吧 (https://www.gkbc8.com/)
Powered by Discuz! X3.4