一、效果预览
二、实现步骤
三、代码部分
ReadImage
Threshold
HalconWindowShowImage
RectangleDef类库
GenRectangle
public static void GenRectangle(HObject Region, out HObject ExternalRegion, out RectangleDef rectangleDef, bool isMargin, HWindow hWindow)
{
if (isMargin == true)
{
HOperatorSet.SetDraw(hWindow, "margin");
}
else
{
HOperatorSet.SetDraw(hWindow, "fill");
}
rectangleDef = new RectangleDef();
HOperatorSet.SmallestRectangle2(Region, out rectangleDef._CenterRow, out rectangleDef._CenterCol, out rectangleDef.Angle, out rectangleDef.len1, out rectangleDef.len2);
HOperatorSet.GenRectangle2(out ExternalRegion, rectangleDef._CenterRow, rectangleDef._CenterCol, rectangleDef.Angle, rectangleDef.len1, rectangleDef.len2);
}
ShowText
主函数中的调用
private HObject ho_image;
private void button_test_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
bool result = Algorithm.ImageHelp.ReadImage(out ho_image, openFileDialog.FileName, hWindowControl1.HalconWindow);
if (result != true)
{
MessageBox.Show("失败");
}
}
}
**********************************************************************************************************************************************************************************HTuple MaxGray;
HTuple MinGray;
HObject Region;
private void button_Threshold_Click(object sender, EventArgs e)
{
hWindowControl_Threshold.HalconWindow.ClearWindow();
//普通阈值分割
MaxGray = int.Parse(textBox_MaxGray.Text);
MinGray = int.Parse(textBox_MinGray.Text);
Algorithm.ImageHelp.Threshold(ho_image, out Region, MinGray, MaxGray);
//获得图片宽度和高度
HTuple width, height;
HOperatorSet.GetImageSize(ho_image, out width, out height);
//设置显示范围
HOperatorSet.SetPart(hWindowControl_Threshold.HalconWindow, 0, 0, height - 1, width - 1);
//显示图片
HOperatorSet.DispObj(Region, hWindowControl_Threshold.HalconWindow);
}
**********************************************************************************************************************************************************************************
private void button1_Click(object sender, EventArgs e)
{
string[] Color = new string[12] { "red", "green", "blue", "cyan", "magenta", "yellow", "coral", "spring green", "orange", "orange red", "pink", "navy" };
HObject ConnectionRegion;
HOperatorSet.Connection(Region, out ConnectionRegion);
HObject RRegion;
Algorithm.RectangleDef rectangleDef;
Algorithm.ImageHelp.GenRectangle(ConnectionRegion, out RRegion, out rectangleDef, true, hWindowControl_Threshold.HalconWindow);
HTuple nums;
HOperatorSet.CountObj(RRegion, out nums);
Algorithm.ImageHelp.HalconWindowShowImage(ho_image, RRegion, hWindowControl_Threshold.HalconWindow, Color);
string str = "齿轮的数量" + nums.ToString();
Algorithm.ImageHelp.ShowText(str, hWindowControl_Threshold.HalconWindow, 12, 12);
}
四、参数讲解
第一个参数的读取到的图片--->第二个参数是处理后输出的图片Region,因为Threshold算子中两个参数为灰度值的最大值和最小值,范围0-255,所以要设置两个Int类型的参数来接收
第一个参数的读取到的图片--->第二个参数是处理后输出的图片Region,第三个参数为显示的窗体句柄,第四个参数为connection后的物料单独上色
第一个参数的读取到的Region--->第二个参数是处理后输出的图片ExternalRegion,第三个参数为选用的绘制函数,第四个参数是是否需要填充,第五个参数为显示的窗体句柄
第一个参数是显示的文本内容,第二个参数是显示的窗体句柄,第三第四个参数是在哪个坐标下显示