/// <summary> /// Initializes the variables. /// </summary> private void InitVariables() { numOfScannersX = _gridSizeX * _gridSize; numOfScannersY = _gridSizeY * _gridSize; scannersList = new GameObject[numOfScannersX, numOfScannersY]; allColors = new Color[numOfScannersX * numOfScannersY]; currentIds = new int[numOfScannersX / _gridSize, numOfScannersY / _gridSize]; colorClassifier = new ColorClassifier(); idBuffer = new Queue <int> [numOfScannersX * numOfScannersY]; MakeScanners(); SetupSampleObjects(); // Create UX scanners dock = new Dock(this.gameObject, _gridSize, _scannerScale); slider = new LegoSlider(this.gameObject, _scannerScale, _sliderRange); // Original keystoned object with webcam texture / video cameraKeystonedQuad = GameObject.Find("CameraKeystoneQuad"); // Copy mesh with RenderTexture keystonedQuad = GameObject.Find(colorTexturedQuadName); LoadScannerSettings(); EventManager.TriggerEvent("scannersInitialized"); }
public RecgnizationForm() { InitializeComponent(); _IImageSource = new ImageSets.ImageSource(); imageSourceManager1.ImageSource = _IImageSource; imageSourceManager1.TrainingImageDoubleClicked += imageSourceManager1_TrainingImageDoubleClicked; imageSourceManager1.RecgonizingImageDoubleClicked += imageSourceManager1_RecgonizingImageDoubleClicked; _Builder = new BitmapSetsBuilder(); _ColorClassification = new ColorClassifier(); _ColorClassification.KnownColors = new Color[1] { Color.Red }; _ColorClassification.KnownValues = new double[1] { 1 }; _ColorClassification.Update(); _RecognizerFactory = new RecognizerFactory(_Builder, _ColorClassification); _RecognizerFactory.Initialize(); cmb_Models.ComboBox.DisplayMember = "Name"; cmb_Models.ComboBox.DataSource = _RecognizerFactory.Models; cmb_Models.SelectedIndex = 1; }
public CellPresenter(ColorClassifier value) { MyColor = new SolidColorBrush(value.MyColor.ToMediaColor()); Count = value.Count.ToString(); IsConnected = value.IsConnected; IsDone = value.IsDone; }
public static bool IsSolved(this Color[] arr, ColorClassifier color) { if (color.IsConnected) { var first = arr.IndexOf(color.MyColor); if (first == OutOfBoundsConst || first + color.Count - 1 > arr.Length) { return(false); } for (int i = first; i <= first + color.Count - 1; i++) { if (arr[i] != color.MyColor) { return(false); } } return(true); } else { var count = arr.Count(c => c == color.MyColor); if (count != color.Count) { return(false); } var first = arr.IndexOf(color.MyColor); var last = arr.LastIndexOf(color.MyColor); if (last - first <= color.Count) { return(false); } return(true); } }
/// <summary> /// Returns the distance from the target color histogram the given pixel array is /// </summary> /// <param name="pixelArray">The pixel array of the bitmap to get the distance of</param> /// <param name="bitmapWidth">The width of the bitmap</param> /// <param name="bitmapHeight">The height of the bitmap</param> /// <param name="stride">The number of bytes per row</param> /// <param name="targetColor">The histogram of the color of the target object</param> /// <returns>The minimum distance a subrect of the image is from the target histogram</returns> private static double GetColorDistance(byte[] pixelArray, int bitmapWidth, int bitmapHeight, int stride, double[] targetColor) { var colorDistancePixelArray = (byte[])pixelArray.Clone(); var pixelArrayLock = new object(); // Threshold the image for possible areas where the object is Parallel.ForEach(ExtensionMethods.SteppedRange(0, bitmapWidth, 8), column => { Parallel.ForEach(ExtensionMethods.SteppedRange(0, bitmapHeight, 8), row => { int width = Math.Min(8, bitmapWidth - column); int height = Math.Min(8, bitmapHeight - row); var croppedPixelArray = pixelArray.CropPixelArray(column, row, width, height, stride); var colorBins = ColorClassifier.GetColorBins(croppedPixelArray, true); var distance = ColorClassifier.CalculateBinDistance(colorBins, targetColor); // Possible areas where the object we are looking for is are black 0 byte newColor = distance >= 125 ? (byte)255 : (byte)0; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int index = (row + j) * stride + 4 * (column + i); lock ( pixelArrayLock ) { colorDistancePixelArray[index] = colorDistancePixelArray[index + 1] = colorDistancePixelArray[index + 2] = newColor; } } } }); }); // Look at each blob and determine if it is our // object with a stricted threshold var tempBitmap = new WriteableBitmap(bitmapWidth, bitmapHeight, 96, 96, PixelFormats.Bgr32, null); tempBitmap.WritePixels(new Int32Rect(0, 0, bitmapWidth, bitmapHeight), colorDistancePixelArray, stride, 0); double minDistance = double.PositiveInfinity; var allBlobDistance = GetColorBinDistance(pixelArray, colorDistancePixelArray, stride, targetColor, tempBitmap, Colors.Black); if (allBlobDistance <= 105) { minDistance = allBlobDistance; } var blobColors = BitmapColorer.ColorBitmap(tempBitmap); foreach (var color in blobColors) { var distance = GetColorBinDistance(pixelArray, colorDistancePixelArray, stride, targetColor, tempBitmap, color); if (distance <= minDistance) { minDistance = distance; } } return(minDistance); }
private static double GetColorBinDistance(byte[] pixelArray, byte[] thresholdedPixelArray, int stride, double[] targetHistogram, WriteableBitmap tempBitmap, Color color) { var boundingBox = BitmapColorer.GetBoundingBoxOfColor(tempBitmap, color); var croppedPixelArray = pixelArray.CropPixelArray((int)boundingBox.X, (int)boundingBox.Y, (int)boundingBox.Width, (int)boundingBox.Height, stride); var croppedThresholdedArray = thresholdedPixelArray.CropPixelArray((int)boundingBox.X, (int)boundingBox.Y, (int)boundingBox.Width, (int)boundingBox.Height, stride); var colorBins = ColorClassifier.GetColorBinsWithinBlob(croppedPixelArray, croppedThresholdedArray, true); var distance = ColorClassifier.CalculateBinDistance(colorBins, targetHistogram); return(distance); }
public int UpdateColor() { RaycastHit hit; hitTex = SingletonTMono <Scanners> .Instance.hitTex; colorClassifier = SingletonTMono <Scanners> .Instance.colorClassifier; if (Physics.Raycast(transform.position, Vector3.down, out hit, 6, 1 << 9)) { // Get local tex coords w.r.t. triangle if (!hitTex) { Debug.Log("No hit texture"); if (needUpdateColor) { GetComponent <Renderer>().material.color = Color.magenta; } return(-1); } else { int _locX = Mathf.RoundToInt(hit.textureCoord.x * hitTex.width); int _locY = Mathf.RoundToInt(hit.textureCoord.y * hitTex.height); Color pixel = hitTex.GetPixel(_locX, _locY); int currID = colorClassifier.GetClosestColorId(pixel); //if (isGrid) //{ // if (_useBuffer) // currID = GetIdAverage(i, j, currID); // // Save colors for 3D visualization // if (setup || _isCalibrating) // allColors[i + numOfScannersX * j] = pixel; //} Color minColor; // Display 3D colors & use scanned colors for scanner color //if (SingletonTMono<Scanners>.Instance._isCalibrating && isGrid) if (SingletonTMono <Scanners> .Instance._isCalibrating) { minColor = pixel; } else { minColor = colorClassifier.GetColor(currID); } if (SingletonTMono <Scanners> .Instance._showDebugLines) { // Could improve by drawing only if sphere locations change Vector3 origin = SingletonTMono <Scanners> .Instance._colorSpaceParent.transform.position; Debug.DrawLine(origin + new Vector3(pixel.r, pixel.g, pixel.b), origin + new Vector3(SingletonTMono <Scanners> .Instance.sampleColors[currID].r, SingletonTMono <Scanners> .Instance.sampleColors[currID].g, SingletonTMono <Scanners> .Instance.sampleColors[currID].b), pixel, 1, false); } // Display rays cast at the keystoned quad if (SingletonTMono <Scanners> .Instance._showRays) { Debug.DrawLine(transform.position, hit.point, pixel, 200, false); Debug.Log(hit.point); } if (needUpdateColor) { GetComponent <Renderer>().material.color = minColor; } // Paint scanner with the found color color = minColor; return(currID); } } else { if (needUpdateColor) { GetComponent <Renderer>().material.color = Color.magenta; //paint scanner with Out of bounds / invalid color } return(-1); } }