色婷婷狠狠18禁久久YY,CHINESE性内射高清国产,国产女人18毛片水真多1,国产AV在线观看

如何用LabVIEW做顏色識別

江奕云2年前21瀏覽0評論

如何用LabVIEW做顏色識別?

C#調用NI的庫函數實現顏色識別檢測(在halcon環境下)

一直使用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<double>(colorValue));

Collection<int> 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";

}

}

總結:

先在halcon窗口上劃定ROI區域,將此ROI轉換為LV中Roi類型,然后調用ColorInformation = Algorithms.LearnColor(image,roi,low,threshold)方法,該函數返回16行向量值 ColorInformation即為該區域的顏色分布

給定ROI區域(同樣在halcon中劃定并進行轉換),調用Algorithms.MatchColor(image, ColorInformation, roi)進行指定區域的顏色識別,該方法返回一個匹配分值

在給定image值時,一定要將其typeImage類型設定為RGB32