3DSelfie  Hansjoerg Hofer (1026632), Sebastian Metzler (0927550)
Histogram.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using System.Collections;
3 using System.Collections.Generic;
4 
5 public class Histogram {
6 
7 
8  private Rect a;
9 
10 
11  public float[] xHist;
12  public float[] yHist;
13 
14  public int yOffset = 0;
15 
16  int width;
17  int height;
18 
29  public Histogram(Texture2D binaryImage, Rect a1){
30 
31  width = binaryImage.width;
32  height = binaryImage.height;
33 
34  this.a = new Rect (a1.xMin* width, a1.yMin * height, a1.width * width, a1.height * height);
35 
36 
37  xHist = new float[(int)a.width];
38  yHist = new float[(int)a.height];
39 
40  Color[] c = binaryImage.GetPixels((int)a.xMin, (int)a.yMin, (int)a.width, (int)a.height, 0);
41 
42  for (int y = 0; y < (int)a.height; y++) {
43  for (int x = 0; x < (int)a.width; x++) {
44  Color cp = c[y * (int)a.width + x];
45 
46  xHist[x] = xHist[x] + cp.r;
47  yHist[y] = yHist[y] + cp.r;
48  }
49 
50  }
51 
52  }
53 
61  public void draw(){
62 
63  int aMinX = (int)a.xMin;
64 
65  for(int x = aMinX; x< aMinX + xHist.Length; x++)
66  Debug.DrawLine (new Vector3 ((x)*0.6f/width + 0.2f, 1.0f, -1), new Vector3 ((x)*0.6f/width + 0.2f, 1.0f - xHist[x - aMinX]*0.2f/height, -1), Color.red, 30.0f, false);
67 
68  int aMinY = (int)a.yMin;
69 
70  for(int y = (int)aMinY; y< (int)aMinY + yHist.Length; y++)
71  Debug.DrawLine (new Vector3 (0.2f, (y + yOffset)*1.0f/height, -1), new Vector3 (0.2f + yHist[y - aMinY]*0.2f/width, (y + yOffset)*1.0f/height, -1), Color.green, 30.0f, false);
72 
73  }
74 
84  public List<KeyValuePair<int, float>> findMin(float[] hist){
85 
86  List<KeyValuePair<int, float>> list = new List<KeyValuePair<int, float>>();
87 
88  for(int i = 1; i<hist.Length-1; i++){
89  if(hist[i] <= hist[i-1] && hist[i]<= hist[i+1]){
90  list.Add(new KeyValuePair<int, float>(i, hist[i]));
91 
92 // Debug.Log(i + ":" + hist[i-1] + " " + hist[i] + " " + hist[i+1]);
93  }
94  }
95 
96  list.Sort(CompareValue);
97 
98  return list;
99  }
100 
101 
111  public List<KeyValuePair<int, float>> findMax(float[] hist){
112 
113  List<KeyValuePair<int, float>> list = new List<KeyValuePair<int, float>>();
114 
115  for(int i = 1; i<hist.Length-1; i++){
116  if(hist[i] >= hist[i-1] && hist[i]>= hist[i+1]){
117 
118  list.Add(new KeyValuePair<int, float>(i, hist[i]));
119 
120  }
121  }
122 
123  list.Sort(CompareValue);
124 
125  return list;
126  }
127 
137  public List<KeyValuePair<int, float>> findCluster(float[] hist){
138 
139 
140 // bool newCluster = true;
141  float clusterSize = 0f;
142  int clusterStart = 0;
143 
144 
145  List<KeyValuePair<int, float>> list = new List<KeyValuePair<int, float>>();
146 
147  for (int i = 0; i<hist.Length; i++) {
148  float f = hist[i];
149 
150 
151  if(f>0f && clusterSize == 0f){
152 // if(newCluster){
153 // newCluster = false;
154  clusterStart = i;
155 // }
156 
157  }
158 
159  clusterSize++;
160 
161  if(clusterSize > 0f && ( f == 0f || i == hist.Length - 1)){
162 // if(!newCluster){
163 //
164 // newCluster = true;
165 // Debug.Log(clusterSize + " " + clusterStart + " " + i);
166  list.Add(new KeyValuePair<int, float>(clusterStart + (i - clusterStart)/2, clusterSize));
167  clusterSize = 0f;
168 
169 // }
170 
171  }
172  }
173 
174  list.Sort(CompareValue);
175 
176  return list;
177 
178  }
179 
191  public int[] findSimilarArea(int index, float eps, float[] hist){
192  int upperBound = index;
193  int lowerBound = index;
194 
195  for (int i=index; i<hist.Length && hist[i] -eps < hist[index]; i++) {
196  upperBound = i;
197  }
198 
199  for (int i=index; i>=0 && hist[i] -eps < hist[index] ; i--) {
200  lowerBound = i;
201  }
202 
203  return new int[]{lowerBound, upperBound};
204  }
205 
215  int CompareKey(KeyValuePair<int, float> a, KeyValuePair<int, float> b)
216  {
217  return a.Key.CompareTo(b.Key);
218  }
219 
229  int CompareValue(KeyValuePair<int, float> a, KeyValuePair<int, float> b)
230  {
231  return a.Value.CompareTo(b.Value);
232  }
233 }
Histogram(Texture2D binaryImage, Rect a1)
A histogram of a binary image.
Definition: Histogram.cs:29
List< KeyValuePair< int, float > > findCluster(float[] hist)
Finds all clusters in the given histogram.
Definition: Histogram.cs:137
int yOffset
Definition: Histogram.cs:14
float[] xHist
Definition: Histogram.cs:11
List< KeyValuePair< int, float > > findMin(float[] hist)
Finds all local minima in the given histogram.
Definition: Histogram.cs:84
float[] yHist
Definition: Histogram.cs:12
List< KeyValuePair< int, float > > findMax(float[] hist)
Finds all local maxima in the given histogram.
Definition: Histogram.cs:111
void draw()
Draws the histogram.
Definition: Histogram.cs:61
int[] findSimilarArea(int index, float eps, float[] hist)
Finds all surrounding Values, which are not larger than the value with the given index by eps in the ...
Definition: Histogram.cs:191