Exemple #1
0
        private void compareHistButton_Click(object sender, RoutedEventArgs e)
        {
            int i = 0;

            //值方圖匹配的參數預設
            histMatchRate = 1;
            matchIndex    = -1;
            ShowHistViewer(templateHistImgBox, showTemplateHistImg, "樣板影像");
            foreach (Contour <System.Drawing.Point> c in topContours)
            {
                if (i == 3)
                {
                    break;
                }
                DenseHistogram    observedRectHist;
                Image <Bgr, byte> observedContourRectImg = DetectObjects.GetBoundingBoxImage(c, loadTestImg);
                double            compareRate            = DetectObjects.CompareHistogram(templateHist, observedContourRectImg, out observedRectHist);
                if (compareRate < histMatchRate)
                {
                    histMatchRate = compareRate;
                    matchIndex    = i;
                }
                showObservedHistImg = SystemToolBox.DrawHsvHistogram(observedRectHist);
                ShowHistViewer(new ImageViewer(), showObservedHistImg, "觀察影像" + i);
                System.Windows.MessageBox.Show("compareRate is =" + compareRate.ToString());
                i++;
            }
        }
Exemple #2
0
 private void extractFeatureButton_Click(object sender, EventArgs e)
 {
     if (learningSys != null)
     {
         surfData = learningSys.CalSURFFeature();
         Image <Bgr, byte> drawKeyPointImg = SystemToolBox.DrawSURFFeature(surfData);
         new ImageViewer(SystemToolBox.DrawSURFFeatureToWPF(surfData, surfData.GetImg())).Show();
         extractFeatureImgBox.Image = drawKeyPointImg.Resize(320, 240, INTER.CV_INTER_LINEAR);
     }
 }
Exemple #3
0
 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();
         }
     }
 }
Exemple #4
0
        private void HsvHistButton_Click(object sender, EventArgs e)
        {
            int  HBins          = 50;
            int  SBins          = 0;
            int  VBins          = 0;
            bool isCorrectValue = false;

            if (HistDim == 1 && int.TryParse(HBinTextBox.Text, out HBins))
            {
                isCorrectValue = true;
            }
            else if (HistDim == 2 && int.TryParse(HBinTextBox.Text, out HBins) && int.TryParse(SBinTextBox.Text, out SBins))
            {
                isCorrectValue = true;
            }
            else
            {
                MessageBox.Show("數值不對");
            }
            if (wantExtractFeatureImage != null && isCorrectValue)
            {
                if (learningSys != null)
                {
                    learningSys.SetLearningImage(wantExtractFeatureImage);
                }
                else
                {
                    learningSys = new FeatureLearning(wantExtractFeatureImage);
                }

                templateHist = learningSys.CalHist(HistDim, HBins, SBins, VBins);
                if (HistDim <= 2)
                {
                    showTemplateHistImg = SystemToolBox.DrawHsvHistogram(templateHist);
                    ShowHistViewer(templateHistImgBox, showTemplateHistImg, "樣板影像");
                }
                else
                {
                    MessageBox.Show("Dim = " + HistDim + ",can;t draw");
                }
            }
            else
            {
                MessageBox.Show("圖片未載入");
            }
        }
Exemple #5
0
        private void extractFeatureButton_Click(object sender, EventArgs e)
        {
            if (wantExtractFeatureImage != null)
            {
                if (learningSys != null)
                {
                    learningSys.SetLearningImage(wantExtractFeatureImage);
                }
                else
                {
                    learningSys = new FeatureLearning(wantExtractFeatureImage);
                }

                surfData = learningSys.CalSURFFeature();
                //Draw Feature
                Image <Bgr, byte> drawKeyPointImg = SystemToolBox.DrawSURFFeature(surfData);
                new ImageViewer(drawKeyPointImg, "擷取特徵點結果").Show();
            }
        }
Exemple #6
0
        //////////////////////////////////////////////////////////////////////////////////////////////
        #endregion

        #region 讀值方圖檔
        //////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// 讀取直方圖(只支援一維與二維
        /// </summary>
        /// <param name="fileName">檔案路徑名稱</param>
        /// <param name="isShowHist">是否要顯示值方圖</param>
        /// <returns>回傳值方圖類別</returns>
        public static DenseHistogram ReadHistogram(string fileName, bool isShowHist)
        {
            string         format    = Path.GetExtension(fileName); //取得副檔名
            DenseHistogram histogram = FeatureDataFilesOperation.ReadHistogramDataFromBinaryXml(fileName);

            Console.WriteLine("Read Histogram Data in " + format + "........\n ");
            if (isShowHist)
            {
                if (histogram.Dimension == 1)
                {
                    SystemToolBox.Show1DHistogramDataOnConsole(histogram);
                }
                else if (histogram.Dimension == 2)
                {
                    SystemToolBox.Show2DHistogramDataOnConsole(histogram);
                }
            }
            Console.WriteLine("\n");
            return(histogram);
        }
Exemple #7
0
        private void loadHistFileButton_Click(object sender, RoutedEventArgs e)
        {
            string filename = OpenLearnedHistogramDataFile();

            if (filename != null)
            {
                templateHistFilePathName = filename;
                templateHist             = DetectObjects.ReadHistogram(templateHistFilePathName, true);

                if (templateHist.Dimension < 3)
                {
                    showTemplateHistImg = SystemToolBox.DrawHsvHistogram(templateHist);
                    ShowHistViewer(templateHistImgBox, showTemplateHistImg, "樣板影像");
                }
                else
                {
                    System.Windows.MessageBox.Show("Dim = " + templateHist.Dimension.ToString() + ",can;t draw");
                }
            }
        }
Exemple #8
0
 /// <summary>
 /// 儲存值方圖資料
 /// </summary>
 /// <param name="fileName">檔案路徑名稱</param>
 /// <param name="hsvHist">值方圖資料</param>
 /// <returns>回傳是否儲存成功</returns>
 public bool SaveHistogram(string fileName, DenseHistogram hsvHist)
 {
     if (hsvHist != null)
     {
         string format = Path.GetExtension(fileName); //取得副檔名
         if (format == ".xml")
         {
             FeatureDataFilesOperation.WriteHistogramDataToBinaryXml(hsvHist, fileName);
         }
         //Console Output觀看數值
         Console.WriteLine("Save Histogram Data in " + format + "........\n ");
         if (hsvHist.Dimension == 1)
         {
             SystemToolBox.Show1DHistogramDataOnConsole(hsvHist);
         }
         else if (hsvHist.Dimension == 2)
         {
             SystemToolBox.Show2DHistogramDataOnConsole(hsvHist);
         }
         Console.WriteLine("\n");
         return(true);
     }
     return(false);
 }
Exemple #9
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);
                //}
            }
        }