// Use this for initialization void Start() { Mat mat = Unity.TextureToMat(this.texture); for (int yi = 0; yi < mat.Height; yi++) { for (int xi = 0; xi < mat.Width; xi++) { Vec3b v = mat.At <Vec3b>(yi, xi); Debug.Log(v[0]); float gr = 0.2126f * v[2] + 0.7152f * v[1] + 0.0722f * v[0]; if (gr < 128) { gr = 0; } else { gr = 255; } v[0] = (byte)gr; v[1] = (byte)gr; v[2] = (byte)gr; mat.Set <Vec3b>(yi, xi, v); } } Texture2D changedTex = Unity.MatToTexture(mat); GetComponent <RawImage>().texture = changedTex; }
public Texture getScanFrame(WebCamTexture inputTexture) { Mat original = Unity.TextureToMat(inputTexture); Size inputSize = new Size(original.Width, original.Height); scanner.Input = Unity.TextureToMat(inputTexture); if (!scanner.Success) { scanner.Settings.GrayMode = PaperScanner.ScannerSettings.ColorMode.HueGrayscale; } Point[] detectedContour = scanner.PaperShape; var matCombinedFrame = new Mat(new Size(inputSize.Width, inputSize.Height), original.Type(), Scalar.FromRgb(64, 64, 64)); original.CopyTo(matCombinedFrame.SubMat(0, inputSize.Height, 0, inputSize.Width)); if (null != detectedContour && detectedContour.Length > 2) { matCombinedFrame.DrawContours(new Point[][] { detectedContour }, 0, Scalar.FromRgb(255, 255, 0), 3); } return(Unity.MatToTexture(matCombinedFrame)); }
// Use this for initialization public void Process(string name) { var rawImage = gameObject.GetComponent <RawImage>(); rawImage.texture = null; Texture2D inputTexture = (Texture2D)Resources.Load("DocumentScanner/" + name); scanner.Settings.NoiseReduction = 0.7; // real-world images are quite noisy, this value proved to be reasonable scanner.Settings.EdgesTight = 0.9; // higher value cuts off "noise" as well, this time smaller and weaker edges scanner.Settings.ExpectedArea = 0.2; // we expect document to be at least 20% of the total image area scanner.Settings.GrayMode = PaperScanner.ScannerSettings.ColorMode.Grayscale; // color -> grayscale conversion mode // process input with PaperScanner Mat result = null; scanner.Input = Unity.TextureToMat(inputTexture); //★scanner.Successで使用する // should we fail, there is second try - HSV might help to detect paper by color difference if (!scanner.Success) { // this will drop current result and re-fetch it next time we query for 'Success' flag or actual data scanner.Settings.GrayMode = PaperScanner.ScannerSettings.ColorMode.HueGrayscale; } // now can combine Original/Scanner image //result = CombineMats(scanner.Input, scanner.Output, scanner.PaperShape); result = scanner.Output; // apply result or source (late for a failed scan) //rawImage.texture = Unity.MatToTexture(result); rawImage.texture = Unity.MatToTexture(result); var transform = gameObject.GetComponent <RectTransform>(); }
// Use this for initialization void Start() { Mat mat = Unity.TextureToMat(this.texture); Vector3[,] v = new Vector3[mat.Height, mat.Width]; for (int yi = 0; yi < mat.Height; yi++) { for (int xi = 0; xi < mat.Width; xi++) { Vec3b vyx = mat.At <Vec3b>(yi, xi); v[yi, xi][0] = vyx[0]; v[yi, xi][1] = vyx[1]; v[yi, xi][2] = vyx[2]; } } v = Median(v, mat.Height, mat.Width); for (int yi = 0; yi < mat.Height; yi++) { for (int xi = 0; xi < mat.Width; xi++) { Vec3b vyx = new Vec3b(); vyx[0] = (byte)v[yi, xi][0]; vyx[1] = (byte)v[yi, xi][1]; vyx[2] = (byte)v[yi, xi][2]; mat.Set <Vec3b>(yi, xi, vyx); } } Texture2D changedTex = Unity.MatToTexture(mat); GetComponent <RawImage>().texture = changedTex; }
// Use this for initialization public void ProcessWebCam(WebCamTexture webCamTexture) { var rawImage = gameObject.GetComponent <RawImage>(); rawImage.texture = null; scanner.Settings.NoiseReduction = 0.7; // real-world images are quite noisy, this value proved to be reasonable scanner.Settings.EdgesTight = 8; // higher value cuts off "noise" as well, this time smaller and weaker edges scanner.Settings.ExpectedArea = 0.05; // we expect document to be at least 20% of the total image area scanner.Settings.GrayMode = PaperScanner.ScannerSettings.ColorMode.Grayscale; // color -> grayscale conversion mode // process input with PaperScanner Mat result = null; scanner.Input = Unity.TextureToMat(webCamTexture);//★scanner.Successで使用する // should we fail, there is second try - HSV might help to detect paper by color difference if (!scanner.Success) { scanner.Settings.GrayMode = PaperScanner.ScannerSettings.ColorMode.HueGrayscale; } result = scanner.Output; rawImage.texture = Unity.MatToTexture(result); var transform = gameObject.GetComponent <RectTransform>(); }
// Use this for initialization void Start() { Mat mat = Unity.TextureToMat(this.texture); Mat changedMat = new Mat(); Cv2.CvtColor(mat, changedMat, ColorConversionCodes.BGR2RGB); Texture2D changedTex = Unity.MatToTexture(changedMat); GetComponent <RawImage>().texture = changedTex; }
// Use this for initialization void Start() { Mat mat = Unity.TextureToMat(this.texture); Mat changedMat = new Mat(); Cv2.MedianBlur(mat, changedMat, 3); Texture2D changedTex = Unity.MatToTexture(changedMat); GetComponent <RawImage>().texture = changedTex; }
private void ChangeTarget(int targetIndex) { maxRate = 0; currentTargetIndex = targetIndex % targetList.Length; if (targetImage.texture != null) { //Resources.UnloadAsset(targetImage.texture); } var data = File.ReadAllBytes(targetList[currentTargetIndex]); var texture = new Texture2D(4, 4); texture.LoadImage(data); targetImage.texture = texture; targetImage.GetComponent <AspectRatioFitter>().aspectRatio = (float)targetImage.texture.width / targetImage.texture.height; if (targetMat != null) { targetMat.Dispose(); } targetMat = new Mat(); Mat targetColorMat = CvUnity.TextureToMat((Texture2D)targetImage.texture); Cv2.CvtColor(targetColorMat, targetMat, ColorConversionCodes.BGRA2GRAY); if (targetMat.Width > 512) { var size = new Size(512, (double)targetMat.Height / targetMat.Width * 512); targetMat = targetMat.Resize(size); } targetPixelCount = 0; for (int y = 0; y < targetMat.Height; ++y) { for (int x = 0; x < targetMat.Width; ++x) { int index = targetMat.Width * y + x; unsafe { byte val = *(targetMat.DataPointer + index); if (val == 0) { targetPixelCount++; } } } } UpdateLine(highLine, 0); }
private void UpdateBackground() { Mat cameraMat = CvUnity.TextureToMat(webCamTexture, webCamParams); if (cameraMat.Width > 512) { var size = new Size(512, (double)cameraMat.Height / cameraMat.Width * 512); cameraMat = cameraMat.Resize(size); } backgroundMat = cameraMat.Clone(); }
/// <summary> /// Creates OpenCV Mat from Unity texture /// </summary> /// <param name="texture">Texture instance, must be either Texture2D or WbCamTexture</param> /// <returns>Newely created Mat object, ready to use with OpenCV</returns> /// <param name="texParams">Texture parameters (flipped, rotated etc.)</param> protected virtual Mat MatFromTexture(T texture, Unity.TextureConversionParams texParams) { if (texture is UnityEngine.Texture2D) { return(Unity.TextureToMat(texture as UnityEngine.Texture2D, texParams)); } else if (texture is UnityEngine.WebCamTexture) { return(Unity.TextureToMat(texture as UnityEngine.WebCamTexture, texParams)); } else { throw new Exception("FaceProcessor: incorrect input texture type, must be Texture2D or WebCamTexture"); } }
// Use this for initialization public void Process(string name) { var rawImage = gameObject.GetComponent <RawImage>(); rawImage.texture = null; Texture2D inputTexture = (Texture2D)Resources.Load("DocumentScanner/" + name); // first of all, we set up scan parameters // // scanner.Settings has more values than we use // (like Settings.Decolorization that defines // whether b&w filter should be applied), but // default values are quite fine and some of // them are by default in "smart" mode that // uses heuristic to find best choice. so, // we change only those that matter for us scanner.Settings.NoiseReduction = 0.7; // real-world images are quite noisy, this value proved to be reasonable scanner.Settings.EdgesTight = 0.9; // higher value cuts off "noise" as well, this time smaller and weaker edges scanner.Settings.ExpectedArea = 0.2; // we expect document to be at least 20% of the total image area scanner.Settings.GrayMode = PaperScanner.ScannerSettings.ColorMode.Grayscale; // color -> grayscale conversion mode // process input with PaperScanner Mat result = null; scanner.Input = Unity.TextureToMat(inputTexture); //★scanner.Successで使用する // should we fail, there is second try - HSV might help to detect paper by color difference if (!scanner.Success) { // this will drop current result and re-fetch it next time we query for 'Success' flag or actual data scanner.Settings.GrayMode = PaperScanner.ScannerSettings.ColorMode.HueGrayscale; } // now can combine Original/Scanner image result = CombineMats(scanner.Input, scanner.Output, scanner.PaperShape); //result = scanner.Output; // apply result or source (late for a failed scan) //rawImage.texture = Unity.MatToTexture(result); rawImage.texture = Unity.MatToTexture(result); var transform = gameObject.GetComponent <RectTransform>(); transform.sizeDelta = new Vector2(result.Width, result.Height); }
// Use this for initialization void Start() { Mat mat = Unity.TextureToMat(this.texture); for (int yi = 0; yi < mat.Height; yi++) { for (int xi = 0; xi < mat.Width; xi++) { Vec3b v = mat.At <Vec3b>(yi, xi); v[0] = (byte)(ReduceColor(v[0])); v[1] = (byte)(ReduceColor(v[1])); v[2] = (byte)(ReduceColor(v[2])); mat.Set <Vec3b>(yi, xi, v); } } Texture2D changedTex = Unity.MatToTexture(mat); GetComponent <RawImage>().texture = changedTex; }
// Use this for initialization void Start() { Mat mat = Unity.TextureToMat(this.texture); for (int yi = 0; yi < 16; yi++) { for (int xi = 0; xi < 16; xi++) { Vec3b max = new Vec3b(); for (int yj = 0; yj < 8; yj++) { for (int xj = 0; xj < 8; xj++) { Vec3b v = mat.At <Vec3b>(yi * 8 + yj, xi * 8 + xj); if (max[0] < v[0]) { max[0] = v[0]; } if (max[1] < v[1]) { max[1] = v[1]; } if (max[2] < v[2]) { max[2] = v[2]; } } } for (int yj = 0; yj < 8; yj++) { for (int xj = 0; xj < 8; xj++) { mat.Set <Vec3b>(yi * 8 + yj, xi * 8 + xj, max); } } } } Texture2D changedTex = Unity.MatToTexture(mat); GetComponent <RawImage>().texture = changedTex; }
// Use this for initialization void Start() { Mat mat = Unity.TextureToMat(this.texture); Mat changedMat = new Mat(); Mat changedMat1 = new Mat(); Cv2.CvtColor(mat, changedMat, ColorConversionCodes.BGR2HSV); for (int yi = 0; yi < mat.Height; yi++) { for (int xi = 0; xi < mat.Width; xi++) { Vec3b v = changedMat.At <Vec3b>(yi, xi); Debug.Log(v[0]); v[0] = (byte)((v[0] - 180) % 360); changedMat.Set <Vec3b>(yi, xi, v); } } Cv2.CvtColor(changedMat, changedMat1, ColorConversionCodes.HSV2BGR); Texture2D changedTex = Unity.MatToTexture(changedMat1); GetComponent <RawImage>().texture = changedTex; }
// Use this for initialization void Start() { Mat mat = Unity.TextureToMat(this.texture); for (int yi = 0; yi < 16; yi++) { for (int xi = 0; xi < 16; xi++) { Vector3 sum = new Vector3(); for (int yj = 0; yj < 8; yj++) { for (int xj = 0; xj < 8; xj++) { Vec3b v = mat.At <Vec3b>(yi * 8 + yj, xi * 8 + xj); sum[0] += v[0]; sum[1] += v[1]; sum[2] += v[2]; } } Debug.Log(sum[0]); Vec3b ave = new Vec3b(); ave[0] = (byte)(sum[0] / 64); ave[1] = (byte)(sum[1] / 64); ave[2] = (byte)(sum[2] / 64); for (int yj = 0; yj < 8; yj++) { for (int xj = 0; xj < 8; xj++) { mat.Set <Vec3b>(yi * 8 + yj, xi * 8 + xj, ave); } } } } Texture2D changedTex = Unity.MatToTexture(mat); GetComponent <RawImage>().texture = changedTex; }
//OpenCVを使用して、座標を求める。 void OpenCVTexture(Texture2D texture) { Mat newMat = Unity.TextureToMat(texture); //画像をCv2.Equalsで変化があるかグローバルのoldMatと比較して検知しようとしたが、できなかった。 //Convert image to grayscale Mat imgGray = new Mat(); Cv2.CvtColor(newMat, imgGray, ColorConversionCodes.BGR2GRAY); //Debug.Log(Cv2.Equals(imgGray, imgGray)); // Clean up image using Gaussian Blur Mat imgGrayBlur = new Mat(); Cv2.GaussianBlur(imgGray, imgGrayBlur, new Size(5, 5), 0); //Extract edges Mat cannyEdges = new Mat(); Cv2.Canny(imgGrayBlur, cannyEdges, 10.0, 70.0); //Do an invert binarize the image Mat mask = new Mat(); Cv2.Threshold(cannyEdges, mask, 70.0, 255.0, ThresholdTypes.BinaryInv); // Extract Contours Point[][] contours; //特徴点が格納される変数。 HierarchyIndex[] hierarchy; //特徴点の階層が格納される。 Cv2.FindContours(cannyEdges, out contours, out hierarchy, RetrievalModes.Tree, ContourApproximationModes.ApproxSimple, null); //特徴点を検出する。 PointChangeNumSendUDP(contours); //Pointが変化しなければ送信を実装しようとしている途中。 //StartCoroutine(udpSendCoroutine(contours)); //輪郭描画 int width = (int)transform.GetComponent <RectTransform>().sizeDelta.x; int height = (int)transform.GetComponent <RectTransform>().sizeDelta.y; Mat Contours = new Mat(width, height, MatType.CV_8UC3, new Scalar(0, 0, 0)); //初期値として黒い画面を作成する。 Cv2.DrawContours(Contours, contours, -1, new Scalar(0, 255, 0, 255), 1); //MatにCountours(特徴点)を描画する。 Texture2D changedTex = Unity.MatToTexture(Contours); //MatをTexture2Dへ変更 GetComponent <RawImage>().texture = changedTex; //RaxImageにTexture2Dを書き込み。 //MatをDisposeする。 newMat.Dispose(); imgGray.Dispose(); imgGrayBlur.Dispose(); cannyEdges.Dispose(); mask.Dispose(); Contours.Dispose(); //TextureをDestryしないとメモリーリークを送りました。 MonoBehaviour.Destroy(texture); if (changedTex != oldChangedTex) { MonoBehaviour.Destroy(oldChangedTex); oldChangedTex = changedTex; } }
private void UpdateImages() { if (backgroundMat == null) { return; } Mat cameraMat = CvUnity.TextureToMat(webCamTexture, webCamParams); if (cameraMat.Width > 512) { var size = new Size(512, (double)cameraMat.Height / cameraMat.Width * 512); cameraMat = cameraMat.Resize(size); } var width = cameraMat.Width - cameraOffset.left - cameraOffset.right; var height = cameraMat.Height - cameraOffset.top - cameraOffset.bottom; var posX = cameraOffset.left; var posY = cameraOffset.top; var cameraRect = new OpenCvSharp.Rect(posX, posY, width, height); cameraMat = cameraMat.Clone(cameraRect); Mat trimBgMat = backgroundMat.Clone(cameraRect); Mat diffBgMat = cameraMat.Clone(); Cv2.Absdiff(diffBgMat, trimBgMat, diffBgMat); Mat grayscaleMat = new Mat(); Cv2.CvtColor(diffBgMat, grayscaleMat, ColorConversionCodes.BGR2GRAY); //Mat blurMat = new Mat(); //Cv2.GaussianBlur(grayscaleMat, blurMat, new Size(5, 5), 0); Mat maskMat = new Mat(); Cv2.Threshold(grayscaleMat, maskMat, maskThreshold, maskMaxVal, ThresholdTypes.BinaryInv); //Cv2.Threshold(blurMat, maskMat, maskThreshold, maskMaxVal, ThresholdTypes.BinaryInv); Mat dilateMat = maskMat.Dilate(new Mat(), null, smoothIterationCount); Mat erodeMat = dilateMat.Erode(new Mat(), null, smoothIterationCount); Mat outputMat = erodeMat.Clone(); float targetAspectRatio = (float)targetMat.Width / targetMat.Height; float outputAspectRatio = (float)erodeMat.Width / erodeMat.Height; if (targetMat.Height != outputMat.Height) { var rate = (double)targetMat.Height / outputMat.Height; var size = new Size(outputMat.Width * rate, outputMat.Height * rate); outputMat = outputMat.Resize(size); } var rect = new OpenCvSharp.Rect((outputMat.Width - targetMat.Width) / 2, 0, targetMat.Width, targetMat.Height); Mat resizeMat = outputMat.Clone(rect); Mat diffMat = new Mat(); Cv2.Absdiff(targetMat, resizeMat, diffMat); Mat resultColorMat = new Mat(resizeMat.Rows, resizeMat.Cols, MatType.CV_8UC4); CountDiff(resizeMat, targetMat, resultColorMat); MatToImage(cameraMat, cameraImage, ref cameraTexture); //MatToImage(backgroundMat, backgroundImage, ref backgroundTexture); //MatToImage(resizeMat, outputImage, ref outputTexture); //MatToImage(diffMat, diffImage, ref diffTexture); MatToImage(resultColorMat, diffImage, ref diffTexture); }
// Use this for initialization void Start() { Mat mat = Unity.TextureToMat(this.texture); float[] results = new float[256]; float[,] grs = new float[mat.Height, mat.Width]; for (int yi = 0; yi < mat.Height; yi++) { for (int xi = 0; xi < mat.Width; xi++) { Vec3b v = mat.At <Vec3b>(yi, xi); float gr = 0.2126f * v[2] + 0.7152f * v[1] + 0.0722f * v[0]; grs[yi, xi] = gr; } } for (int thi = 1; thi < 255; thi++) { int w0 = 0; int w1 = 0; float M0 = 0; float M1 = 0; foreach (float gr in grs) { if (gr < thi) { w0++; M0 += gr; } else { w1++; M1 += gr; } } Debug.Log(w0 + w1); float tmp0 = w0 == 0 ? 0 : M0 / w0; float tmp1 = w1 == 0 ? 0 : M1 / w1; results[thi] = ((float)w0 / (mat.Height * mat.Width)) * ((float)w1 / (mat.Height * mat.Width)) * Mathf.Pow(tmp0 - tmp1, 2); } int z = 0; for (int i = 1; i < 255; i++) { if (results[i] > results[z]) { z = i; } } for (int yi = 0; yi < mat.Height; yi++) { for (int xi = 0; xi < mat.Width; xi++) { if (grs[yi, xi] < z) { Vec3b v = new Vec3b(); v[0] = (byte)0; v[1] = (byte)0; v[2] = (byte)0; mat.Set <Vec3b>(yi, xi, v); } else { Vec3b v = new Vec3b(); v[0] = (byte)255; v[1] = (byte)255; v[2] = (byte)255; mat.Set <Vec3b>(yi, xi, v); } } } Texture2D changedTex = Unity.MatToTexture(mat); GetComponent <RawImage>().texture = changedTex; }