示例#1
0
        //////////////////////////////////////////////////////////////////////////////////////////////

        #endregion

        #region 商家看板辨識
        //////////////////////////////////////////////////////////////////////////////////////////////
        public static SURFMatchedData MatchSURFFeature(SURFFeatureData template, Image <Bgr, Byte> observedImg, bool isShowResult)
        {
            //縮放到一樣大小 (系統修改成可讀圖片時才能加入)
            //observedImg = observedImg.Resize(3, INTER.CV_INTER_LINEAR);
            SURFFeatureData observed = SURFMatch.CalSURFFeature(observedImg);

            if (observed.GetDescriptors() != null)
            {
                SURFMatchedData matchedData = SURFMatch.MatchSURFFeatureByFLANNForObjs(template, observed);
                if (matchedData != null && isShowResult)
                {
                    SURFMatch.ShowSURFMatchForm(matchedData, observed, new ImageViewer());
                }
                return(matchedData);
            }
            else
            {
                return(null);
            }
        }
示例#2
0
        ////////////////////////////////////////////////////////////////////////////
        #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);
                //}
            }
        }