public async Task StartCalibration(ColorSkinCalibrationConfig config = null) { _samples.Clear(); if (config != null) { _config.ColorSkinCalibrationConfig = config; } _calibrationStartDate = DateTime.Now; if (!_useHeadDetection) { _config.BackGroundRemoveConfig.IsActive = false; } Rect handZone = new Rect(_config.ColorSkinCalibrationConfig.TLX, _config.ColorSkinCalibrationConfig.TLY, Math.Min(_config.ColorSkinCalibrationConfig.Width, MaxWidth), Math.Min(_config.ColorSkinCalibrationConfig.Height, MaxHeight)); var tmp = new Mat(_target, handZone); var pipelineBackGround = new Pipeline() .Pipe(new BlurFilter(_config.BlurConfig)); await pipelineBackGround.RunAsync(tmp); Cv2.CvtColor(pipelineBackGround.OutputMat, _wbackground, ColorConversionCodes.BGR2GRAY); CalibrationStarted = true; Result = new SkinPresetHSV(); }
public ColorSkinCalibrationViewModel(Config config) { _config = config; _samples = new List <ColorSkinCalibrationSample>(); _wbackground = new Mat(); _wSubstractBackground = new Mat(); _whsv = new Mat(); _wycrcb = new Mat(); _whisto_h = new Mat(); _whisto_s = new Mat(); _whisto_v = new Mat(); _wcropped = new Mat(); Result = new SkinPresetHSV(); ColorSkinCalibrationConfig.PropertyChanged += Config_PropertyChanged; _haarCascade = new CascadeClassifier(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "haarcascade_frontalface_alt2.xml")); }
public async Task AddSample(Mat frame) { if (RemainingSeconds > 0) { Rect roi = new Rect(); if (frame.Width > 0) { _target = new Mat(); Cv2.CopyTo(frame, _target); if (_useHeadDetection) { Mat gray = new Mat(); Cv2.CvtColor(frame, gray, ColorConversionCodes.BGR2GRAY); Rect[] faces = _haarCascade.DetectMultiScale(gray, _config.FaceConfig.Scale, _config.FaceConfig.MinNeighBours, HaarDetectionType.ScaleImage, new Size(_config.FaceConfig.MinSize, _config.FaceConfig.MinSize)); if (faces.Count() > 0) { var maxArea = faces.Max(x => x.Width * x.Height); roi = faces.FirstOrDefault(x => x.Width * x.Height == maxArea); } } else { roi = new Rect(_config.ColorSkinCalibrationConfig.TLX, _config.ColorSkinCalibrationConfig.TLY, Math.Min(_config.ColorSkinCalibrationConfig.Width, MaxWidth), Math.Min(_config.ColorSkinCalibrationConfig.Height, MaxHeight)); } if (roi != null && roi.Width > 0) { _wcropped = new Mat(frame, roi); var pipeline = new Pipeline() .Pipe(new BlurFilter(_config.BlurConfig)) .Pipe(new BackgroundRemoveFilter(_wbackground, _config.BackGroundRemoveConfig)); await pipeline.RunAsync(_wcropped); _wSubstractBackground = pipeline.OutputMat; HSVFilter hsvFilter = new HSVFilter(); hsvFilter.Apply(_wSubstractBackground); _whsv = hsvFilter.Output; YCrCbFilter ycrcbFilter = new YCrCbFilter(); ycrcbFilter.Apply(_wSubstractBackground); _wycrcb = ycrcbFilter.Output; var sample = ColorSkinCalibrationSample.From(_wSubstractBackground, _whsv, _wycrcb); _samples.Add(sample); _whisto_h = GetHistogramGraph(sample.HSV.Histogram_H, _wcropped.Width, _wcropped.Height); _whisto_s = GetHistogramGraph(sample.HSV.Histogram_S, _wcropped.Width, _wcropped.Height); _whisto_v = GetHistogramGraph(sample.HSV.Histogram_V, _wcropped.Width, _wcropped.Height); Result = new SkinPresetHSV { HSV = new HSV { H = (int)_samples.Select(x => (double)x.HSV.MeanH.X).Median(), S = (int)_samples.Select(x => (double)x.HSV.MeanS.X).Median(), V = (int)_samples.Select(x => (double)x.HSV.MeanV.X).Median() }, YCRCB = new YCrCb { Y = (int)_samples.Select(x => (double)x.YCRCB.MeanY.X).Median(), Cr = (int)_samples.Select(x => (double)x.YCRCB.MeanCr.X).Median(), Cb = (int)_samples.Select(x => (double)x.YCRCB.MeanCb.X).Median() } }; RaisePropertyChanged(() => W_CROPPED); RaisePropertyChanged(() => W_BACKGROUND_SUBSTRACTED); RaisePropertyChanged(() => W_HSV); RaisePropertyChanged(() => W_HISTO_H); RaisePropertyChanged(() => W_HISTO_S); RaisePropertyChanged(() => W_HISTO_V); RaisePropertyChanged(() => W_HANDTARGET); } RaisePropertyChanged(() => RemainingSeconds); } } else { Result = new SkinPresetHSV { HSV = new HSV { H = (int)_samples.Select(x => (double)x.HSV.MeanH.X).Median(), S = (int)_samples.Select(x => (double)x.HSV.MeanS.X).Median(), V = (int)_samples.Select(x => (double)x.HSV.MeanV.X).Median() }, YCRCB = new YCrCb { Y = (int)_samples.Select(x => (double)x.YCRCB.MeanY.X).Median(), Cr = (int)_samples.Select(x => (double)x.YCRCB.MeanCr.X).Median(), Cb = (int)_samples.Select(x => (double)x.YCRCB.MeanCb.X).Median() } }; CalibrationStarted = false; CanStartCalibration = false; W_HANDTARGET = null; RaisePropertyChanged(() => W_HANDTARGET); } }