private void matchFeatureButton_Click(object sender, RoutedEventArgs e) { if (templateSurfFeature != null) { Image <Bgr, byte> observedContourRectImg = DetectObjects.GetBoundingBoxImage(topContours[matchIndex], loadTestImg); observedContourRectImg.Resize(templateSurfFeature.GetImg().Width, templateSurfFeature.GetImg().Height, INTER.CV_INTER_LINEAR); MatchRecognition.MatchSURFFeature(templateSurfFeature, observedContourRectImg, true); //先使用原影像 } }
private void getMappingFeatureButton_Click(object sender, RoutedEventArgs e) { if (templateHistFilePathName != null) { string templateHistFileName = System.IO.Path.GetFileName(templateHistFilePathName); //取得路徑的檔案名稱 templateSURFPathFileName = SystemToolBox.GetMappingDescriptorDataFile(templateHistFileName, dir); if (templateSURFPathFileName != null) { templateSurfFeature = MatchRecognition.ReadSURFFeature(templateSURFPathFileName); Image <Bgr, byte> showFeatureImg = SystemToolBox.DrawSURFFeature(templateSurfFeature); new ImageViewer(showFeatureImg, "取得樣板特徵圖像").Show(); } } }
//////////////////////////////////////////////////////////////////////////// #endregion /// <summary> /// 執行辨識 /// </summary> /// <param name="isDrawResultToShowOnDialog">是否要顯示出辨識的結果</param> /// <returns>回傳商品資訊, 格式=>"商品名稱,商品售價" ;請記得做字串切割,若無比對到或有任何問題則會回傳null</returns> public string RunRecognition(bool isDrawResultToShowOnDialog) { if (surfFiles.Count != 0) { //匹配特徵並取回匹配到的特徵 KeyValuePair <String, SURFMatchedData> mathedGoodsData = MatchRecognition.MatchSURFFeature(surfFiles, objectImg, true); if (mathedGoodsData.Key != null && mathedGoodsData.Value != null) { //透過樣板檔案名稱取出匹配到的商品資訊 string matchedFileName = mathedGoodsData.Key; //切割出商品檔案ID=> 特徵檔案名稱命名規則:(商品ID+特徵點編號),因為一種商品可能需要多張畫面的特徵點 string[] split = matchedFileName.Split('-'); string goodsMsg; //特徵檔案名稱使否有存在此商品 if (goodsData.TryGetValue(split[0], out goodsMsg)) { return(goodsMsg); } else { //System.Windows.MessageBox.Show("特徵檔案並無存在可對應的商品資料!"); return("Does not exist for this product!"); } } else { //System.Windows.MessageBox.Show("沒有對應到的商品或是不存在此商品"); return("Does not exist for this product"); } } else { // System.Windows.MessageBox.Show("沒有特徵資料"); return("Does not exist for this product"); } }
//////////////////////////////////////////////////////////////////////////// #endregion /// <summary> /// 執行辨識 /// </summary> /// <param name="isDrawResultToShowOnDialog">是否要顯示出辨識的結果</param> /// <returns>回傳看板資訊, 格式=>"看板名稱" ;請記得做字串切割,若無比對到或有任何問題則會回傳null</returns> public void RunRecognition(bool isDrawResultToShowOnDialog) { SURFMatchedData mathedObjectsData = null; string matchedFileName = null; Image <Bgr, byte> observedContourRectImg = null; if (surfFiles.Count != 0 && histFiles.Count != 0) { Stopwatch watch = Stopwatch.StartNew(); ////偵測物體 foreach (string histFilePath in histFiles) { //1.取出直方圖資料 DenseHistogram hist; string histFilename = System.IO.Path.GetFileName(histFilePath); if (!histDatas.ContainsKey(histFilename)) { hist = DetectObjects.ReadHistogram(histFilePath, false); histDatas.Add(histFilename, hist); } else { hist = histDatas[histFilename]; } //2.取出SURF資料 string templateHistFileName = System.IO.Path.GetFileName(histFilePath); //取得路徑的檔案名稱 string templateSURFPathFileName = SystemToolBox.GetMappingDescriptorDataFile(templateHistFileName, dir); SURFFeatureData templateSurf; string surfFilename = System.IO.Path.GetFileName(templateSURFPathFileName); if (!surfDatas.ContainsKey(surfFilename)) { templateSurf = MatchRecognition.ReadSURFFeature(templateSURFPathFileName); surfDatas.Add(surfFilename, templateSurf); } else { templateSurf = surfDatas[surfFilename]; } //3.做偵測 using (Image <Gray, byte> backProjectImg = DetectObjects.DoBackProject(hist, observedImg)) using (Image <Gray, byte> binaryImg = DetectObjects.DoBinaryThreshold(backProjectImg, 200)) { Image <Gray, byte> morphologyImg = DetectObjects.DoErode(binaryImg, 2); morphologyImg = DetectObjects.DoDilate(morphologyImg, 1); List <Contour <System.Drawing.Point> > topContours = DetectObjects.GetOrderMaxContours(morphologyImg); //new ImageViewer(DetectObjects.DrawContoursTopThreeBoundingBoxOnImg(topContours, observedImg.Copy())).Show(); int i = 0; double histMatchRate = 1; int matchIndex = -1; foreach (Contour <System.Drawing.Point> c in topContours) { if (i == 3) { break; } //判斷待偵測的輪廓面積是否過小,如果太小就省略 if (c.Area >= (templateSurf.GetImg().Width *templateSurf.GetImg().Height) * 0.4) { DenseHistogram observedRectHist; observedContourRectImg = DetectObjects.GetBoundingBoxImage(c, observedImg.Copy()); double compareRate = DetectObjects.CompareHistogram(hist, observedContourRectImg, out observedRectHist); if (compareRate < histMatchRate) { histMatchRate = compareRate; matchIndex = i; } observedRectHist.Dispose(); } i++; } if (histMatchRate < 0.5) { //影像正規化(如果觀察影像過大的話) // if (observedContourRectImg != null && observedContourRectImg.Height * observedContourRectImg.Width > templateSurf.GetImg().Width * templateSurf.GetImg().Height) //observedContourRectImg = observedContourRectImg.Resize(templateSurf.GetImg().Width, templateSurf.GetImg().Height, INTER.CV_INTER_LINEAR); //取出特徵 if (obervedSurfData == null && observedContourRectImg != null) { obervedSurfData = SURFMatch.CalSURFFeature(observedContourRectImg); observedContourRectImg.Dispose(); } //匹配特徵並取回匹配到的特徵 SURFMatchedData mathedCandidateData = MatchRecognition.MatchSURFFeatureForVideoObjs(templateSurf, obervedSurfData, null); //招出最好的特徵 if (mathedCandidateData != null && mathedCandidateData.GetHomography() != null) { if (mathedObjectsData == null) { mathedObjectsData = mathedCandidateData; matchedFileName = templateSURFPathFileName; } else if (mathedCandidateData.GetMatchedCount() > mathedObjectsData.GetMatchedCount() && mathedCandidateData.GetHomography() != null) { mathedObjectsData = mathedCandidateData; matchedFileName = templateSURFPathFileName; } } } morphologyImg.Dispose(); topContours.Clear(); if (mathedObjectsData != null && obervedSurfData != null) { SURFMatch.ShowSURFMatchForm(mathedObjectsData, obervedSurfData, viewer); } } } watch.Stop(); Console.WriteLine("File = " + System.IO.Path.GetFileName(matchedFileName) + " Video Analytics time = " + watch.ElapsedMilliseconds); //if (matchedFileName != null) //{ // string[] split = System.IO.Path.GetFileName(matchedFileName).Split('b'); // //voice.Speak("前方有" + split[0], SpeechVoiceSpeakFlags.SVSFlagsAsync); //} } }