Advertisement
sau003

Untitled

Mar 20th, 2014
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using Emgu.CV;
  7. using Emgu.CV.UI;
  8. using Emgu.CV.Structure;
  9. using System.Drawing;
  10. using System.Runtime.InteropServices;
  11. using System.Windows.Forms;
  12.  
  13. namespace Vmouse
  14. {
  15.     public class BackProject
  16.     {
  17.         public Image<Bgr, Byte> im;
  18.         public Image<Hsv, Byte> hsv;
  19.         public Image<Gray, Byte> mask, backproject;
  20.         public Image<Gray, Byte>[] hsv_plane;
  21.         public DenseHistogram hist;
  22.         public RangeF[] range;
  23.         public int[] hdims = { 30, 32 };
  24.         public static int[] hranges_arr = { 0, 180 };
  25.         public static int[] sranges_arr = { 0, 255 };
  26.         public MCvScalar m1, m2;
  27.         public IntPtr[] img;
  28.         public StructuringElementEx rect_3;
  29.         public StructuringElementEx rect_6;
  30.  
  31.  
  32.         public BackProject(string filename)
  33.         {
  34.             range = new RangeF[2];
  35.             range[0] = new RangeF(0, 180);
  36.             range[1] = new RangeF(0, 255);
  37.             int[] bin = { 16, 32 };
  38.             hist = new DenseHistogram(bin, range);
  39.             hsv_plane = new Image<Gray, Byte>[2];
  40.             m1 = new MCvScalar(0, 30, 10, 0);
  41.             m2 = new MCvScalar(180, 256, 256, 0);
  42.             rect_3 = new StructuringElementEx(3, 3, 1, 1, Emgu.CV.CvEnum.CV_ELEMENT_SHAPE.CV_SHAPE_RECT);
  43.             rect_6 = new StructuringElementEx(6, 6, 3, 3, Emgu.CV.CvEnum.CV_ELEMENT_SHAPE.CV_SHAPE_RECT);
  44.  
  45.             im = new Image<Bgr, Byte>(filename);
  46.            
  47.             hsv = im.Convert<Hsv, Byte>();
  48.             mask = new Image<Gray, Byte>(im.Size);
  49.             CvInvoke.cvInRangeS(hsv, m1, m2, mask);
  50.             hsv_plane = hsv.Split();
  51.             img = new IntPtr[2]
  52.             {
  53.                 hsv_plane[0],hsv_plane[1]
  54.             };
  55.             Emgu.CV.CvInvoke.cvCalcHist(img, hist, false, mask);
  56.         }
  57.  
  58.         public Image<Gray, Byte> findBackProject(Image<Bgr, Byte> frame,int method)
  59.         {
  60.             backproject = new Image<Gray, Byte>(frame.Size);
  61.             im = frame.Copy();
  62.             hsv = frame.Convert<Hsv, Byte>();
  63.             hsv_plane = hsv.Split();
  64.             img[0] = hsv_plane[0];
  65.             img[1] = hsv_plane[1];
  66.             Emgu.CV.CvInvoke.cvCalcBackProject(img, backproject, hist);
  67.             // CvInvoke.cvAnd(this.backproject, this.mask, this.backproject, IntPtr.Zero);
  68.             // CvInvoke.cvDilate(backproject, backproject, rect_3, 2);
  69.             //CvInvoke.cvErode(backproject, backproject, rect_3, 2);
  70.             backproject = backproject.InRange(new Gray(10), new Gray(256));
  71.             if(method == 1 || method == 2)
  72.             CvInvoke.cvDilate(backproject, backproject, rect_3, 2);
  73.  
  74.             if(method == 2)
  75.             CvInvoke.cvErode(backproject, backproject, rect_3, 2);
  76.  
  77.             return backproject;
  78.  
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement