Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using Emgu.CV;
- using Emgu.CV.UI;
- using Emgu.CV.Structure;
- using System.Drawing;
- using System.Runtime.InteropServices;
- using System.Windows.Forms;
- namespace Vmouse
- {
- public class BackProject
- {
- public Image<Bgr, Byte> im;
- public Image<Hsv, Byte> hsv;
- public Image<Gray, Byte> mask, backproject;
- public Image<Gray, Byte>[] hsv_plane;
- public DenseHistogram hist;
- public RangeF[] range;
- public int[] hdims = { 30, 32 };
- public static int[] hranges_arr = { 0, 180 };
- public static int[] sranges_arr = { 0, 255 };
- public MCvScalar m1, m2;
- public IntPtr[] img;
- public StructuringElementEx rect_3;
- public StructuringElementEx rect_6;
- public BackProject(string filename)
- {
- range = new RangeF[2];
- range[0] = new RangeF(0, 180);
- range[1] = new RangeF(0, 255);
- int[] bin = { 16, 32 };
- hist = new DenseHistogram(bin, range);
- hsv_plane = new Image<Gray, Byte>[2];
- m1 = new MCvScalar(0, 30, 10, 0);
- m2 = new MCvScalar(180, 256, 256, 0);
- rect_3 = new StructuringElementEx(3, 3, 1, 1, Emgu.CV.CvEnum.CV_ELEMENT_SHAPE.CV_SHAPE_RECT);
- rect_6 = new StructuringElementEx(6, 6, 3, 3, Emgu.CV.CvEnum.CV_ELEMENT_SHAPE.CV_SHAPE_RECT);
- im = new Image<Bgr, Byte>(filename);
- hsv = im.Convert<Hsv, Byte>();
- mask = new Image<Gray, Byte>(im.Size);
- CvInvoke.cvInRangeS(hsv, m1, m2, mask);
- hsv_plane = hsv.Split();
- img = new IntPtr[2]
- {
- hsv_plane[0],hsv_plane[1]
- };
- Emgu.CV.CvInvoke.cvCalcHist(img, hist, false, mask);
- }
- public Image<Gray, Byte> findBackProject(Image<Bgr, Byte> frame,int method)
- {
- backproject = new Image<Gray, Byte>(frame.Size);
- im = frame.Copy();
- hsv = frame.Convert<Hsv, Byte>();
- hsv_plane = hsv.Split();
- img[0] = hsv_plane[0];
- img[1] = hsv_plane[1];
- Emgu.CV.CvInvoke.cvCalcBackProject(img, backproject, hist);
- // CvInvoke.cvAnd(this.backproject, this.mask, this.backproject, IntPtr.Zero);
- // CvInvoke.cvDilate(backproject, backproject, rect_3, 2);
- //CvInvoke.cvErode(backproject, backproject, rect_3, 2);
- backproject = backproject.InRange(new Gray(10), new Gray(256));
- if(method == 1 || method == 2)
- CvInvoke.cvDilate(backproject, backproject, rect_3, 2);
- if(method == 2)
- CvInvoke.cvErode(backproject, backproject, rect_3, 2);
- return backproject;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement