public void IsOverlappingTest( int min1, int max1, int min2, int max2, bool expectedResult ) { IntRange range1 = new IntRange( min1, max1 ); IntRange range2 = new IntRange( min2, max2 ); Assert.AreEqual<bool>( expectedResult, range1.IsOverlapping( range2 ) ); }
public void ToRangeTest( int iMin, int iMax, float fMin, float fMax ) { IntRange iRange = new IntRange( iMin, iMax ); Range range = iRange; Assert.AreEqual<float>( fMin, range.Min ); Assert.AreEqual<float>( fMax, range.Max ); }
public void EqualityOperatorTest( int min1, int max1, int min2, int max2, bool areEqual ) { IntRange range1 = new IntRange( min1, max1 ); IntRange range2 = new IntRange( min2, max2 ); Assert.AreEqual( range1.Equals( range2 ), areEqual ); Assert.AreEqual( range1 == range2, areEqual ); Assert.AreEqual( range1 != range2, !areEqual ); }
public void ScaleTest() { IntRange from = new IntRange(0, 100); IntRange to = new IntRange(0, 50); int x = 50; int expected = 25; int actual = Tools.Scale(from, to, x); Assert.AreEqual(expected, actual); }
public void AddColor(HSL selectedColor,IntRange Hue_range, DoubleRange Saturation_range, DoubleRange Luminance_range) { TreeNode node = new TreeNode(new String(' ',100)); node.Tag = new LayerColor(selectedColor, Hue_range, Saturation_range, Luminance_range); node.Checked = true; new_color_parent.Nodes.Add(node); new_color_parent.ExpandAll(); treeLayers.SelectedNode = node; UpdateFilter(); }
public LayerColor(HSL _color, IntRange _hue_range, DoubleRange _sat_range, DoubleRange _lum_range) { color = _color; hue_range = _hue_range; sat_range = _sat_range; lum_range = _lum_range; RGB rgb = new RGB(); AForge.Imaging.ColorConverter.HSL2RGB(color, rgb); RGBColor = rgb.Color; }
/// <summary> /// Limits the range of colors present on the given image. /// </summary> /// <param name="r">Range of R channel</param> /// <param name="g">Range of G channel</param> /// <param name="b">Range of B channel</param> /// <param name="image">Image to filter</param> public static void FilterColor(IntRange r, IntRange g, IntRange b, Bitmap image) { // create color filter ColorFiltering colorFilter = new ColorFiltering(); // configure the filter to keep red object only colorFilter.Red = r; colorFilter.Green = g; colorFilter.Blue = b; // filter image colorFilter.ApplyInPlace(image); }
private void init() { int[] binSizes = new int[] { 64, 64 }; //X bins per channel IntRange[] ranges = new IntRange[] { new IntRange(0, 180), //Hue (for 8bpp image hue range is (0-180) otherwise (0-360) new IntRange(0, 255) }; originalObjHist = new DenseHistogram(binSizes, ranges); backgroundHist = originalObjHist.CopyBlank(); }
public ColorPart() { //previousHSV = null; resizeFilter = new ResizeBilinear(128, 128); pixelateFilter = new Pixellate(); pixelateFilter.PixelWidth = 32; pixelateFilter.PixelHeight = 32; redChannelFilter = new ChannelFiltering(); greenChannelFilter = new ChannelFiltering(); blueChannelFilter = new ChannelFiltering(); intRange = new IntRange(0, 0); redChannelFilter.Green = intRange; redChannelFilter.Blue = intRange; greenChannelFilter.Red = intRange; greenChannelFilter.Blue = intRange; blueChannelFilter.Red = intRange; blueChannelFilter.Green = intRange; frameMatrices = new List<int[, ,]>(); }
private static void initalize(DenseHistogram hist, int[] binSizes, IntRange[] ranges) { hist.binSizes = binSizes; hist.ranges = ranges; var valueToIndexMultipliers = new float[binSizes.Length][]; /***********************************************************************************/ // binSizes[i] //idx = (val - range[i].Min) * ----------------------------------- // range[i].Max - range[i].Min + 1 for (int bin = 0; bin < binSizes.Length; bin++) { valueToIndexMultipliers[bin] = new float[2]; valueToIndexMultipliers[bin][0] = (float)binSizes[bin] / (ranges[bin].Max - ranges[bin].Min + 1); valueToIndexMultipliers[bin][1] = -ranges[bin].Min * valueToIndexMultipliers[bin][0]; } hist.valueToIndexMultipliers = valueToIndexMultipliers; /***********************************************************************************/ /***********************************************************************************/ var strides = new int[binSizes.Length]; strides[binSizes.Length - 1] = 1; for (int bin = (binSizes.Length - 1) - 1; bin >= 0; bin--) { strides[bin] = strides[bin + 1] * (binSizes[bin + 1]); } hist.strides = strides; /***********************************************************************************/ hist.NumberOfElements = binSizes.Aggregate((a, b) => a * b); hist.histogram = new float[hist.NumberOfElements]; hist.histogramHandle = GCHandle.Alloc(hist.histogram, GCHandleType.Pinned); hist.histPtr = (float*)hist.histogramHandle.AddrOfPinnedObject(); }
/// <summary> /// Set bounds for the specified servo. /// </summary> /// /// <param name="servo">Servo to set bounds for, [0, <see cref="Servos.Count"/>).</param> /// <param name="bound">Bounds to set for the specified servo.</param> /// /// <remarks><para>The method sets servo's physical bounds in which it may move. /// See documentation to <see cref="Qwerk.Servos"/> for clarification.</para></remarks> /// /// <exception cref="ArgumentOutOfRangeException">Invalid servo is specified.</exception> /// <exception cref="NotConnectedException">No connection to Qwerk or its service.</exception> /// <exception cref="ConnectionLostException">Connestion to Qwerk is lost.</exception> /// public void SetBound( int servo, IntRange bound ) { if ( ( servo < 0 ) || ( servo >= Count ) ) { throw new ArgumentOutOfRangeException( "Invalid servo is specified." ); } bool[] mask = new bool[Count]; IntRange[] bounds = new IntRange[Count]; mask[servo] = true; bounds[servo] = bound; SetBounds( mask, bounds ); }
public ColorFilter(int low, int up, ColorRangeType type) { Range = new IntRange(low, up); this.Type = type; }
/// <summary> /// Check if the specified range overlaps with this range /// </summary> /// /// <param name="range">Range to check for overlapping</param> /// /// <returns><b>True</b> if the specified range overlaps with this range or /// <b>false</b> otherwise.</returns> /// public bool IsOverlapping(IntRange range) { return((IsInside(range.min)) || (IsInside(range.max))); }
/// <summary> /// Check if the specified range is inside this range /// </summary> /// /// <param name="range">Range to check</param> /// /// <returns><b>True</b> if the specified range is inside this range or /// <b>false</b> otherwise.</returns> /// public bool IsInside(IntRange range) { return((IsInside(range.min)) && (IsInside(range.max))); }
/// <summary> /// Calculate filtering map. /// </summary> /// /// <param name="range">Filtering range.</param> /// <param name="fill">Fillter value.</param> /// <param name="fillOutsideRange">Fill outside or inside the range.</param> /// <param name="map">Filtering map.</param> /// private static void CalculateMap(IntRange range, byte fill, bool fillOutsideRange, byte[] map) { for (int i = 0; i < 256; i++) { if ((i >= range.Min) && (i <= range.Max)) { map[i] = (fillOutsideRange) ? (byte)i : fill; } else { map[i] = (fillOutsideRange) ? fill : (byte)i; } } }
/// <summary> /// This method scans an Image object for players and returns an <see cref="IProcessedImage"/> object. /// </summary> /// <param name="image">An image to process for blob objects.</param> /// <param name="players">A list of players to scan for.</param> /// <returns>An <see cref="IProcessedImage"/> object, which contains the image input and a list of players found.</returns> public IProcessedImage Analyze(Bitmap image, IEnumerable<IPlayer> players) { List<IPlayer> playerList= players.ToList(); List<IPlayerBlob> playerBlobs = new List<IPlayerBlob>(); List<IBlob> allBlobs = new List<IBlob>(); Bitmap imageTemp = (Bitmap) image.Clone(); BitmapData objectsData = imageTemp.LockBits(new Rectangle(0, 0, imageTemp.Width, imageTemp.Height), ImageLockMode.ReadOnly, imageTemp.PixelFormat); // Loop each player Parallel.For(0, playerList.Count, delegate(int i) { SuperHlsFilter hslFilter = new SuperHlsFilter { MultiThreaded = true }; IntRange hueRange = new IntRange(360, 0); DoubleRange satRange = new DoubleRange(1.0, 0.0); DoubleRange lumRange = new DoubleRange(1.0, 0.0); IPlayer player = playerList[i]; hueRange.Max = (int)Math.Min(player.ClothingColor.GetHue() + (HueTolerance / 2.0), 360.0); hueRange.Min = (int)Math.Max(player.ClothingColor.GetHue() - (HueTolerance / 2.0), 0); satRange.Max = Math.Min(player.ClothingColor.GetSaturation() + SaturationTolerance / 2, 1.0); satRange.Min = Math.Max(player.ClothingColor.GetSaturation() - SaturationTolerance / 2, 0.0); lumRange.Max = Math.Min(player.ClothingColor.GetBrightness() + BrightnessTolerance / 2, 1.0); lumRange.Min = Math.Max(player.ClothingColor.GetBrightness() - BrightnessTolerance / 2, 0.0); hslFilter.Hue = hueRange; hslFilter.Saturation = satRange; hslFilter.Luminance = lumRange; AForge.Imaging.Blob[] blobs; using (UnmanagedImage unmanagedImage = new UnmanagedImage(objectsData)) { // Apply the filter using (UnmanagedImage gsImage = hslFilter.Apply(unmanagedImage)) { // Extracts the array of found blobs from the BlobCounter blobs = FindBlobs(gsImage); } } // Converts raw blobs into IBlobs IList<IBlob> foundBlobs = (from blob in blobs orderby blob.Area descending select new Blob(blob, player.ClothingColor)).Cast<IBlob>().ToList(); IList<IBlob> mergedBlobs = MergeOverlappingBlobs(foundBlobs); mergedBlobs = (from blob in mergedBlobs orderby blob.Size descending select blob).ToList(); IBlob bestBlob = mergedBlobs.FirstOrDefault(); if (bestBlob != null) { lock (playerBlobs) { playerBlobs.Add(new PlayerBlob(bestBlob, player)); } lock (allBlobs) { allBlobs.AddRange(mergedBlobs); } } }); imageTemp.UnlockBits(objectsData); imageTemp.Dispose(); return new ProcessedImage(image, playerBlobs, allBlobs); }
public static void inkFinder() { Image<Hls, Byte> wrk = new Image<Hls, Byte>(img); Image<Gray, Byte>[] channels = wrk.Split(); Image<Gray, Byte> imgHue = channels[0]; Image<Gray, Byte> imgLig = channels[1]; Image<Gray, Byte> imgSat = channels[2]; int bl = 0; int wi = 0; int col = 0; ArrayList white = new ArrayList(); // HSL for white ones //ArrayList black = new ArrayList(); // HSL for black ones ArrayList other = new ArrayList(); // // collect for (int i = 0; i < wrk.Height; i++) { for (int j = 0; j < wrk.Width; j++) { int hue = (int)(imgHue[i, j].Intensity * 2); float sat = (float)(imgSat[i, j].Intensity/255); float lig = (float)(imgLig[i, j].Intensity/255); //double border = -Math.Sqrt((1 - (lig - 0.5f) * (lig - 0.5f) / 0.34 / 0.34) * 0.84 * 0.84) + 1; if (lig < 0.275 - 0.125 * sat || lig < 0.5 - 0.5 * sat / 0.6 || (sat < 0.18 && lig < 0.5)) { //black.Add(new HSL(hue, sat, lig)); bl++; continue; } if (lig > 0.725 + 0.125 * sat || lig > 0.5 + 0.5 * sat / 0.6 || (sat < 0.18 && lig >= 0.5)) { white.Add(new HSL(hue, sat, lig)); wi++; continue; } col++; addToOther(other, 5, hue, sat, lig); } Console.WriteLine("Row - " + i + " : clusters - " + other.Count); } Console.WriteLine(bl + " " + wi + " " + col); Console.WriteLine(other.Count); ArrayList majCol = (ArrayList)other[0]; // // find major color for (int t = 0; t < other.Count; t++) { if (majCol.Count < ((ArrayList)other[t]).Count) { majCol = (ArrayList)other[t]; } } // // find ranges HSL firstCol = (HSL)majCol[0]; filterHue = new IntRange(firstCol.Hue, firstCol.Hue); filterLig = new Range(firstCol.Luminance, firstCol.Luminance); filterSat = new Range(firstCol.Saturation, firstCol.Saturation); for (int q = 0; q < majCol.Count; q++) { HSL nextCol = (HSL)majCol[q]; if (nextCol.Hue < filterHue.Min) filterHue.Min = nextCol.Hue; if (nextCol.Hue > filterHue.Max) filterHue.Max = nextCol.Hue; if (nextCol.Luminance < filterLig.Min) filterLig.Min = nextCol.Luminance; if (nextCol.Luminance > filterLig.Max) filterLig.Max = nextCol.Luminance; if (nextCol.Saturation < filterSat.Min) filterSat.Min = nextCol.Saturation; if (nextCol.Saturation > filterSat.Max) filterSat.Max = nextCol.Saturation; } // // find fill color int huesum = 0; float satsum = 0; float ligsum = 0; for (int p = 0; p < white.Count; p++) { HSL wh = (HSL)white[p]; huesum += wh.Hue; satsum += wh.Saturation; ligsum += wh.Luminance; } fillColor = new HSL(huesum / white.Count, satsum / white.Count, ligsum / white.Count); }
public void SetColorRange(PictureBox pb, MouseEventArgs e, bool bgselect, Panel p) { try { // GET COLOR OF PIXEL Bitmap bitmap = new Bitmap((Bitmap)pb.Image); var loc = TranslateZoomMousePosition(e.Location, pb); var pixelcolor = bitmap.GetPixel(loc.X, loc.Y); if (bgselect) { // SET COLOR RANGE RangeRed = new IntRange(pixelcolor.R - 50, pixelcolor.R + 50); RangeGreen = new IntRange(pixelcolor.G - 50, pixelcolor.G + 50); RangeBlue = new IntRange(pixelcolor.B - 50, pixelcolor.B + 50); } else { ColonyColor = new RGB(pixelcolor); } p.BackColor = pixelcolor; // DISPOSE OF BITMAP bitmap.Dispose(); } catch (Exception) { } }
/// <summary> /// Check if the specified range is inside of the range. /// </summary> /// /// <param name="range">Range to check.</param> /// /// <returns><b>True</b> if the specified range is inside of the range or /// <b>false</b> otherwise.</returns> /// public bool IsInside( IntRange range ) { return ( ( IsInside( range.min ) ) && ( IsInside( range.max ) ) ); }
/// <summary> /// Initializes a new instance of the <see cref="HSLFiltering"/> class. /// </summary> /// /// <param name="hue">Range of hue component.</param> /// <param name="saturation">Range of saturation component.</param> /// <param name="luminance">Range of luminance component.</param> /// public HSLFiltering( IntRange hue, Range saturation, Range luminance ) : this( ) { this.hue = hue; this.saturation = saturation; this.luminance = luminance; }
/// <summary> /// Initializes a new instance of the <see cref="ColorFiltering"/> class. /// </summary> /// /// <param name="red">Red components filtering range.</param> /// <param name="green">Green components filtering range.</param> /// <param name="blue">Blue components filtering range.</param> /// public ColorFiltering( IntRange red, IntRange green, IntRange blue ) : this( ) { this.red = red; this.green = green; this.blue = blue; }
public bool IsInside(IntRange range) { return(this.IsInside(range.min) && this.IsInside(range.max)); }
// Selection changed in channels combo private void channelCombo_SelectedIndexChanged( object sender, System.EventArgs e ) { AForge.Math.Histogram h = null; Color color = Color.White; IntRange input = new IntRange( 0, 255 ); IntRange output = new IntRange( 0, 255 ); int index = channelCombo.SelectedIndex; if ( !imgStat.IsGrayscale ) { // RGB image histogram.Color = colors[index]; switch ( index ) { case 0: // red h = imgStat.Red; input = inRed; output = outRed; color = Color.FromArgb( 255, 0, 0 ); break; case 1: // green h = imgStat.Green; input = inGreen; output = outGreen; color = Color.FromArgb( 0, 255, 0 ); break; case 2: // blue h = imgStat.Blue; input = inBlue; output = outBlue; color = Color.FromArgb( 0, 0, 255 ); break; } } else { // grayscale image histogram.Color = colors[3]; h = imgStat.Gray; input = inGreen; output = outGreen; } histogram.Values = h.Values; inMinBox.Text = input.Min.ToString( ); inMaxBox.Text = input.Max.ToString( ); outMinBox.Text = output.Min.ToString( ); outMaxBox.Text = output.Max.ToString( ); // input slider inSlider.Color2 = color; inSlider.Min = input.Min; inSlider.Max = input.Max; // output slider outSlider.Color2 = color; outSlider.Min = output.Min; outSlider.Max = output.Max; }
/// <summary> /// Set bounds for specified servos. /// </summary> /// /// <param name="mask">Mask array specifying which servos need to be set.</param> /// <param name="bounds">Array of servos' bounds. Each bound may be in [0, 255] range.</param> /// /// <remarks><para>The method sets servos' physical bounds in which they may move. /// See documentation to <see cref="Qwerk.Servos"/> for clarification.</para></remarks> /// /// <exception cref="ArgumentException">Incorrect length of <paramref name="mask"/>, /// or <paramref name="bounds"/> array.</exception> /// <exception cref="NotConnectedException">No connection to Qwerk or its service.</exception> /// <exception cref="ConnectionLostException">Connestion to Qwerk is lost.</exception> /// public void SetBounds( bool[] mask, IntRange[] bounds ) { if ( ( mask.Length != Count ) || ( bounds.Length != Count ) ) { throw new ArgumentException( "Incorrect length of mask or positions array." ); } // check controller if ( servoController == null ) { throw new NotConnectedException( "Qwerk's service is not connected." ); } try { TeRKIceLib.Bounds[] nativeBounds = new TeRKIceLib.Bounds[Count]; for ( int i = 0; i < Count; i++ ) { if ( mask[i] ) { nativeBounds[i].min = bounds[i].Min; nativeBounds[i].max = bounds[i].Max; } } // set servos' bounds servoController.setBounds( mask, nativeBounds ); } catch { throw new ConnectionLostException( "Connection is lost." ); } }
/// <summary> /// Initializes a new instance of the <see cref="HeadController"/> class. /// </summary> /// public HeadController() { // Setup tracker tracker = new Camshift(); tracker.Mode = CamshiftMode.RGB; tracker.Conservative = true; tracker.AspectRatio = 1.5f; // Setup detector detector = new HaarObjectDetector(new FaceHaarCascade()); detector.MinSize = new Size(25, 25); detector.SearchMode = ObjectDetectorSearchMode.Single; detector.ScalingFactor = 1.2f; detector.ScalingMode = ObjectDetectorScalingMode.GreaterToSmaller; xaxisRange = new IntRange(0, 320); yaxisRange = new IntRange(0, 240); scaleRange = new DoubleRange(0, Math.Sqrt(320 * 240)); angleRange = new DoubleRange(0, 2 * Math.PI); }
/// <summary> /// Get bounds of all servos. /// </summary> /// /// <returns>Returns array of configured servos' bounds.</returns> /// /// <exception cref="NotConnectedException">No connection to Qwerk or its service.</exception> /// <exception cref="ConnectionLostException">Connestion to Qwerk is lost.</exception> /// public IntRange[] GetBounds( ) { // check controller if ( servoController == null ) { throw new NotConnectedException( "Qwerk's service is not connected." ); } try { // get servos' bounds TeRKIceLib.Bounds[] nativeBounds = servoController.getBounds( ); IntRange[] bounds = new IntRange[Count]; for ( int i = 0; i < Count; i++ ) { bounds[i] = new IntRange( nativeBounds[i].min, nativeBounds[i].max ); } return bounds; } catch { throw new ConnectionLostException( "Connection is lost." ); } }
/// <summary> /// Initializes a new instance of the <see cref="ChannelFiltering"/> class. /// </summary> /// /// <param name="red">Red channel's filtering range.</param> /// <param name="green">Green channel's filtering range.</param> /// <param name="blue">Blue channel's filtering range.</param> /// public ChannelFiltering(IntRange red, IntRange green, IntRange blue) { Red = red; Green = green; Blue = blue; formatTranslations[PixelFormat.Format24bppRgb] = PixelFormat.Format24bppRgb; formatTranslations[PixelFormat.Format32bppRgb] = PixelFormat.Format32bppRgb; formatTranslations[PixelFormat.Format32bppArgb] = PixelFormat.Format32bppArgb; }
/// <summary> /// Initializes a new instance of the <see cref="FrequencyFilter"/> class. /// </summary> /// /// <param name="frequencyRange">Range of frequencies to keep.</param> /// public FrequencyFilter(IntRange frequencyRange) { this.frequencyRange = frequencyRange; }
/// <summary> /// Calculate conversion map. /// </summary> /// /// <param name="inRange">Input range.</param> /// <param name="outRange">Output range.</param> /// <param name="map">Conversion map.</param> /// private void CalculateMap( IntRange inRange, IntRange outRange, ushort[] map ) { double k = 0, b = 0; if ( inRange.Max != inRange.Min ) { k = (double) ( outRange.Max - outRange.Min ) / (double) ( inRange.Max - inRange.Min ); b = (double) ( outRange.Min ) - k * inRange.Min; } for ( int i = 0; i < 65536; i++ ) { ushort v = (ushort) i; if ( v >= inRange.Max ) v = (ushort) outRange.Max; else if ( v <= inRange.Min ) v = (ushort) outRange.Min; else v = (ushort) ( k * v + b ); map[i] = v; } }
/// <summary> /// Check if the specified range overlaps with the range. /// </summary> /// /// <param name="range">Range to check for overlapping.</param> /// /// <returns><b>True</b> if the specified range overlaps with the range or /// <b>false</b> otherwise.</returns> /// public bool IsOverlapping( IntRange range ) { return ( ( IsInside( range.min ) ) || ( IsInside( range.max ) ) || ( range.IsInside( min ) ) || ( range.IsInside( max ) ) ); }
/// <summary> /// Constructs histogram. /// </summary> /// <param name="binSize">Bin size.</param> /// <param name="range">Image values range.</param> public DenseHistogram(int binSize, IntRange range) : this(new int[] { binSize }, new IntRange[] { range }) { }
/// <summary> /// Calculate conversion map. /// </summary> /// /// <param name="inRange">Input range.</param> /// <param name="outRange">Output range.</param> /// <param name="map">Conversion map.</param> /// private void CalculateMap( IntRange inRange, IntRange outRange, byte[] map ) { double k = 0, b = 0; if ( inRange.Max != inRange.Min ) { k = (double) ( outRange.Max - outRange.Min ) / (double) ( inRange.Max - inRange.Min ); b = (double) ( outRange.Min ) - k * inRange.Min; } for ( int i = 0; i < 256; i++ ) { byte v = (byte) i; if ( v >= inRange.Max ) v = (byte) outRange.Max; else if ( v <= inRange.Min ) v = (byte) outRange.Min; else v = (byte) ( k * v + b ); map[i] = v; } }
/// <summary> /// Constructs histogram. /// </summary> /// <param name="binSizes">Bin sizes.</param> /// <param name="ranges">Image values ranges.</param> public DenseHistogram(int[] binSizes, IntRange[] ranges) { initalize(this, binSizes, ranges); }