一直使用C#+halcon进行视觉算法的开发,但是遇到了一个非常普遍的需求,对物体进行颜色识别。在halcon中颜色识别主要分两种方式,一种为进行色域转化,由RGB转换为HSV后根据颜色表在H或者其他通道中对不同的颜色值进行区分,此种方式缺点是在进行建模时必须知道目标ROI的H通道值,且与其他ROI的值差别较大,不然非常容易误报。另一种方法即建立分类器,使用mlp或者gmm进行训练,然后将要识别的区域给分类器让其判断,这其中有一个缺点为,在建立分类器时必须知道当前有几种颜色,然后建立起对应输出的分类器,并且再有样本添加进入时也必须按同时将这几种颜色都加入进去(即使当前状态只有一种颜色出现差异需要再训练),同时,也不能再追加一种新的颜色。
在LabView的Vision模块中,有直接的颜色匹配模式,即将选定的ROI区域划分为16个向量再与检测的ROI作比较,识别较为准确。故本文介绍在C#环境下调用LV中的颜色识别函数,显示窗口依然使用halcon的HWindowControl(毕竟主要的开发算法还是在halcon下写的,并且个人感觉LV的图像显示窗口做的并不好,杂乱!)。
首先,调用LV需要先安装labview并且安装vision assistan模块,安装好后在其安装路径下有两个dll,分别为NationlInstryments.Vision.dll 和 NationlInstryments.Vision.Common.dll,同时引用halcondonet.dll(halcon的dll),找不到在哪的可以使用软件everything进行搜索。在自己的工程中引用这两个dll,同时引用namespace,添加halcon图像显示窗口,使用该文章中https://blog.csdn.net/qizijuesha/article/details/77400312的封装后的显示窗口 :
using NationalInstrumens.Vision
using NationInstruments.Vision.Analysis;
using HalconDotNet;
下面上代码:
private VisionImage myVisionImage = new VisionImage(); // VisionImage作为LV库函数中的图像输入
// 从本地读取图像
private void buttonReadImage_Click(object sender, EventArgs e)
{
ImagePreviewFileDialog imageDialog = new ImagePreviewFileDialog();
imageDialog.InitialDirectory = "D:\\";
imageDialog.Filter = "All Files(*.*)";
if (imageDialog.ShowDialog() == DialogResult.OK)
{
string imagePath = imageDialog.FileName;
LoadSelectedImage(imagePath); // 使用LV读取图像
}
}
private void LoadSelectedImage(string imagePath)
{
myVisionImage.ReadFile(imagePath);
myVisionImage.Type = ImageType.Rgb32; // 次句一定要加上,不然在进行识别时报错,默认读取进入后是U8单通道格式
}
在halcon窗口上进行roi的划定
private HObject GetModelDrawRegion(HObject drawImage, ref HTuple hv_Row1, ref HTuple hv_Column1, ref HTuple hv_Row2, ref HTuple hv_Column2)
{
HObject ho_ModelRegion, ho_TemplateImage, ho_RegionSelect, ho_RegionUnion, ho_RegionModel;
HObject ho_ModelContours, ho_TransContours = null;
HTuple hv_TempHomMat2D = new HTuple();
HTuple hv_HomMat = new HTuple();
// 初始化本地变量值
HOperatorSet.GenEmptyObj(out ho_ModelRegion);
HOperatorSet.GenEmptyObj(out ho_TemplateImage);
HOperatorSet.GenEmptyObj(out ho_ModelContours);
HOperatorSet.GenEmptyObj(out ho_TransContours);
HOperatorSet.GenEmptyObj(out ho_RegionSelect);
HOperatorSet.GenEmptyObj(out ho_RegionUnion);
HOperatorSet.GenEmptyObj(out ho_RegionModel);
try
{
HObject ho_temp_brush = new HObject();
hWindow_Final1.DrawModel = true; // 缩放功能禁用
HOperatorSet.SetSystem("border_shape_models", "false");
ho_ModelRegion.Dispose();
HalconToolClass.set_display_font(hWindow_Final1.hWindowControl.HalconWindow, 10, "mono", new HTuple("true"), new HTuple("false"));
HalconToolClass.disp_message(hWindow_Final1.hWindowControl.HalconWindow, "在窗口中将MARK1点位置框出,点击右键完成", "window", 20, 20, "red", "false");
hWindow_Final1.Focus();
HOperatorSet.SetColor(hWindow_Final1.hWindowControl.HalconWindow, "red");
HOperatorSet.DrawRectangle1(hWindow_Final1.hWindowControl.HalconWindow, out hv_Row1, out hv_Column1, out hv_Row2, out hv_Column2);
HOperatorSet.GenRectangle1(out ho_ModelRegion, hv_Row1, hv_Column1, hv_Row2, hv_Column2);
hWindow_Final1.DrawModel = false;
if (hv_Row1.D != 0)
{
brush_region.Dispose();
brush_region = ho_ModelRegion;
}
else
{
hWindow_Final1.HobjectToHimage(drawImage);
HalconToolClass.set_display_font(hWindow_Final1.hWindowControl.HalconWindow, 20, "mono", new HTuple("true"), new HTuple("false"));
HalconToolClass.disp_message(hWindow_Final1.hWindowControl.HalconWindow, "未画出有效区域", "window", 20, 20, "red", "false");
}
HalconToolClass.set_display_font(hWindow_Final1.hWindowControl.HalconWindow, 20, "mono", new HTuple("true"), new HTuple("false"));
hWindow_Final1.DispObj(ho_ModelRegion, "yellow");
ho_TemplateImage.Dispose();
HOperatorSet.ReduceDomain(drawImage, ho_ModelRegion, out ho_TemplateImage);
}
catch
{
MessageBox.Show("划定模板框出错!");
}
finally
{
ho_ModelRegion.Dispose();
}
return ho_TemplateImage;
}
划定好ROI后进行颜色的学习,并将学习完毕的颜色向量存入数据库
private void buttonRecColor_Click(object sender, EventArgs e)
{
HTuple hv_Row1 = null, hv_Column1 = null, hv_Row2 = null, hv_Column2 = null;
HObject ho_ModelRegion;
ho_ModelRegion = GetModelDrawRegion(halconImage, ref hv_Row1, ref hv_Column1, ref hv_Row2, ref hv_Column2);
double []lvRoi = ConvertHalconToLV(hv_Row1, hv_Column1, hv_Row2, hv_Column2); // 在halcon中矩形的存储为左上行列坐标,右下行列坐标;
// 而在LV中,矩形存储方式为中心行列坐标,weight和height长
// 查询插入语言
sqlCommand = "INSERT INTO roi_rec_inf(id, left_top_row, left_top_column, right_bottom_row, right_bottom_column) SELECT (SELECT MAX(id) FROM roi_rec_inf)+1, '" + hv_Row1 + "', '" + hv_Column1 + "', '" + hv_Row2 + "', '" + hv_Column2 + "';";
mySqlClass.UsualSqlCommand(sqlCommand);
RectangleContour rectangle = new RectangleContour(lvRoi[0], lvRoi[1], lvRoi[2], lvRoi[3]); // 矩形
Roi rectangleRoi = rectangle.ConvertToRoi();
// 该函数为调用的LV中学习颜色的函数,ROI使用halcon窗口中画出的ROI,若此时不存入数据库,也可直接使用colorInformation进行颜色识别
ColorInformation colorInformation = Algorithms.LearnColor(myVisionImage, rectangleRoi, ColorSensitivity.Low, (int)80);
sqlCommand = @"INSERT INTO color_match(
rec_id, color1, color2, color3, color4, color5, color6, color7, color8, color9, color10, color11, color12, color13, color14, color15, color16)
SELECT (SELECT MAX(id) from roi_rec_inf),
'" + colorInformation.Information[0] + "', '" + colorInformation.Information[1] + "', '" + colorInformation.Information[2] + "', '" + colorInformation.Information[3] + "', '" + colorInformation.Information[4] + "', '" + colorInformation.Information[5] + "', '" + colorInformation.Information[6] + "', '" + colorInformation.Information[7] + "', '" + colorInformation.Information[8] + "', '" + colorInformation.Information[9] + "', '" + colorInformation.Information[10] + "', '" + colorInformation.Information[11] + "', '" + colorInformation.Information[12] + "', '" + colorInformation.Information[13] + "', '" + colorInformation.Information[14] + "', '" + colorInformation.Information[15] + "'";
mySqlClass.UsualSqlCommand(sqlCommand); // 插入颜色数据
}
private double[] ConvertHalconToLV(HTuple hv_Row1, HTuple hv_Column1, HTuple hv_Row2, HTuple hv_Column2)
{
double width = 0, height = 0;
if (hv_Row2 > hv_Row1)
{
width = hv_Row2 - hv_Row1;
}
if (hv_Column2 > hv_Column1)
{
height = hv_Column2 - hv_Column1;
}
double[] lvRoi = { hv_Column1, hv_Row1, width, height };// 需要传出的左上横纵坐标及宽,长信息
return lvRoi;
}
现在进行图像颜色识别,给定要识别的ROI区域及对应的图像和之前保存的颜色向量,函数返回匹配分值
private void MatchColor(HObject imageMatch)
{
VisionImage myImage = new VisionImage();
myImage.Type = ImageType.Rgb32;
LoadSelectedImage("F:\\tempImage.jpeg", ref myImage);
double[] lvROI = ConvertHalconToLV(Convert.ToDouble(dtSelect.Rows[0]["left_top_row"].ToString()), Convert.ToDouble(dtSelect.Rows[0]["left_top_column"].ToString()), Convert.ToDouble(dtSelect.Rows[0]["right_bottom_row"].ToString()), Convert.ToDouble(dtSelect.Rows[0]["right_bottom_column"].ToString()));
Roi rectangleRoi = new Roi(new RectangleContour(lvROI[0], lvROI[1], lvROI[2], lvROI[3])); // 矩形
qlCommand = "SELECT color1, color2, color3, color4, color5, color6, color7, color8, color9, color10, color11, color12, color13, color14, color15, color16 FROM color_match WHERE rec_id = '" + Convert.ToInt32(dtSelect.Rows[0]["id"].ToString()) + "';";
DataTable dtColor = mySqlClass.SelectDataUsual(sqlCommand);
double []colorValue = DTConvertToDouble(dtColor);
ColorInformation myColorInformation = new ColorInformation(new Collection(colorValue));
Collection scores = Algorithms.MatchColor(myImage, myColorInformation, rectangleRoi);
if (scores[0] < 700)
{
DoNGSomething(Convert.ToInt32(dtSelect.Rows[0]["id"].ToString()));
richTextBox1.Text = "NG";
}
else
{
DoOKSomething(Convert.ToInt32(dtSelect.Rows[0]["id"].ToString()));
richTextBox1.Text = "OK";
}
}
总结: