void DrawHistogram(PictureBox window, CVHistogram histo, int channelIdx) { int imageWidth = window.Width; int imageHeight = window.Height; int bins = histo.BinSizes[0]; int binWidth = imageWidth / bins; if (binWidth <= 0) { binWidth = 1; } CVPair minMax = histo.MinMaxValue; CVImage outputImage = new CVImage(imageWidth, imageHeight, CVDepth.Depth8U, 3); outputImage.Zero(); for (int bin = 0; bin < bins; bin++) { double binValue = histo[bin]; byte level = (byte)CVUtils.Round(binValue * 255 / minMax.Second); byte binHeight = (byte)CVUtils.Round(binValue * imageHeight / minMax.Second); byte[] color = new byte[3]; color[channelIdx] = (byte)(((double)bin / (double)bins) * 255); byte[] markerColor = new byte[3]; markerColor[channelIdx] = level; Color colColor = Color.FromArgb(color[2], color[1], color[0]); Color colMarker = Color.FromArgb(markerColor[2], markerColor[1], markerColor[0]); outputImage.DrawRectangle(new Rectangle(bin * binWidth, imageHeight - binHeight, binWidth - 1, binHeight), colColor); outputImage.DrawRectangle(new Rectangle(bin * binWidth, imageHeight - binHeight, binWidth - 1, binHeight), colMarker); outputImage.DrawRectangle(new Rectangle(bin * binWidth, imageHeight - binHeight, 1, binHeight), colMarker); } window.Image = outputImage.ToBitmap(); outputImage.Release(); }
private void ParseBuffer() { //This is the tricky code that stores all data received //parses and stores it in a List of command value pairs try { if (serialPort1.IsOpen) { //Read everything the Arduino has sent so far incomeBuffer += serialPort1.ReadExisting(); } //Split string into c-v pairs //For example: "digital-HIGH&analog-512&led-HIGH&" //will be split in { "digital-HIGH", "analog-512", "led-HIGH", ""} string[] cvPairs = incomeBuffer.Split('&'); incomeBuffer = cvPairs[cvPairs.Length - 1]; for (int i = 0; i < (cvPairs.Length - 1); i++) { string[] cvPair = cvPairs[i].Split('-'); if (cvPair.Length == 2) { //If the pair has not exactly 2 elements //a command and a value, discard it CVPair pair = new CVPair(); pair.Command = cvPair[0]; pair.Value = cvPair[1]; dataQueue.Enqueue(pair); Console.WriteLine("Command Enqued: " + cvPairs[i]); } else { Console.WriteLine("Command Discarded: " + cvPairs[i]); } } } catch (Exception ex) { timer1.Stop(); timer1.Enabled = false; Console.WriteLine(ex); MessageBox.Show("Error reading data from the Arduino"); this.Close(); } }
public CVHistogram(int[] binSizes, CVPair[] binRanges) { CreateHist(binSizes, binRanges); }
protected void CreateHist(int[] binSizes, CVPair[] binRanges) { this._binSizes = binSizes; this._binRanges = binRanges; float[][] ranges = new float[binRanges.Length][]; // make sure ranges & sizes are of the same dimention. System.Diagnostics.Debug.Assert(binSizes.Length == binRanges.Length, "Ranges & sizes must be of the same dimention"); // create ranges array. for (int i = 0; i < binRanges.Length; ++i) { float[] range = new float[2]; range[0] = binRanges[i].First; range[1] = binRanges[i].Second; ranges[i] = range; } this.Internal = PInvoke.cvCreateHist(this._binSizes, (int)CVHistogramType.Array, ranges, true); CVUtils.CheckLastError(); }
public CVHistogram CalcHistogram(int[] binSizes, CVPair[] binRanges, CVImage mask) { CVHistogram h = new CVHistogram(binSizes, binRanges); __IplImagePtr[] images = new __IplImagePtr[this.Channels]; if (this.Channels == 1) { images[0] = this.Internal; } else { CVImage[] planes = this.Split(); for (int i = 0; i < planes.Length; ++i) { images[i] = planes[i].Internal; } } __CvArrPtr maskArr = IntPtr.Zero; if (mask != null) maskArr = mask.Array; PInvoke.cvCalcHist(images, h.Internal, 0, maskArr); CVUtils.CheckLastError(); return h; }
public CVHistogram CalcHistogram(int[] binSizes, CVPair[] binRanges) { return CalcHistogram(binSizes, binRanges, null); }
public CVHistogram CalcHistogram(int binsSize, CVImage mask) { Int32[] binSizes = new Int32[3]; CVPair[] binRanges = new CVPair[3]; binSizes[0] = binSizes[1] = binSizes[2] = binsSize; binRanges[0] = binRanges[1] = binRanges[2] = new CVPair(0, 255); return CalcHistogram(binSizes, binRanges, mask); }