private void Update() { //print(ColorMethods.GetResistorColorCode(new Color(0f, 4f/255f, 13f/255f)).ToString()); if (Input.GetKeyDown(KeyCode.Space)) { var data = t.EncodeToPNG(); File.WriteAllBytes("a.png", data); print("oldu"); } if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began && Input.touches[0].position.x > Screen.width / 2) { Application.CaptureScreenshot("ss" + Time.fixedTime + ".png"); //StartCoroutine(CaptureScreenshotCoroutine(Screen.width, Screen.height)); s += "s"; } if (!camAvailable) { return; } float ratio = (float)backCam.width / (float)backCam.height; fit.aspectRatio = ratio; float zoomValue = 2.7f; float scaleY = backCam.videoVerticallyMirrored ? -1f : 1f; background.rectTransform.localScale = new Vector3(1f * zoomValue, scaleY * zoomValue, 1f); int orient = -backCam.videoRotationAngle; background.rectTransform.localEulerAngles = new Vector3(0, 0, orient); Color color = backCam.GetPixel(backCam.width / 2, backCam.height / 2); ColorMethods.ColorName colorName = ColorMethods.GetResistorColorCode(color); colorDisplayer.color = GetPrimitiveColor(colorName); //colorText.text = "Color: " + colorName.ToString(); infoText.text = ColorMethods.GetHSV(color).ToString() + s; cursor.anchoredPosition = new Vector3(1080 / 2, 1920 / 2, 0f); int width = 75; int heigth = 5; Color[] colorBlock = backCam.GetPixels(backCam.width / 2 - width / 2, backCam.height / 2 - (heigth - 1) / 2, width, heigth); Color[,] colorBlock2D = ArrayTo2DArray(colorBlock, width, heigth); //Color baseColor; //List<ColorData> colorData = ResistorOhmFinder.GetLines(colorBlock2D, out baseColor); //simulator.Set(colorData, baseColor); t = new Texture2D(width, heigth); t.SetPixels(0, 0, width, heigth, colorBlock); t.Apply(); var t2 = MedianBlur(t, 5); t2.Apply(); Color[] colorBlockBlurred = t2.GetPixels(); Color[,] colorBlockBlurred2D = ArrayTo2DArray(colorBlockBlurred, width, heigth); //testTexture2D = MedianBlur(testTexture2D); testImage.texture = t; testImage2.texture = t2; List <ColorMethods.ColorName> colorData = ResistorOhmFinder.GetLines(colorBlockBlurred2D, lineCount); simulator.Set(colorData, lineCount); redLine.sizeDelta = new Vector2(width * zoomValue / 2 * 1.1f, 3); //redLine.sizeDelta = new Vector2(width * ratio / 2 * zoomValue * 1.1f, 3); }
public static List <ColorData> GetLines(Color[,] data, out Color baseColorInfo) { //Convert all data to ColorData. ColorData[,] colorInfo = GetColorData(data); //Find the center of the vertical axis of pixel group. int j = colorInfo.GetLength(0) / 2; //Set base color info to most left and center pixel. baseColorInfo = colorInfo[j, 0].color; //Set most left and center pixel as base color of resistor. ColorMethods.HSV baseColor = ColorMethods.GetHSV(colorInfo[j, 0].color); //Create an empty list to save lines. List <ColorData> lines = new List <ColorData>(); //Set tolerance level to check if a color is same as the base color since base color has no known range. float[] tolerance = { 5f, 0.1f, 0.3f }; //Create a flag to know if last checked pixel was a part of a line. ColorData lastPixel = null; //Iterate through all pixel from left to right to resolve their colors and find line colors. for (int i = 1; i < colorInfo.GetLength(1); i++) { //Get current pixel. ColorData currentPixel = colorInfo[j, i]; //If current pixel is white, check if it is just a shine effect. if (currentPixel.colorName == ColorMethods.ColorName.White) { currentPixel = CheckShine(i, colorInfo); } //If color of current pixel couldn't be resolved, continue to next pixel. if (currentPixel.colorName == ColorMethods.ColorName.Unknown) { Debug.LogWarning("Unknown pixel color found: " + currentPixel.color.ToString()); continue; } //If current pixel is NOT same as the base color ... if (CompareColors(baseColor, ColorMethods.GetHSV(currentPixel.color), tolerance) == false) { /*If last checked pixel was NOT a line color, that means we found a new line on resistor. * We should add this pixel to lines list and turn on the flag to let next pixel know last * pixel was a line pixel.*/ if (lastPixel == null) { if (lines.Count < 6) { lines.Add(currentPixel); lastPixel = currentPixel; } /*If there are already 6 lines but found a seventh one, that means this object is not a resistor.*/ else { return(null); } } /*If last checked pixel was a line color, but not same as this pixel's color; that means * there are two different lines next to each other without a base color seperating them. * That is not possible on a resistor.*/ else { if (lastPixel.colorName != currentPixel.colorName) { Debug.LogWarning("Two different adjoining lines found: 1)" + currentPixel.colorName.ToString() + " 2)" + lastPixel.colorName.ToString()); } } } //If current pixel is same as the base color, turn off the flag of last pixel was a line. else { lastPixel = null; } } return(lines); }