public void Edit() { RawTexture2D rawTexture = new RawTexture2D (image); rawTexture.Grayscale (); //rawTexture.OtsuThreshold (); float[,] matrix = new float[3, 3] { {-1f,0f,1f}, {-1f,0f,1f}, {-1f,0f,1f} }; float[,] gaussMatrix = new float[5, 5]{ {0.003765f, 0.015019f, 0.023792f, 0.015019f, 0.003765f}, {0.015019f, 0.059912f, 0.094907f, 0.059912f, 0.015019f}, {0.023792f, 0.094907f, 0.150342f, 0.094907f, 0.023792f}, {0.015019f, 0.059912f, 0.094907f, 0.059912f, 0.015019f}, {0.003765f, 0.015019f, 0.023792f, 0.015019f, 0.003765f} }; rawTexture.Convolve (matrix); RawTexture2D originRawTexture = new RawTexture2D (image); originRawTexture.Grayscale (); Tuple<int,int>[] bands = rawTexture.BandDetection(); Tuple<int,int>[] plates = new Tuple<int, int>[bands.Length]; RawTexture2D[] bandTextures = new RawTexture2D[bands.Length]; RawTexture2D[] plateTextures = new RawTexture2D[bands.Length]; for(int i = 0; i < bands.Length; i++) { bandTextures[i] = rawTexture.BandClipping (bands[i]); plates[i] = bandTextures[i].PlateDetection(); bandTextures[i] = originRawTexture.BandClipping(bands[i]); plateTextures[i] = bandTextures[i].PlateClipping(plates[i]); } RawTexture2D plate = plateTextures [0].PlateClipping2nd (); plate.Convolve (gaussMatrix); plate.Inverse (); plate.OtsuThreshold (); this.GetComponent<RawImage> ().texture = plate.ToTexture2D (); //plate.ZhangSuenThinning (); //Debug.Log (rawTexture.ColorCount ()); List<string> results = new List<string> (); List<RawTexture2D> blobs = plate.BlobDetection (); Debug.Log ("blob" + blobs.Count); foreach (RawTexture2D blob in blobs) { //blob.GetChainCode(); results.Add(this.GetComponent<Classifier>().ClassifyChainCode(blob)); } string resultString = "Angka :"; foreach (string result in results) { resultString += (" "+result); } text.GetComponent<Text> ().text = resultString; //this.GetComponent<RawImage> ().texture = blobs[0].ToTexture2D(); }
public void Edit() { RawTexture2D rawTexture = new RawTexture2D (image); //rawTexture.Grayscale (); rawTexture.Equalize (); this.GetComponent<RawImage> ().texture = rawTexture.ToTexture2D (); }
public void Edit() { RawTexture2D rawTexture = new RawTexture2D (image); rawTexture.Grayscale (); rawTexture.OtsuThreshold(); rawTexture.ZhangSuenThinning (); this.GetComponent<RawImage> ().texture = rawTexture.ToTexture2D (); }
public void ProcessTexture() { RawTexture2D rawTexture = new RawTexture2D (texture); List<RawTexture2D> blobs = rawTexture.BlobDetection (); bool[,] matrix = this.GetComponent<Classifier> ().ConvertToMatrix (blobs [0]); RawTexture2D resultRawTexture = new RawTexture2D (matrix); Texture2D resultTexture = resultRawTexture.ToTexture2D (); resultView.transform.GetChild(0).GetComponent<RawImage> ().texture = resultTexture; string resultClass = this.GetComponent<Classifier> ().ClassifyTurnCode (resultRawTexture); resultView.transform.GetChild (1).GetComponent<Text> ().text = "Angka : " + resultClass; resultView.SetActive (true); }
public void InitObjects(Game game) { // スクリプト用のオブジェクト。 GameObject titleEnterObject = new GameObject(); // コンポーネントをアタッチ。 titleEnterObject.AddComponent(new TitleEnterComponent(game)); AddObject(titleEnterObject); // カメラの初期化 StaticCamera camera = new StaticCamera(); camera.SetPosition(new Vec3(0, 0, -10)); AddObject(camera); // タイトルのテキスト // 色とフォントサイズ、テキストを指定 TextRenderer titleObject = new TextRenderer(800, 100) { FontColor = Color.Red, FontSize = 35, Text = "スミス VS たこ焼き 大決戦" }; titleObject .SetBounds(new Vec3(8, 1, 0)) .SetPosition(new Vec3(-3.5f, 2, 0)); AddObject(titleObject); // 'Press Enter..'のテキスト TextRenderer pressEnterObject = new TextRenderer(200, 100) { FontColor = Color.DarkRed, Text = "Press Enter..." }; pressEnterObject .SetBounds(new Vec3(2, 1, 0)) .SetPosition(new Vec3(-1f, -2, 0)); AddObject(pressEnterObject); // バックグラウンドを配置 RawTexture2D background = new RawTexture2D("Images/sky.png") { Layer = 1 }; background .SetBounds(new Vec3(15f, 10f)) .SetPosition(new Vec3(-5.5f, -4.5f, 0)); AddObject(background); }
public RawTexture2D BandClipping(Tuple<int,int> band) { RawTexture2D bandTexture = new RawTexture2D(); bandTexture = new RawTexture2D(); int bandWidth = width; int bandHeight = band.Item2-band.Item1+1; int bandSize = bandWidth * bandHeight; bandTexture.pixels = new Color32[bandSize]; bandTexture.width = bandWidth; bandTexture.height = bandHeight; for(int j = 0; j < bandSize; j++){ bandTexture.pixels[j] = pixels[band.Item1*width+j]; } return bandTexture; }
public string ClassifyTurnCountCode(RawTexture2D rawTexture) { string turnCodeClass = ""; int lowestScore = 99999999; int[] count = CountChainCode(rawTexture.GetChainCode ()); foreach (KeyValuePair<string, int[]> data in turnCodeCountDataSet) { int score = 0; for(int i = 0; i < 6; i++){ score += Mathf.Abs(count[i] - data.Value[i]); } if(score < lowestScore){ lowestScore = score; turnCodeClass = data.Key; } } return turnCodeClass; }
public string ClassifyTurnCode(RawTexture2D rawTexture) { string turnCodeClass = ""; int lowestScore = 0; int[] turnCode = ChainCodeToTurnCode(rawTexture.GetChainCode()); foreach (KeyValuePair<string, int[]> data in turnCodeDataSet) { int score = 0; for(int i = 0; i < Mathf.Min(turnCode.Length, data.Value.Length); i++){ score += turnCode[i] == data.Value[i] ? 1 : 0; } if(score > lowestScore){ lowestScore = score; turnCodeClass = data.Key; } } return turnCodeClass; }
public void InitObjects(Game game) { // カメラの初期化 StaticCamera camera = new StaticCamera(); camera.SetPosition(new Vec3(0, 0, -10)); AddObject(camera); // バックグラウンドを配置 RawTexture2D background = new RawTexture2D("Images/sky.png") { Layer = 1 }; background .SetBounds(new Vec3(15f, 10f)) .SetPosition(new Vec3(-5.5f, -4.5f, 0)); AddObject(background); // プレイヤーを配置 RawTexture2D player = new RawTexture2D("Images/smith.png"); player .SetBounds(new Vec3(.8f, 1.3f, 0)) .SetPosition(new Vec3(-0.5f, -3.0f, 0)); player.AddComponent(new PlayerComponent(game)); AddObject(player); TextRenderer renderer = new TextRenderer(200, 100) { FontColor = Color.Red }; renderer .SetBounds(new Vec3(2, 1, 0)) .SetPosition(new Vec3(-3f, 3f, 0)); AddObject(renderer); // スクリプト用オブジェクト GameObject components = new GameObject(); components.AddComponent(new RandomSpawnerComponent()); components.AddComponent(new TimerComponent(game, renderer)); AddObject(components); }
public override void OnUpdate(double deltaTime) { base.OnUpdate(deltaTime); if (_tick % 60 == 0) { Vec3 pos = new Vec3(_random.Next(-5, 5), 5, 0); RawTexture2D takoyaki = new RawTexture2D("Images/takoyaki.png") { Tag = "Takoyaki", Layer = 0 }; takoyaki .SetBounds(new Vec3(1, 1, 0)) .SetPosition(pos); takoyaki.AddComponentUnsafe <TakoyakiFallComponent>(); takoyaki.AddComponentUnsafe <TakoyakiRemoveComponent>(); GameObject.Scene.AddObject(takoyaki); } _tick++; }
public RawTexture2D PlateClipping2nd() { int[] px = HorizontalProjection (); int[] px2 = new int[px.Length]; int h = 4; for (int i = h; i < px.Length; i++) { px2[i] = (px[i]-px[i-h])/h; //Debug.Log("-> "+px2[i]); } int max = px2.Max (); int min = px2.Min (); int p0 = 0; int p1 = width-1; float c = 0.1f; for (int i = 0; i < px2.Length/2; i++) { if(px2[i]<c*(float)min){ p0 = i; break; } } for (int i =px2.Length-1; i>=px2.Length/2; i--) { if(px2[i]>c*(float)max){ p1 = i; break; } } int plateWidth = p1 - p0 +1; int plateHeight = height; int plateSize = plateWidth * plateHeight; RawTexture2D plateTexture = new RawTexture2D (); plateTexture.width = plateWidth; plateTexture.height = plateHeight; plateTexture.pixels = new Color32[plateSize]; //Debug.Log ("=>" + p0 + " " + p1); for(int j = 0; j < plateSize; j++){ plateTexture.pixels[j] = pixels[p0+(j%plateWidth)+(j/plateWidth)*width]; } return plateTexture; }
public RawTexture2D PlateClipping(Tuple<int,int> plate) { Debug.Log (plate.Item1 + " " + plate.Item2); RawTexture2D bandTexture = new RawTexture2D(); int bandWidth = plate.Item2 - plate.Item1 +1; int bandHeight = height; int bandSize = bandWidth * bandHeight; bandTexture.pixels = new Color32[bandSize]; bandTexture.width = bandWidth; bandTexture.height = bandHeight; //Debug.Log (bandTexture.width + "==" + bandTexture.height); for(int j = 0; j < bandSize; j++){ bandTexture.pixels[j] = pixels[plate.Item1+(j%bandWidth)+(j/bandWidth)*width]; } return bandTexture; }
public List<RawTexture2D> BlobDetection() { int w = width; int h = height; int s = pixels.Length; bool[] p = new bool[5]; int[] pL = new int[5]; int[,] labels = new int[h, w]; int[] linked = new int[w*h/4]; int label = 1; for (int i = 0; i < s; i++) { int x = i%w; int y = i/w; p [0] = pixels [i].r != 255; pL [0] = labels[y,x]; if(y - 1 < 0 || x - 1 < 0){ p[1] = false; pL[1] = 0; } else { p[1] = pixels [i - w - 1].r != 255; pL[1] = linked[labels[y-1,x-1]]; } if(y - 1 < 0){ p[2] = false; pL[2] = 0; } else { p[2] = pixels [i - w].r != 255; pL[2] = linked[labels[y-1,x]]; } if(y - 1 < 0 || x + 1 > w - 1){ p[3] = false; pL[3] = 0; } else { p[3] = pixels [i - w + 1].r != 255; pL[3] = linked[labels[y-1,x+1]]; } if(x - 1 < 0){ p[4] = false; pL[4] = 0; } else { p[4] = pixels [i - 1].r != 255; pL[4] = linked[labels[y,x-1]]; } if(p[0]){ if(pL[1]==0 && pL[2]==0 && pL[3]==0 && pL[4]==0){ labels[y,x] = label; linked[label] = label; label++; } else { int minLabel = 999; for(int j = 1; j<5; j++){ if(pL[j] != 0 && pL[j] < minLabel) minLabel = pL[j]; } labels[y,x] = minLabel; if(pL[1]!=0){ linked[pL[1]] = minLabel; //labels[y-1,x-1] = minLabel; } if(pL[2]!=0){ linked[pL[2]] = minLabel; //labels[y-1,x] = minLabel; } if(pL[3]!=0){ linked[pL[3]] = minLabel; //labels[y-1,x+1] = minLabel; } if(pL[4]!=0){ linked[pL[4]] = minLabel; //labels[y,x-1] = minLabel; } } } } int k = 1; while (linked[k]!=0) { int j = k; while(linked[j] != j){ j = linked[j]; } linked[k] = j; k++; } k = 1; int maxLabel = linked.Distinct ().Count ()-1; int[] newLabels = new int[maxLabel+1]; int index = 1; while (linked[k]!=0) { bool isFound = false; for (int i = 1; i <= index-1; i++) { if(linked[k] == newLabels[i] && linked[k]!=0){ linked[k] = i; isFound = true; } } if(!isFound) { newLabels[index] = linked[k]; linked[k] = index; index++; } k++; } for (int j = 0; j < h; j++) { for(int i = 0; i < w; i++){ labels[j,i] = linked[labels[j,i]]; } } /* for (int j = 0; j < h; j++) { for(int i = 0; i < w; i++){ if(labels[j,i] != 0){ pixels[j*w+i].r = (byte)((255 * labels[j,i] / maxLabel)%256); pixels[j*w+i].b = (byte)(255-pixels[j*w+i].r); pixels[j*w+i].g = (byte)((pixels[j*w+i].r+pixels[j*w+i].b)/2); } } } */ Rect[] blobRect = new Rect[maxLabel]; for (int i = 0; i < maxLabel; i++) { blobRect[i] = new Rect(w,h,0,0); } for (int j = 0; j < h; j++) { for(int i = 0; i < w; i++){ if(labels[j,i] != 0){ for(int lb = 0; lb < maxLabel; lb++){ if(labels[j,i]-1 == lb){ if(i < blobRect[lb].x) blobRect[lb].x = i; if(i > blobRect[lb].width) blobRect[lb].width = i; if(j < blobRect[lb].y) blobRect[lb].y = j; if(j > blobRect[lb].height) blobRect[lb].height = j; } } } } } List<Tuple<int,int>> order = new List<Tuple<int, int>> (); for (int i = 0; i < blobRect.Length; i++) { order.Add(Tuple.Create(i+1, (int)blobRect[i].x)); } order = order.OrderBy (i => i.Item2).ToList(); blobRect = blobRect.OrderBy(i => i.x).ToArray(); List<RawTexture2D> blobs = new List<RawTexture2D> (); for (int i = 0; i < maxLabel; i++) { int blobX = (int)blobRect[i].x; int blobY = (int)blobRect[i].y; int blobW = (int)(blobRect[i].width - blobRect[i].x + 1); int blobH = (int)(blobRect[i].height - blobRect[i].y + 1); if(blobW*blobH>9){ RawTexture2D rawTexture = new RawTexture2D(blobW, blobH); for(int yy=0; yy < blobH; yy++){ for(int xx=0; xx<blobW; xx++){ rawTexture.pixels[yy*blobW+xx] = labels[blobY+yy, blobX+xx] == order[i].Item1 ? Color.black : Color.white; } } blobs.Add(rawTexture); } } return blobs; }
public bool[,] ConvertToMatrix(RawTexture2D texture) { bool[,] matrix = new bool[5, 5]; for (int j = 0; j < 5; j++) { for(int i = 0; i < 5; i++){ bool isFound = false; for(int y = j*texture.height/5; y < (j+1)*texture.height/5; y++){ for(int x = i*texture.width/5; x < (i+1)*texture.width/5; x++){ if(texture.GetPixel(x,y).r < 255){ isFound = true; break; } } if(isFound) break; } matrix[j,i] = isFound; } } return matrix; }
// Use this for initialization void Start() { imageData = System.Array.ConvertAll (Resources.LoadAll ("Images/"+path, typeof(Texture2D)), i => (Texture2D)i); foreach (Texture2D image in imageData) { RawTexture2D rawTexture2D = new RawTexture2D(image); int[] chainCode = rawTexture2D.GetChainCode(); int[] turnCode = ChainCodeToTurnCode(chainCode); foreach(int x in turnCode){; Debug.Log (image.name+"-- "+x); } chainCodeDataSet.Add(image.name, chainCode); chainCodeCountDataSet.Add(image.name, CountChainCode(chainCode)); turnCodeDataSet.Add(image.name, turnCode); turnCodeCountDataSet.Add(image.name, CountTurnCode(turnCode)); } }