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++; } }
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); //先使用原影像 } }
//////////////////////////////////////////////////////////////////////////// /// <summary> /// 取出ROI物體圖像 /// </summary> /// <param name="filteredSkinObserved">過濾膚色的圖像</param> /// <returns>回傳物體圖像</returns> private Image <Bgr, Byte> GetObjectBoundingBoxImg(Image <Bgr, Byte> filteredSkinObserved) { //get bounding box Image <Gray, Byte> gray = filteredSkinObserved.Convert <Gray, Byte>(); gray = DetectObjects.DoBinaryThreshold(gray, 0); gray._Erode(5); gray._Dilate(5); new ImageViewer(gray).Show(); Contour <System.Drawing.Point> sceneContours = DetectObjects.DoContours(gray); Contour <System.Drawing.Point> objectBox = DetectObjects.GetMaxContours(sceneContours); Image <Bgr, byte> boxImg = DetectObjects.GetBoundingBoxImage(objectBox, filteredSkinObserved); new ImageViewer(boxImg).Show(); return(boxImg); }
/// <summary> /// 膚色過濾 /// </summary> /// <param name="observed">要對得觀察景象</param> /// <returns>回傳過濾掉的膚色</returns> private Image <Bgr, Byte> SkinFilter(Image <Bgr, Byte> observed) { Image <Bgr, Byte> dst = observed.Copy(); Image <Hsv, Byte> hsv = observed.Convert <Hsv, Byte>(); //皮膚偵測並填為黑 Image <Gray, Byte> skin = new Image <Gray, byte>(observed.Size); CvInvoke.cvInRangeS(hsv, new MCvScalar(0, 58, 89), new MCvScalar(25, 173, 229), skin); skin = FillHoles(skin); skin._Erode(2); skin._Dilate(2); Contour <System.Drawing.Point> skinContours = DetectObjects.DoContours(skin); Contour <System.Drawing.Point> max = DetectObjects.GetMaxContours(skinContours); //new ImageViewer(DetectObjects.DrawMaxContoursOnImg(max, dst)).Show(); dst.Draw(max, new Bgr(0, 0, 0), -1); //new ImageViewer(dst).Show(); return(dst); }
// Start is called before the first frame update void Start() { Weapons = new List <GameObject>(); Weapons.Add(PunchPrefab); collider = GetComponent <Collider>(); rigidbody = GetComponent <Rigidbody>(); audioSource = GetComponent <AudioSource>(); detectObjects = GetComponent <DetectObjects>(); if (animator == null) { Debug.LogError("Defina o animator do personagem"); } CharState = CharacterState.ALIVE; CharMode = CharacterMode.WALKING_MODE; _start(); }
// Start is called before the first frame update void Start() { //Subsrcibe to the RGB Image provider. RGB_Image_Provider imageSource = GetComponent <RGB_Image_Provider>(); imageSource.OnImageAvailableCallback += new RGB_Image_Provider.OnImageAvailableCallbackFunc(onImageAvailable); DetectObjects objectDetector = GetComponent <DetectObjects>(); objectDetector.onDetectionAvailableCallback += new DetectObjects.OnDetectionAvailableCallbackFunc(onDetectionAvailable); pointIds = new List <int>(); pointList = new List <Point>(); pointCache = new Dictionary <int, Point>(); detectionCache = new Dictionary <int, DetectionPoint>(); channel = new Channel(host + ":" + port, ChannelCredentials.Insecure); client = new GenesisSimulator.GenesisSimulatorClient(channel); cam = GetComponent <Camera>(); Screen.sleepTimeout = SleepTimeout.NeverSleep; }
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"); } } }
public void DetectObjects() { if (ProcessImage == null) return; // Hasn't applied any filters yet if (OutputImage != null) { OutputImage.Dispose(); // Reset output image OutputImage = new Bitmap(ProcessImage.Size.Width, ProcessImage.Size.Height); } else OutputImage = new Bitmap(ProcessImage.Size.Width, ProcessImage.Size.Height); IsBusy = true; System.Drawing.Color[,] OriginalColors = new System.Drawing.Color[ProcessImage.Size.Width, ProcessImage.Size.Height]; System.Drawing.Color[,] InputColors = new System.Drawing.Color[ProcessImage.Size.Width, ProcessImage.Size.Height]; System.Drawing.Color[,] OutputColors = null; for (int x = 0; x < ProcessImage.Size.Width; x++) for (int y = 0; y < ProcessImage.Size.Height; y++) InputColors[x, y] = ProcessImage.GetPixel(x, y); for (int x = 0; x < InputImage.Size.Width; x++) for (int y = 0; y < InputImage.Size.Height; y++) OriginalColors[x, y] = InputImage.GetPixel(x, y); ThreadPool.QueueUserWorkItem(o => { DetectObjects dobj = new DetectObjects(); dobj.detectObjects(InputColors); var listIObj = dobj.getDetectedObjects(); foreach (var iObj in listIObj) InputColors = iObj.FillObject(InputColors); dobj = new DetectObjects(); dobj.detectRugbyBalls(InputColors); listIObj = dobj.getDetectedBalls(); OutputColors = dobj.ColorTheBalls(OriginalColors); string message = String.Format("We detected {0} object{1} which consisted of {2} rugby ball{3}", dobj.getDetectedObjects().Count, dobj.getDetectedObjects().Count != 1 ? "s" : "", dobj.getDetectedBalls().Count, dobj.getDetectedBalls().Count != 1 ? "s" : ""); if (dobj.getDetectedBalls().Count > 0) message += "\nDetected balls can be seen with a red outline."; MessageBox.Show(message, "Detect rugby balls", MessageBoxButton.OK); /*foreach (var iObj in listIObj) // Should be only balls { if (OutputColors == null) OutputColors = iObj.ColorizeVectors(InputColors); else OutputColors = iObj.ColorizeVectors(OutputColors); }*/ Application.Current.Dispatcher.BeginInvoke(new Action(() => { for (int x = 0; x < InputImage.Size.Width; x++) for (int y = 0; y < InputImage.Size.Height; y++) OutputImage.SetPixel(x, y, OutputColors[x, y]); NewImage = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( // Display output image OutputImage.GetHbitmap(), IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(ProcessImage.Size.Width, ProcessImage.Size.Height)); IsBusy = false; })); }); }
public void DetectObjects() { if (ProcessImage == null) { return; // Hasn't applied any filters yet } if (OutputImage != null) { OutputImage.Dispose(); // Reset output image OutputImage = new Bitmap(ProcessImage.Size.Width, ProcessImage.Size.Height); } else { OutputImage = new Bitmap(ProcessImage.Size.Width, ProcessImage.Size.Height); } IsBusy = true; System.Drawing.Color[,] OriginalColors = new System.Drawing.Color[ProcessImage.Size.Width, ProcessImage.Size.Height]; System.Drawing.Color[,] InputColors = new System.Drawing.Color[ProcessImage.Size.Width, ProcessImage.Size.Height]; System.Drawing.Color[,] OutputColors = null; for (int x = 0; x < ProcessImage.Size.Width; x++) { for (int y = 0; y < ProcessImage.Size.Height; y++) { InputColors[x, y] = ProcessImage.GetPixel(x, y); } } for (int x = 0; x < InputImage.Size.Width; x++) { for (int y = 0; y < InputImage.Size.Height; y++) { OriginalColors[x, y] = InputImage.GetPixel(x, y); } } ThreadPool.QueueUserWorkItem(o => { DetectObjects dobj = new DetectObjects(); dobj.detectObjects(InputColors); var listIObj = dobj.getDetectedObjects(); foreach (var iObj in listIObj) { InputColors = iObj.FillObject(InputColors); } dobj = new DetectObjects(); dobj.detectRugbyBalls(InputColors); listIObj = dobj.getDetectedBalls(); OutputColors = dobj.ColorTheBalls(OriginalColors); string message = String.Format("We detected {0} object{1} which consisted of {2} rugby ball{3}", dobj.getDetectedObjects().Count, dobj.getDetectedObjects().Count != 1 ? "s" : "", dobj.getDetectedBalls().Count, dobj.getDetectedBalls().Count != 1 ? "s" : ""); if (dobj.getDetectedBalls().Count > 0) { message += "\nDetected balls can be seen with a red outline."; } MessageBox.Show(message, "Detect rugby balls", MessageBoxButton.OK); /*foreach (var iObj in listIObj) // Should be only balls * { * if (OutputColors == null) * OutputColors = iObj.ColorizeVectors(InputColors); * else * OutputColors = iObj.ColorizeVectors(OutputColors); * }*/ Application.Current.Dispatcher.BeginInvoke(new Action(() => { for (int x = 0; x < InputImage.Size.Width; x++) { for (int y = 0; y < InputImage.Size.Height; y++) { OutputImage.SetPixel(x, y, OutputColors[x, y]); } } NewImage = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( // Display output image OutputImage.GetHbitmap(), IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(ProcessImage.Size.Width, ProcessImage.Size.Height)); IsBusy = false; })); }); }
private void findTopThreeContourButton_Click(object sender, RoutedEventArgs e) { contoursImg = morphologyImg.Convert <Bgr, byte>(); topContours = DetectObjects.GetOrderMaxContours(morphologyImg); contoursImgBox.Image = DetectObjects.DrawContoursTopThreeBoundingBoxOnImg(topContours, contoursImg).Resize(320, 240, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR); }
private void drawContoursButton_Click(object sender, RoutedEventArgs e) { contoursImg = morphologyImg.Convert <Bgr, byte>(); contoursImgBox.Image = DetectObjects.DrawAllContoursOnImg(DetectObjects.DoContours(morphologyImg), contoursImg).Resize(320, 240, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR); }
private void dialteButton_Click(object sender, RoutedEventArgs e) { morphologyImg = DetectObjects.DoDilate(morphologyImg, Convert.ToInt32(dialteTextBox.Text)); morphologyImgBox.Image = morphologyImg.Resize(320, 240, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR); }
private void binaryThresButton_Click(object sender, RoutedEventArgs e) { binaryImg = DetectObjects.DoBinaryThreshold(backProjectImg, 200); morphologyImgBox.Image = binaryImg.Resize(320, 240, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR); }
private void backProjectButton_Click(object sender, RoutedEventArgs e) { backProjectImg = DetectObjects.DoBackProject(templateHist, loadTestImg); backProjectImgBox.Image = backProjectImg.Resize(320, 240, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR); }
//////////////////////////////////////////////////////////////////////////// #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); //} } }
private void Start() { _detectObjects = FindObjectOfType <DetectObjects>(); }