private void CheckPointDistance(DSAR_Blob tb, float distanceThreshold) { //check distances between remaining points for (int m = 0; m < tb.corner_pixels.Count; m++) { for (int n = 0; n < tb.corner_pixels.Count; n++) { if (m != n) { if (Vector2.Distance(tb.corner_pixels[m], tb.corner_pixels[n]) < (tb.pixels_in_blob.Count * distanceThreshold)) { tb.corner_pixels[n] = new Vector2(-1, -1); } } } } //remove points that are too close together for (int p = tb.corner_pixels.Count - 1; p > 0; p--) { if (tb.corner_pixels[p] == new Vector2(-1, -1)) { tb.corner_pixels.Remove(tb.corner_pixels[p]); } } }
private void CheckPointAngle(DSAR_Blob tb, float angleThreshold) { //check angles between points float[] angles = new float[tb.pixels_in_bb.Count]; for (int j = 0; j < tb.pixels_in_bb.Count; j++) { if (j != 0 && j != tb.pixels_in_bb.Count - 1) { angles[j] = Vector2.Angle((tb.pixels_in_bb[j] - tb.pixels_in_bb[j - 1]), (tb.pixels_in_bb[j] - tb.pixels_in_bb[j + 1])); } else if (j == 0) { angles[j] = Vector2.Angle((tb.pixels_in_bb[j] - tb.pixels_in_bb[tb.pixels_in_bb.Count - 1]), (tb.pixels_in_bb[j] - tb.pixels_in_bb[j + 1])); } else if (j == tb.pixels_in_bb.Count) { angles[j] = Vector2.Angle((tb.pixels_in_bb[j] - tb.pixels_in_bb[j - 1]), (tb.pixels_in_bb[j] - tb.pixels_in_bb[0])); } } //remove points that are almost on the same vector for (int p = tb.pixels_in_bb.Count - 1; p > 0; p--) { if (Mathf.Abs(angles[p]) < angleThreshold) { tb.pixels_in_bb.Remove(tb.pixels_in_bb[p]); } } }
public bool solveShapes(int i) { //continue only of the cell is black if (!isBlack(c [i])) { return(false); } //first time called for new blob if (!isRecursive) { isRecursive = true; //GameObject tempBlob = Instantiate(blob); blobs.Add(blob); currentBlob = blobs [blobs.Count - 1].GetComponent <DSAR_Blob> (); b_count = 0; } b_count++; // commented out because its interfering //currentBlob.pixels_in_blob.Add (i); //check above pixel if (i + c_w < c.Length) { if (!isWhite(c[i + c_w])) { c[i] = newColor; if (isBlack(c[i + c_w])) { solveShapes(i + c_w); } return(true); } } //check left pixel if (i % c_w != 0) { if (!isWhite(c [i - 1])) { c [i] = newColor; if (isBlack(c [i - 1])) { solveShapes(i - 1); } return(true); } } //check right pixel if ((i + 1) % c_w != 0 && i < (c_w * c_h)) { if (!isWhite(c [i + 1])) { c [i] = newColor; if (isBlack(c [i + 1])) { solveShapes(i + 1); } return(true); } } //check below pixel if (i - c_w >= 0) { if (!isWhite(c[i - c_w])) { c[i] = newColor; if (isBlack(c[i - c_w])) { solveShapes(i - c_w); } return(true); } } return(false); }
private float CheckCornerColours2(DSAR_Blob tb) { int totalWhiteCount = 0; int totalEdgeCount = 0; for (int j = 0; j < tb.pixels_in_blob.Count; j++) { int pixelNum = (int)((c_w * tb.pixels_in_blob[j].y) + tb.pixels_in_blob[j].x); int whiteCount = 0; if (c[pixelNum] == Color.white) { whiteCount++; } if (c[pixelNum + 1] == Color.white) { whiteCount++; } if (c[pixelNum - 1] == Color.white) { whiteCount++; } if (c[pixelNum + c_w] == Color.white) { whiteCount++; } if (c[pixelNum - c_w] == Color.white) { whiteCount++; } if (c[pixelNum + c_w + 1] == Color.white) { whiteCount++; } if (c[pixelNum + c_w - 1] == Color.white) { whiteCount++; } if (c[pixelNum - c_w + 1] == Color.white) { whiteCount++; } if (c[pixelNum - c_w - 1] == Color.white) { whiteCount++; } if (whiteCount > 0) { totalWhiteCount += whiteCount; totalEdgeCount += 1; } //Debug.Log(whiteCount); //is corner if most pixels around are white if (whiteCount >= 5) { //is corner tb.corner_pixels.Add(tb.pixels_in_blob[j]); } } return(totalWhiteCount / totalEdgeCount); }
private void CheckCornerColours(DSAR_Blob tb) { for (int j = 0; j < tb.pixels_in_bb.Count; j++) { int pixelNum = (int)((c_w * tb.pixels_in_bb[j].y) + tb.pixels_in_bb[j].x); int whiteCount = 0; if (c[pixelNum] == Color.white) { whiteCount++; } if (c[pixelNum + 1] == Color.white) { whiteCount++; } if (c[pixelNum - 1] == Color.white) { whiteCount++; } if (c[pixelNum + c_w] == Color.white) { whiteCount++; } if (c[pixelNum - c_w] == Color.white) { whiteCount++; } if (c[pixelNum + c_w + 1] == Color.white) { whiteCount++; } if (c[pixelNum + c_w - 1] == Color.white) { whiteCount++; } if (c[pixelNum - c_w + 1] == Color.white) { whiteCount++; } if (c[pixelNum - c_w - 1] == Color.white) { whiteCount++; } //Debug.Log(whiteCount); //is corner if most pixels around are white if (whiteCount >= 5) { //is corner } else { tb.pixels_in_bb[j] = new Vector2(-1, -1); } } //remove points that are too close together for (int p = tb.pixels_in_bb.Count - 1; p > 0; p--) { if (tb.pixels_in_bb[p] == new Vector2(-1, -1)) { tb.pixels_in_bb.Remove(tb.pixels_in_bb[p]); } } }
/**********************************************************************************/ // W O R K I N G F I L T E R /**********************************************************************************/ private void BlobDetectionFilter() { for (int i = 0; i < data.Length; i++) { avBrightness += ((data[i].r + data[i].g + data[i].b) / 3); } avBrightness /= c.Length; avBrightness *= 0.5f; //determine if pixels could be part of a blob for (int i = 0; i < data.Length; i++) { if ((data[i].r + data[i].g + data[i].b) / 3 < avBrightness) { c[i] = Color.black; GoodPixels[i] = true; } else { c[i] = Color.white; } } for (int i = 0; i < GoodPixels.Length; i++) { pixels_in_blob = new List <Vector2>(); if (GoodPixels[i]) { Vector3 Blob = BlobFind(i); if (Blob.z > 100) { Blobs[count * 3] = Blob.x; Blobs[(count * 3) + 1] = Blob.y; Blobs[(count * 3) + 2] = Blob.z; if (firstScan) { GameObject tempBlob = Instantiate(blob); blobs.Add(tempBlob); DSAR_Blob tb = tempBlob.GetComponent <DSAR_Blob>(); tb.blobID = count; tb.pixels_in_blob = pixels_in_blob; //check for corners //float avWhitePixels = CheckCornerColours2(tb); //check radius lines from center of blob Vector4 sValues = CheckRadiusOfShape(new Vector2((int)(Blob.x / Blob.z), (int)(Blob.y / Blob.z)), pixels_in_blob.Count); //place bounding box dot on each vertice for (int j = 0; j < tb.corner_pixels.Count; j++) { GameObject bbpoint = Instantiate(BB_Point); tempBlob.GetComponent <DSAR_Blob>().corner_gos.Add(bbpoint); bbpoint.transform.parent = tempBlob.transform; bbpoint.GetComponent <GUITexture>().pixelInset = new Rect(tb.corner_pixels[j].x - 10, tb.corner_pixels[j].y - 10, 20, 20); } //tempBlob.GetComponent<GUIText>().text = sValues.x + ", " + sValues.y + ", " + sValues.z + ", " + sValues.w; //determine what the shape is based on the values obtained above if (sValues == new Vector4(0, 0, 0, 0)) { //no shape tempBlob.GetComponent <GUIText>().text = ""; } else if (sValues.x == 12) { //circle or square if (Mathf.Abs(sValues.z - sValues.w) < 1f) { //circle or square, check corners if (tb.corner_pixels.Count >= 4) { //square tempBlob.GetComponent <GUIText>().text = "Square"; } else { //circle tempBlob.GetComponent <GUIText>().text = "Circle"; } } else { //square tempBlob.GetComponent <GUIText>().text = "Square"; } } else if (sValues.y == 1) { //rectangle tempBlob.GetComponent <GUIText>().text = "Rectangle"; } else { //triangle tempBlob.GetComponent <GUIText>().text = "Triangle"; } tempBlob.name = tempBlob.GetComponent <GUIText>().text; //tempBlob.GetComponent<GUIText>().text = sValues.x + ", " + sValues.y + ", " + sValues.z + ", " + sValues.w; /* * * //Triangle * // s = 1, 2, 3 * // w = 2 * //Square * // s = 9, 10 * // s = 2, 3 * //Circle * // s = 6, 7, 8 * // w = 2 * //Rectangle * // s = 2, 3 * // w = 2, 3 * * switch(tb.corner_pixels.Count){ * case 0: * tempBlob.GetComponent<GUIText>().text = "Circle"; * break; * case 1: * tempBlob.GetComponent<GUIText>().text = "Circle"; * break; * case 2: * tempBlob.GetComponent<GUIText>().text = "Circle"; * break; * case 3: * tempBlob.GetComponent<GUIText>().text = "Triangle"; * break; * case 4: * tempBlob.GetComponent<GUIText>().text = "Rectangle"; * break; * case 5: * tempBlob.GetComponent<GUIText>().text = "Pentagon"; * break; * case 6: * tempBlob.GetComponent<GUIText>().text = "Hexagon"; * break; * case 7: * tempBlob.GetComponent<GUIText>().text = "Heptagon"; * break; * case 8: * tempBlob.GetComponent<GUIText>().text = "Octogon"; * break; * case 9: * tempBlob.GetComponent<GUIText>().text = "Nonagon"; * break; * case 10: * tempBlob.GetComponent<GUIText>().text = "Decagon"; * break; * default: * tempBlob.GetComponent<GUIText>().text = "???"; * break; * } * * * Vector2[] tempBB = new Vector2[tb.pixels_in_blob.Count]; * tb.numPixelsBB = chainHull_2D(tb.pixels_in_blob.ToArray(), tb.pixels_in_blob.Count, tempBB); * * for(int j=0; j<tb.numPixelsBB; j++){ * tb.pixels_in_bb.Add(tempBB[j]); * } * * CheckCornerColours(tb); * //CheckPointAngle(tb, 30); * CheckPointDistance(tb, 0.001f); * //int sValues = CheckRadiusOfShape(new Vector2((int)(Blob.x/Blob.z), (int)(Blob.y/Blob.z))); * * //place bounding box dot on each vertice * for(int j=0; j<tb.pixels_in_bb.Count; j++){ * GameObject bbpoint = Instantiate(BB_Point); * bbpoint.GetComponent<GUITexture>().pixelInset = new Rect(tb.pixels_in_bb[j].x-10, tb.pixels_in_bb[j].y-10, 20, 20); * } * * tempBlob.GetComponent<GUIText>().text = "C: " + tb.pixels_in_bb.Count + ", S: " + sValues; * * //for(int k=0; k<tb.numPixelsBB-1; k++){ * //if(k==(tb.numPixelsBB-1)) * // VectorLine.SetLine(Color.green, tb.pixels_in_bb[k], tb.pixels_in_bb[0]); * //else * // VectorLine.SetLine(Color.green, tb.pixels_in_bb[k], tb.pixels_in_bb[k+1]); * //} * * //VectorLine.SetLine (Color.green, topLeft, topRight); * //VectorLine.SetLine (Color.green, topRight, rightTop); * //VectorLine.SetLine (Color.green, rightTop, rightBottom); * //VectorLine.SetLine (Color.green, rightBottom, bottomRight); * //VectorLine.SetLine (Color.green, bottomRight, bottomLeft); * //VectorLine.SetLine (Color.green, bottomLeft, leftBottom); * //VectorLine.SetLine (Color.green, leftBottom, leftTop); * //VectorLine.SetLine (Color.green, leftTop, topLeft); */ } DSAR_Blob curBlobScript = blobs[count].GetComponent <DSAR_Blob>(); curBlobScript.blob_velocity = new Vector2(curBlobScript.blob_position_center.x - (Blob.x / Blob.z), curBlobScript.blob_position_center.y - (Blob.y / Blob.z)); curBlobScript.blob_position_center = new Vector2((int)(Blob.x / Blob.z), (int)(Blob.y / Blob.z)); curBlobScript.blob_size = (int)Blob.z; GUITexture curBlobIndicator = blobs[count].GetComponent <GUITexture>(); curBlobIndicator.pixelInset = new Rect((int)(Blob.x / Blob.z) - 10, (int)(Blob.y / Blob.z) - 10, 20, 20); blobs[count].GetComponent <GUIText>().pixelOffset = new Vector2((int)(Blob.x / Blob.z), (int)(Blob.y / Blob.z)); count++; } } } pixelCheckTexture.SetPixels32(newTexColours); pixelCheckTexture.Apply(); targetGUI.texture = pixelCheckTexture; for (int i = 0; i < data.Length; i++) { Blobs[i] = 0; } GoodPixels = new bool[data.Length]; count = 0; firstScan = false; }