private void Doit() { //we will repeatedly get an image, do an operation on it, then possibly //give it to someone else while (KeepRunning) { try { //System.Console.WriteLine("I am a thread!"); Emgu.CV.UMat image1 = GetNextImage(); //perhaps randomly clone() it and/or set it to null or change it's type Emgu.CV.UMat image2 = DoRandomHouseholding(image1); //do some image operation Emgu.CV.UMat image3 = OperateOnImage(image2); //give it to someone else? or dispose it? or set it to null? who knows! GetRidOfImage(image3); } catch (System.Threading.ThreadAbortException e) { //System.Console.WriteLine($"oops, I'm aborted!"); this.StackTrace = e.StackTrace; return; } catch (System.Exception e) { //maybe something about mismatch in types or so, normal. } //signal that we are alive ++Counter; } }
static public List <Emgu.CV.UMat> CreateThermalImages(List <Emgu.CV.Matrix <int> > intMatrices, double min, double max) { try { System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, 160, 120); if (intMatrices != null) { Emgu.CV.CvInvoke.UseOpenCL = false; List <Emgu.CV.UMat> ThermalImage = declareThermalImage(intMatrices.Count); Parallel.For(0, intMatrices.Count, i => { Emgu.CV.CvInvoke.UseOpenCL = false; Emgu.CV.UMat ColorImg = new Emgu.CV.UMat(); Emgu.CV.UMat UMatFloatImg; Emgu.CV.UMat UMatFloatImg2 = new Emgu.CV.UMat(); intMatrices[i] = (intMatrices[i] - min); UMatFloatImg = new Emgu.CV.UMat(intMatrices[i].Clone().ToUMat(), rect); UMatFloatImg.ConvertTo(UMatFloatImg, Emgu.CV.CvEnum.DepthType.Cv8U, (1 / max) * 255); Emgu.CV.CvInvoke.ApplyColorMap(UMatFloatImg, UMatFloatImg2, Emgu.CV.CvEnum.ColorMapType.Hot); ThermalImage[i] = UMatFloatImg2.Clone(); }); return(ThermalImage); } return(null); } catch (Exception) { return(null); } }
private Emgu.CV.UMat Do_RegionMask(Emgu.CV.UMat input) { int x = Random.Next(0, input.Size.Width - 1); int y = Random.Next(0, input.Size.Height - 1); int width = Random.Next(1, input.Size.Width - x); int height = Random.Next(1, input.Size.Height - y); System.Drawing.Rectangle roi = new System.Drawing.Rectangle(x, y, width, height); if (true) { System.Diagnostics.Contracts.Contract.Assert(0 <= roi.Top && roi.Top <= input.Size.Height); System.Diagnostics.Contracts.Contract.Assert(0 <= roi.Bottom && roi.Bottom <= input.Size.Height); System.Diagnostics.Contracts.Contract.Assert(0 <= roi.Left && roi.Left <= input.Size.Width); System.Diagnostics.Contracts.Contract.Assert(0 <= roi.Right && roi.Right <= input.Size.Width); } Emgu.CV.UMat cvResult = new Emgu.CV.UMat(input.Size, input.Depth, input.NumberOfChannels); using (var inputInsideRoi = new Emgu.CV.UMat(input, roi)) using (var resultInsideRoi = new Emgu.CV.UMat(cvResult, roi)) { cvResult.SetTo(new Emgu.CV.Structure.MCvScalar(0)); inputInsideRoi.CopyTo(resultInsideRoi); } input.Dispose(); return(cvResult); }
private Emgu.CV.UMat Do_Gaussian(Emgu.CV.UMat input) { double sigmax = 1 + Random.NextDouble() * 2; double sigmay = 1 + Random.NextDouble() * 2; var ksize = System.Drawing.Size.Empty; Emgu.CV.UMat output; bool inplace = GetInPlace(); if (inplace) { output = input; } else { output = new Emgu.CV.UMat(); } Emgu.CV.CvInvoke.GaussianBlur(input, output, ksize, sigmax, sigmay); if (!inplace) { input.Dispose(); } return(output); }
// thread safe wrt. Push internal Emgu.CV.UMat Pop() { Emgu.CV.UMat ret = null; if (ImageQueue.TryDequeue(out ret)) { return(ret); } return(null); }
private Emgu.CV.UMat MakeRandomImage() { int width = Random.Next(1, 2049); int height = Random.Next(1, 2049); var size = new System.Drawing.Size(width, height); var image = new Emgu.CV.UMat(size, MakeRandomFormat(), 1); image.SetTo(new Emgu.CV.Structure.MCvScalar(1)); return(image); }
private Emgu.CV.UMat Do_Padding(Emgu.CV.UMat input) { double sigmax = 1 + Random.NextDouble() * 2; int top = Random.Next(0, 5); int bottom = Random.Next(0, 5); int left = Random.Next(0, 5); int right = Random.Next(0, 5); var output = new Emgu.CV.UMat(); Emgu.CV.CvInvoke.CopyMakeBorder(input, output, top, bottom, left, right, Emgu.CV.CvEnum.BorderType.Reflect, new Emgu.CV.Structure.MCvScalar(Random.NextDouble())); input.Dispose(); return(output); }
// randomly make up an image, or pick one from incoming private Emgu.CV.UMat GetNextImage() { double imagesource = Random.NextDouble(); if (imagesource < ProbabilityOfMaking) { Emgu.CV.UMat image = MakeRandomImage(); return(image); } else { //take one from the input queue. return(Parent.Pop()); } }
// thread safe wrt. Pop internal void Push(Emgu.CV.UMat image) { if (image != null) { if (ImageQueue.Count < Nthreads_ * 2) { ImageQueue.Enqueue(image); } else { //we are reaching into memory limits, so dispose it to prevent //exhausting the ram image.Dispose(); } } }
private ThermalFile GenerateThermalFileWithBrokenImage(ThermalFile file) { ThermalFile brokenThermalFile = DeclareBadThermalFile(file); List <Emgu.CV.UMat> imgs = new List <Emgu.CV.UMat>(); Emgu.CV.UMat img = file.images[0]; imgs.Add(img); imgs.Add(img); for (int i = 2; i < file.count; i++) { imgs.Add(file.images[i]); } brokenThermalFile.images = new List <Emgu.CV.UMat>(imgs); return(brokenThermalFile); }
// randomly operate on the given image private Emgu.CV.UMat OperateOnImage(Emgu.CV.UMat input) { if (input == null) { return(null); } double selector = Random.NextDouble(); //pick out the first element that is not less than selector int chosen = 0; for (int i = 0; i < CumulativeProbabilityOfOperation.Length; ++i) { if (CumulativeProbabilityOfOperation[i] <= selector) { //still lower. } else { //this is it! chosen = i; break; } } switch (chosen) { case (int)Operation.GAUSSIAN: return(Do_Gaussian(input)); case (int)Operation.PADDING: return(Do_Padding(input)); case (int)Operation.CONVERTTO: return(Do_ConvertTo(input)); case (int)Operation.REGIONMASK: return(Do_RegionMask(input)); default: //weird. do nothing! return(input); } return(input); }
private Emgu.CV.UMat Do_ConvertTo(Emgu.CV.UMat input) { //since 0 and 1 are very common input to scaling, select them with much higher probability than other random values double scale = 1; double offset = 0; if (Random.Next(2) != 0) { scale = Random.NextDouble(); } if (Random.Next(2) != 0) { offset = Random.NextDouble(); } var output = new Emgu.CV.UMat(); input.ConvertTo(output, MakeRandomFormat(), scale, offset); input.Dispose(); return(output); }
// randomly do something with the image private Emgu.CV.UMat DoRandomHouseholding(Emgu.CV.UMat image) { if (image == null) { return(null); } double selector = Random.NextDouble(); if (selector < 0.1) { //the input image will be garbage collected - for the finalizer to eventuall reap. return(image.Clone()); } if (selector < 0.2) { image.Dispose(); return(null); } return(image); }
private void GetRidOfImage(Emgu.CV.UMat image) { if (image == null) { return; } double selector = Random.NextDouble(); if (selector < 0.85) { //give it to the parent, for someone else to use Parent.Push(image); return; } //dispose it. but only maybe. this is to trigger the //finalizer. if (Random.NextDouble() < 0.8) { image.Dispose(); } }
private void FrameProcessor_ImageProcessed(Emgu.CV.UMat source, Emgu.CV.UMat processed) { imageBoxSource.Image = source; imageBoxProcessed.Image = processed; }