public static Mat AnnotateImage(Mat orgImage, DnnDetectedObject[] detectedObjects) { if (detectedObjects == null) { throw new ArgumentNullException(nameof(detectedObjects)); } Mat result = new Mat(); Cv2.CopyTo(orgImage, result); foreach (var dObj in detectedObjects) { var x1 = dObj.BoundingBox.X.CropInRange(0, (double)result.Width); var y1 = dObj.BoundingBox.Y.CropInRange(0, (double)result.Height); var w = dObj.BoundingBox.Width.CropInRange(0, (double)result.Width - x1); var h = dObj.BoundingBox.Height.CropInRange(0, (double)result.Height - y1); var color = dObj.Color; var label = $"{dObj.Label} {dObj.Probability * 100:0.00}%"; var textSize = Cv2.GetTextSize(label, HersheyFonts.HersheyTriplex, 0.5, 1, out var baseline); var yLabelBase = y1.CropInRange(baseline, y1 - baseline); var yLabelTop = y1.CropInRange(0, y1 - textSize.Height - baseline); //draw result boundingbox result.Rectangle(new Point(x1, y1), new Point(x1 + w, y1 + h), color, 2); //draw result label on top of boundingbox Cv2.Rectangle(result, new Rect(new Point(x1, yLabelTop), new Size(textSize.Width, textSize.Height + baseline)), color, Cv2.FILLED); Cv2.PutText(result, label, new Point(x1, yLabelBase), HersheyFonts.HersheyTriplex, 0.5, Scalar.Black); } return(result); }
private Mat mergeImageLayer(Mat buttomLayer, Mat upperLayer) { Mat result = new Mat(350, 450, MatType.CV_8UC3, Scalar.LightGray); Cv2.CopyTo(upperLayer, result); for (int row = 0; row < buttomLayer.Rows; row++) { for (int col = 0; col < buttomLayer.Cols; col++) { Debug.Log(row + ": " + col); int blue = buttomLayer.At <Vec3b>(row, col).Item0; int green = buttomLayer.At <Vec3b>(row, col).Item1; int red = buttomLayer.At <Vec3b>(row, col).Item2; // RGB value if (blue == 255 && green == 255 && red == 255) // white { int blue2 = upperLayer.At <Vec3b>(row, col).Item0; int green2 = upperLayer.At <Vec3b>(row, col).Item1; int red2 = upperLayer.At <Vec3b>(row, col).Item2; if (blue2 == 211 && green2 == 211 && red2 == 211) // light-grey { result.Set <Vec3b>(row, col, new Vec3b(255, 255, 255)); } } else if (blue == 144 && green == 238 && red == 144) // light-green { result.Set <Vec3b>(row, col, new Vec3b(144, 238, 144)); } } } return(result); }
private Mat mergeImageLayer(Mat buttomLayer, Mat upperLayer) { //Cv2.ImWrite(Application.persistentDataPath + "/buttom.png", buttomLayer); Cv2.CopyTo(upperLayer, result); Parallel.For(0, buttomLayer.Rows, row => { for (int col = 0; col < buttomLayer.Cols; col++) { int blue = buttomLayer.At <Vec3b>(row, col).Item0; int green = buttomLayer.At <Vec3b>(row, col).Item1; int red = buttomLayer.At <Vec3b>(row, col).Item2; // RGB value if (blue == 255 && green == 255 && red == 255) // white { int blue2 = upperLayer.At <Vec3b>(row, col).Item0; int green2 = upperLayer.At <Vec3b>(row, col).Item1; int red2 = upperLayer.At <Vec3b>(row, col).Item2; if (blue2 == 211 && green2 == 211 && red2 == 211) // light-grey { result.Set <Vec3b>(row, col, new Vec3b(255, 255, 255)); } } else if (blue == 144 && green == 238 && red == 144) // light-green { int blue2 = upperLayer.At <Vec3b>(row, col).Item0; int green2 = upperLayer.At <Vec3b>(row, col).Item1; int red2 = upperLayer.At <Vec3b>(row, col).Item2; result.Set <Vec3b>(row, col, new Vec3b(144, 238, 144)); } } }); return(result); }
public void CopyTo() { using var src = Image("lenna.png"); var dst = new Mat(); Cv2.CopyTo(src, dst); ImageEquals(src, dst); }
public override void Apply(Mat input) { _start = DateTime.Now; Input = input; Mat hsv = new Mat(); Mat ycrcb = new Mat(); Mat hsv_mask = new Mat(input.Size(), MatType.CV_8UC1, Scalar.All(0)); Mat ycrcb_mask = new Mat(input.Size(), MatType.CV_8UC1, Scalar.All(0)); Mat global_mask = new Mat(input.Size(), MatType.CV_8UC1, Scalar.All(0)); //Cv2.CvtColor(hsv, hsv, ColorConversionCodes.BGR2HSV); //Cv2.CvtColor(ycrcb, ycrcb, ColorConversionCodes.BGR2HSV); if (_config.HSVThresholdConfig.IsActive) { Cv2.CopyTo(input, hsv); HSVFilter hsvFilter = new HSVFilter(); HSVThresoldFilter hsvThreshold = new HSVThresoldFilter(_config.HSVThresholdConfig); hsvFilter.Apply(hsv); hsvThreshold.Apply(hsvFilter.Output); Cv2.CopyTo(hsvThreshold.Output, hsv_mask); } else { hsv_mask = new Mat(input.Size(), MatType.CV_8UC1, Scalar.All(255)); } if (_config.YCrCbThresholdConfig.IsActive) { Cv2.CopyTo(input, ycrcb); YCrCbFilter ycrcbFilter = new YCrCbFilter(); YCrCbThreshold ycrcbthreshold = new YCrCbThreshold(_config.YCrCbThresholdConfig); ycrcbFilter.Apply(ycrcb); ycrcbthreshold.Apply(ycrcbFilter.Output); Cv2.CopyTo(ycrcbthreshold.Output, ycrcb_mask); } else { ycrcb_mask = new Mat(input.Size(), MatType.CV_8UC1, Scalar.All(255)); } Cv2.BitwiseAnd(hsv_mask, ycrcb_mask, global_mask); Cv2.CopyTo(global_mask, Output); //Cv2.CopyTo(input, Output, global_mask); //if (IsActive) //{ //OpenCvSharp.Cv2.InRange(input, new OpenCvSharp.Scalar(_config.HSVThresholdConfig.H.Min, _config.HSVThresholdConfig.S.Min, _config.HSVThresholdConfig.V.Min), new OpenCvSharp.Scalar(_config.HSVThresholdConfig.H.Max, _config.HSVThresholdConfig.S.Max, _config.HSVThresholdConfig.V.Max), Output); //} //else //{ // OpenCvSharp.Cv2.CopyTo(input, Output); //} base.Apply(input); }
/// <summary> /// i 番目の要素を削除。 /// </summary> /// <param name="i">削除位置</param> public void Erase(int i) { for (int n = i; n < this.Count - 1; ++n) { this[n] = this[n + 1]; Cv2.CopyTo(this.img[n + 1], this.img[n]); } this.bottom = (this.bottom - 1) & this.mask; }
/// <summary> /// 先頭に新しい要素を追加。 /// </summary> /// <param name="elem">追加する要素</param> public void InsertFirst(ref ImageData elem) { run_ave(elem); if (this.Count >= this.data.Length - 1) { this.Extend(); } this.top = (this.top - 1) & this.mask; this.data[this.top] = elem; Cv2.CopyTo(elem.img, this.img[this.top]); }
/// <summary> /// 末尾に新しい要素を追加。 /// </summary> /// <param name="elem">追加する要素</param> public void InsertLast(ImageData elem) { run_ave(elem); if (this.Count >= this.data.Length - 1) { this.Extend(); } this.data[this.bottom] = elem; this.bottom = (this.bottom + 1) & this.mask; Cv2.CopyTo(elem.img, this.img[this.bottom]); }
public async Task RunAsync(Mat input) { //_input = input; _input = new Mat(); FrameDate = DateTime.Now; Cv2.CopyTo(input, _input); OutputMat = new Mat(); //PipelineResult result = new PipelineResult(); try { await Task.Factory.StartNew(() => { List <IFilter> transforms = new List <IFilter>(); foreach (var step in Steps) { if (transforms.Count == 0) { step.Filter.Apply(InputMat); } else { step.Filter.Apply(transforms.Last().Output); } transforms.Add(step.Filter); } var resizeFilter = transforms.FirstOrDefault(x => x.FilterName == "resize"); if (resizeFilter != null) { Cv2.CopyTo(resizeFilter.Output, InputMat); } Cv2.CopyTo(transforms.Last().Output, OutputMat); //result.ResultSteps.Add(new PipelineResultStep(contourMat)); //result.ResultSteps.AddRange(transforms.Select(f => new PipelineResultStep(f))); }); } catch (AggregateException ae) { throw ae; } catch (Exception e) { throw e; } //return result; }
public void DrawTarget(ColorSkinCalibrationConfig config) { if (_useHeadDetection) { return; } if (_initialTarget != null && _initialTarget.Width > 0) { Rect handZone = new Rect(_config.ColorSkinCalibrationConfig.TLX, _config.ColorSkinCalibrationConfig.TLY, Math.Min(_config.ColorSkinCalibrationConfig.Width, MaxWidth), Math.Min(_config.ColorSkinCalibrationConfig.Height, MaxHeight)); Cv2.CopyTo(_initialTarget, _target); Cv2.Rectangle(_target, handZone, new Scalar(0, 255, 0)); RaisePropertyChanged(() => MaxWidth); RaisePropertyChanged(() => MaxHeight); W_HANDTARGET = _target.ToBitmap().ToBitmapImage(); } }
public void ActivateCalibration(Mat currentFrame, ColorSkinCalibrationConfig config = null) { _target = new Mat(); _initialTarget = new Mat(); Cv2.CopyTo(currentFrame, _target); Cv2.CopyTo(currentFrame, _initialTarget); if (config != null) { _config.ColorSkinCalibrationConfig = config; } CanStartCalibration = true; RaisePropertyChanged(() => MaxWidth); RaisePropertyChanged(() => MaxHeight); DrawTarget(_config.ColorSkinCalibrationConfig); }
public override void Apply(Mat input) { _start = DateTime.Now; Input = input; Cv2.CopyTo(Input, Output); if (IsActive) { Mat greyframe = new Mat(); Cv2.CvtColor(input, greyframe, ColorConversionCodes.BGR2GRAY); Input = input; var indexer_frame = Output.GetGenericIndexer <Vec3b>(); var indexer_frame_grey = greyframe.GetGenericIndexer <Vec3b>(); var indexer_bg = _background.GetGenericIndexer <Vec3b>(); for (int i = 0; i < Output.Rows; i++) { for (int j = 0; j < Output.Cols; j++) { var grey_frame = indexer_frame_grey[i, j]; var grey_bg = indexer_bg[i, j]; if (Math.Abs(grey_frame.Item0 - grey_bg.Item0) < _config.BackGroundThreshold) { //frame.Item0 = 0; indexer_frame[i, j] = new Vec3b(0, 0, 0); } else { //frame.Item0 = 255; //indexer_frame[i, j] = 255; } } } } else { OpenCvSharp.Cv2.CopyTo(input, Output); } base.Apply(input); }
private Mat PreprocessImage(Mat imageBuffer) { Mat publishedImage; if (RotateFlags.HasValue) { Mat rotImage = new Mat(); Cv2.Rotate(imageBuffer, rotImage, RotateFlags.Value); publishedImage = rotImage; } else { Mat cloneImage = new Mat(); Cv2.CopyTo(imageBuffer, cloneImage); publishedImage = cloneImage; } return(publishedImage); }
public override void Apply(Mat input) { _start = DateTime.Now; Input = input; Cv2.CopyTo(input, Output); if (IsActive) { Mat gray = new Mat(); Cv2.CvtColor(input, gray, ColorConversionCodes.BGR2GRAY); Rect[] faces = _haarCascade.DetectMultiScale(gray, _faceConfig.Scale, _faceConfig.MinNeighBours, HaarDetectionType.ScaleImage, new Size(_faceConfig.MinSize, _faceConfig.MinSize)); var mat_currentFrame = new Mat <Vec3b>(Output); var indexer_currentFrame = mat_currentFrame.GetIndexer(); foreach (var face in faces) { Cv2.Rectangle(Output, face, new Scalar(0, 0, 0), -1); } } base.Apply(input); }
public override void Apply(Mat input) { _start = DateTime.Now; Input = input; if (IsActive) { Mat equ = new Mat(); Cv2.CvtColor(input, equ, ColorConversionCodes.BGR2YCrCb); Mat[] Channels; Cv2.Split(equ, out Channels); Cv2.EqualizeHist(Channels[0], Channels[0]); Cv2.Merge(Channels, equ); Cv2.CvtColor(equ, Output, ColorConversionCodes.YCrCb2BGR); } else { Cv2.CopyTo(Input, Output); } base.Apply(input); }
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); } }
/// <summary> /// 画像を保存します。 /// </summary> /// <remarks> /// ビデオ書き込み /// </remarks> public void VideoWriterFrame() { if (vw == null || vw.IsDisposed /* || writer == null */) { return; } if (save_frame_count++ > save_frame_count_max) { save_frame_count = 0; VideoWriterRelease(); string fn = FileName + "_" + (++avi_id).ToString() + ".avi"; VideoWriterInit(fn); } // 画像内にデータ埋め込み vd.id = (ushort)this.data[this.bottom].id; vd.gx = this.data[this.bottom].gx; vd.gy = this.data[this.bottom].gy; vd.vmax = this.data[this.bottom].vmax; vd.msec = (ushort)this.data[this.bottom].t.Millisecond; vd.sec = (ushort)this.data[this.bottom].t.Second; vd.min = (ushort)this.data[this.bottom].t.Minute; vd.hour = (ushort)this.data[this.bottom].t.Hour; vd.day = (ushort)this.data[this.bottom].t.Day; vd.month = (ushort)this.data[this.bottom].t.Month; vd.year = (ushort)this.data[this.bottom].t.Year; vd.kgx = this.data[this.bottom].kgx; vd.kgy = this.data[this.bottom].kgy; vd.kvx = this.data[this.bottom].kvx; vd.kvy = this.data[this.bottom].kvy; vd.x2pos = this.data[this.bottom].udpkv1.x2pos; vd.y2pos = this.data[this.bottom].udpkv1.y2pos; vd.x2v = this.data[this.bottom].udpkv1.x2v; vd.y2v = this.data[this.bottom].udpkv1.y2v; vd.az2_c = this.data[this.bottom].udpkv1.az2_c; vd.alt2_c = this.data[this.bottom].udpkv1.alt2_c; vd.vaz2_kv = this.data[this.bottom].udpkv1.vaz2_kv; vd.valt2_kv = this.data[this.bottom].udpkv1.valt2_kv; if (MtMode == 2) { vd.x2pos = this.data[this.bottom].udpkv1.xpos; vd.y2pos = this.data[this.bottom].udpkv1.ypos; vd.x2v = this.data[this.bottom].udpkv1.x1v; vd.y2v = this.data[this.bottom].udpkv1.y1v; vd.az2_c = this.data[this.bottom].udpkv1.az1_c; vd.alt2_c = this.data[this.bottom].udpkv1.alt1_c; vd.vaz2_kv = this.data[this.bottom].udpkv1.vaz1_kv; vd.valt2_kv = this.data[this.bottom].udpkv1.valt1_kv; } vd.kvaz = this.data[this.bottom].udpkv1.kvaz; vd.kvalt = this.data[this.bottom].udpkv1.kvalt; vd.kv_status = this.data[this.bottom].udpkv1.kv_status; vd.data_request = this.data[this.bottom].udpkv1.data_request; vd.mt3mode = this.data[this.bottom].udpkv1.mt3mode; vd.az = this.data[this.bottom].az; vd.alt = this.data[this.bottom].alt; vd.vaz = this.data[this.bottom].vaz; vd.valt = this.data[this.bottom].valt; Cv2.CopyTo(this.img[this.bottom], imgR); //String filename = "FifoImg-" + vd.id + ".jpg"; //imgR.SaveImage(filename); //String str = String.Format("ID:{0,6:D1} ", this.data[this.bottom].id) + this.data[this.bottom].t.ToString("yyyyMMdd_HHmmss_fff") ; //imgR.PutText(str, new CvPoint(6, 14), font, new Scalar(0, 100, 120)); //Cv2.PutText(img, "English!!", new OpenCvSharp.Point(10, 180), HersheyFonts.HersheyComplexSmall, 1, new Scalar(255, 0, 255), 1, LineTypes.AntiAlias); Marshal.StructureToPtr(vd, imgR.Data, false); //filename = "FifoImg-" + vd.id + "vd.jpg"; //imgR.SaveImage(filename); //String str = String.Format("ID:{0,10:D1} ", this.data[this.bottom].id) + this.data[this.bottom].t.ToString("yyyyMMdd_HHmmss_fff") + String.Format(" ({0,6:F1},{1,6:F1})({2,6:F1})", gx, gy, vmax); //if (this.data[this.bottom].ImgSaveFlag) str += " True"; //Cv.CvtColor(this.img[this.bottom], imgGBR, ColorConversion.GrayToBgr); //3 Cv.Copy(this.img[this.bottom], imgGBR); //3 //imgGBR.PutText(str, new CvPoint(6, 14) , font, new Scalar(0, 100, 100)); //imgGBR.Circle(new CvPoint((int)(gx+0.5),(int)(gy+0.5)), 15, new Scalar(0, 100, 255)); vw.Write(imgR); //writer.WriteLine("{0} {1} {2} ", vd.id, vd.kgx, vd.kgy); int id = System.Threading.Thread.CurrentThread.ManagedThreadId; Console.WriteLine("RingBuf ThreadID : " + id); appendText += DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss ID:") + vd.id.ToString() + " " + this.data[this.bottom].timestamp.ToString() + Environment.NewLine; }