Skip to content

Commit 0a4d93e

Browse files
author
Brianna Lam
committed
Finished commenting getWhiteDot and defined some constants to remove magic numbers. Made code cleaner and more comprehensible.
1 parent f47611e commit 0a4d93e

File tree

1 file changed

+151
-17
lines changed
  • dvs-core/src/main/java/com/ucsd/globalties/dvs/core

1 file changed

+151
-17
lines changed

dvs-core/src/main/java/com/ucsd/globalties/dvs/core/Pupil.java

Lines changed: 151 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,28 @@ public class Pupil {
3131
@Getter
3232
private Eye eye; // the Eye from which this Pupil was derived
3333
@Getter
34+
/*Mat is a n-dimensional dense array that can store images, 2d complex arrays,
35+
* or a matrix of 16-bit signed integers for algebraic operations*/
3436
private Mat mat;
3537

3638
private WhiteDot whiteDot;
39+
40+
/*Important Values used in the code*/
41+
42+
/*RBG values*/
43+
public int WHITE = 255;
44+
/*This value might need to be alternated to allow the threshold effect to make the cresent
45+
* more prominent*/
46+
public int GRAY = 240;
47+
public int BLACK = 0;
48+
49+
/*Values for contouring*/
50+
public int fillCONTOURS = -1;
51+
public int contourTHICKNESS = -1;
52+
53+
/*Values for the circle*/
54+
public int circleTHICKNESS = 1;
55+
3756

3857
public Pupil(Eye eye, Mat mat) {
3958
this.eye = eye;
@@ -46,80 +65,194 @@ public Pupil(Eye eye, Mat mat) {
4665
* information to detect diseases. I don't think we really need to crop it out
4766
* of the image; the positional information will probably suffice.
4867
*
49-
* @return a WhiteDot object identifying the white dot's positional information relative to the pupil
68+
* @return a WhiteDot object identifying the white dot's positional information
69+
* relative to the pupil
5070
*/
5171
public WhiteDot getWhiteDot() {
5272
if (whiteDot != null) {
5373
return whiteDot;
5474
}
55-
//random code so that debug output will not override each other
75+
/*random code so that debug output will not override each other*/
5676
int code = (new Random()).nextInt();
77+
78+
/*Creating the image container to hold the imported image*/
5779
Mat src = new Mat();
80+
/*Copies the data from the private mat variable into src*/
5881
mat.copyTo(src);
82+
/*This will hold the gray-scaled image*/
5983
Mat gray = new Mat();
84+
85+
/*Converts the image from src into gray-scale and stores it into gray*/
6086
Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
87+
88+
/*This is the test image to test if the image will be converted to grayscale*/
6189
//Highgui.imwrite("gray-test.jpg", gray);
62-
// threshold the image for white values
63-
Double thresh = Imgproc.threshold(gray, gray, 240, 255, Imgproc.THRESH_BINARY);
90+
91+
/*Applies the threshold effect on the image for white values. Takes the image in
92+
* gray and detects pixels of GRAY and turns it WHITE, and it stores it back to gray.*/
93+
Double thresh = Imgproc.threshold(gray, gray, GRAY, WHITE, Imgproc.THRESH_BINARY);
94+
95+
/*Test that checks if the image has the threshold effect applied*/
6496
//Highgui.imwrite("thresh-test.jpg", gray);
97+
98+
/*Creating a list to hold the values of the detected contours. Each contour
99+
* found will be stored as a vector of points*/
65100
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
101+
102+
/*This will find the contours in a cloned gray and store the points of those contours
103+
* in contours list.
104+
* Imgproc.RETR_LIST = 1; Retrieves all of the contours without establishing any hierarchical
105+
* relationships
106+
* Imgproc.CHAIN_APPROX_SIMPLE = 2; Stores absolutely all the contour points.
107+
* These are static final int constants defined in Imgproc object.
108+
* */
66109
Imgproc.findContours(gray.clone(), contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
67-
Imgproc.drawContours(gray, contours, -1, new Scalar(255, 255, 255), -1);
110+
111+
/*This draws the draws contour outlines in the image if thickness >= 0 or fills the area
112+
* bounded by the contours if thickness<0. thickness is last parameter. fillCONTOURS will allow
113+
* all the contours to be drawn.*/
114+
Imgproc.drawContours(gray, contours, fillCONTOURS, new Scalar(WHITE, WHITE, WHITE), contourTHICKNESS);
115+
116+
/*If the contours list has nothing in it, then it means that the patient does not have contours in
117+
* their image. Stop function.*/
68118
if (contours.isEmpty()) {
69119
log.error("No contours found for this pupil.");
70120
return null;
71121
}
122+
123+
124+
/*Creating a point representing a location in (x,y) coordinate space, specified in integer precision.
125+
* This sets up a pointer to point to the very center of the image. As you can see, we have x point
126+
* to half of mat(the image) and for the y axis to half of mat's height. TODO*/
72127
java.awt.Point pupilCenter = new java.awt.Point(mat.width() / 2, mat.height() / 2);
128+
129+
130+
/*List holding the distants in the contours. This will hold pairs(left, right), where left is the contour
131+
* and right is the contour's distance from the center of the pupil*/
73132
List<Pair<MatOfPoint, Double>> contourDistances = new ArrayList<>(contours.size());
74-
// Populate a List of Pairs, where pair.getLeft() is the contour and pair.getRight() is the contour's
75-
// distance from the center of the pupil
133+
134+
/*For-loop will go through the contours list and evaluate each of the contour points found in the list.
135+
* It will store the distance (in contourDistances) it finds between the center of the pupil to the
136+
* contours it locates. */
76137
for (int i = 0; i < contours.size(); i++) {
138+
139+
/*Creates rectangle object. boundingRect calculates the up-right bounding rectangle of a point set using
140+
* the value count in contours.*/
77141
Rect rect = Imgproc.boundingRect(contours.get(i));
78-
Core.circle(src, new Point(rect.x + rect.width / 2, rect.y + rect.width / 2), rect.width / 2, new Scalar(255, 0, 0), 1);
142+
143+
/*To obtain the radius for the circle*/
79144
int radius = rect.width / 2;
145+
146+
/*Creates a circle using the information from the rectangle object.
147+
* circle(Mat img, Point center, int radius, Scalar color, int thickness).
148+
* Thickness is the outline of the circle.*/
149+
Core.circle(src, new Point(rect.x + rect.width / 2, rect.y + rect.width / 2), radius,
150+
new Scalar(WHITE, BLACK, BLACK), circleTHICKNESS);
151+
152+
/*Points to the center of the circle*/
80153
java.awt.Point center = new java.awt.Point(rect.x + radius, rect.y + radius);
154+
155+
/*Gets the distance between the pupil to the contour and stores it as pairs in the
156+
* contourDistance list. First element is the value of the contour, then the second is
157+
* the distance between the pupil center to the contour.*/
81158
contourDistances.add(new Pair<>(contours.get(i), pupilCenter.distanceSq(center)));
82159
}
83-
// sort the contours based on the distance from the center of the pupil (ascending)
160+
161+
/*sort the contours based on the distance from the center of the pupil (ascending)*/
84162
contourDistances.sort(contourCompare);
163+
164+
/*Empty pair object*/
85165
Pair<MatOfPoint, Double> whiteDotPair = null;
86166

87-
// Find the closest contour that matches certain criteria (currently checks for size)
88-
for (Pair<MatOfPoint, Double> pair : contourDistances) {
167+
168+
/*For-each loop: For each pair found in the contourDistances list, find the closest contour that matches
169+
* certain criteria (currently checks for size)*/
170+
for (Pair<MatOfPoint, Double> pair: contourDistances) {
171+
172+
/* pair.getLeft() is the contour and pair.getRight() is the contour's distance from the
173+
* center of the pupil*/
174+
/*This calculates the contour area and stores it into area*/
89175
double area = Imgproc.contourArea(pair.getLeft());
176+
177+
/*This will print out that the white dot is currently at the contour's distance from the center of
178+
* the pupil. It will also tell us the current area of the contour*/
90179
log.info("whiteDot distance: {}, area: {}", pair.getRight(), area);
91180

92-
if (area < 10 || area > 200.0) { // basic bounds checking, may need some tuning
181+
/*NEEDS TUNING: This is suppose to check the bounds*/
182+
if (area < 10 || area > 200.0) {
183+
/*If the area falls between these ranges, then reiterate up the loop and don't evaluate whats
184+
* below this if statement.*/
93185
continue;
94186
}
187+
188+
/*If the area doesn't call between those ranges, then continue onto the loop and break.*/
189+
190+
/*Stores the pair with the information about the contour's area and the distance between the
191+
* contour and the center of the pupil into the whiteDotPair.*/
95192
whiteDotPair = pair;
193+
194+
/*Prints out information about the area of the found contour.*/
96195
log.info("selected pair with area: " + area);
196+
197+
/*Escape the for-loop*/
97198
break;
98199
}
200+
201+
/*If whiteDotPair is null, meaning that the area was never within the correct ranges, then we can't
202+
* detect the white dot in the eye.*/
99203
if (whiteDotPair == null) {
100204
log.error("[WhiteDot Detection] Unable to find suitable white dot");
101205
return null;
102206
}
103-
MatOfPoint whiteDotContour = whiteDotPair.getLeft(); // assume white dot is the contour closest to the center of the image
104207

105-
// debug test
208+
/* whiteDotPair.getLeft() is the contour(of type MatOfPoint) and whiteDotPair.getRight() is the contour's
209+
* distance from the center of the pupil*/
210+
/*assume white dot is the contour closest to the center of the image*/
211+
MatOfPoint whiteDotContour = whiteDotPair.getLeft();
212+
213+
/*DEBUGGING TEST*/
214+
/*This creates a retangle by calculating the up-right bounding rectangle of a point set.
215+
* The function calculates and returns the minimal up-right bounding rectangle for the
216+
* specified point set.
217+
* Basically, creating a retangle out of the value of the contour*/
106218
Rect rect = Imgproc.boundingRect(whiteDotContour);
219+
220+
/*Tester*/
107221
//Highgui.imwrite("test" + code + ".jpg", src);
222+
223+
/*Calculates area by pi * ((rectangle's width / 2)^2) */
108224
double wdarea = Math.PI * Math.pow(rect.width / 2, 2);
225+
109226
//need: distance from white dot center to pupil center,
110227
// angle between white dot center and pupil center
228+
229+
/*Radius to center*/
111230
int radius = rect.width / 2;
231+
232+
/*Make new pointer point to the center of the rectangle*/
112233
java.awt.Point whiteDotCenter = new java.awt.Point(rect.x + radius, rect.y + radius);
234+
235+
/*Calculates distance between the pupil center and the white dot*/
113236
double distance = pupilCenter.distance(whiteDotCenter);
237+
238+
/*Calculate the difference in the distance between the white dot and the pupil center along the x-axis*/
114239
double xDist = whiteDotCenter.x - pupilCenter.x;
240+
241+
/*If the xdistance is greater than the distance between the pupil center and the white dot is greater,
242+
* then it means that the distance calculation was incorrect*/
115243
if (xDist > distance) {
116244
log.error("[WhiteDot Detection] unfulfilled invariant: adjacent edge of triangle is bigger than hypotenuse");
117245
return null;
118246
}
247+
/*Print out information about the x distance and the distance between pupil center and white dot.*/
119248
log.info("[WhiteDot Detection] Computing angle for xDist: {}, dist: {}", xDist, distance);
249+
/*Calculates the arccosine of the x-distance and the distance to find the y-distance or height.*/
120250
double angle = Math.acos(xDist / distance);
121-
122-
log.info("[WhiteDot Detection] computed white dot with distance: {}, angle: {}, area: {}", distance, Math.toDegrees(angle), wdarea);
251+
252+
/*Print information about the white dot detection*/
253+
log.info("[WhiteDot Detection] computed white dot with distance: {}, angle: {}, area: {}", distance,
254+
Math.toDegrees(angle), wdarea);
255+
/*Sets current info about white dot to a WhiteDot object (defined in WhiteDot.java)*/
123256
this.whiteDot = new WhiteDot(distance, wdarea, angle);
124257
return whiteDot;
125258
}
@@ -136,6 +269,7 @@ public int compare(Pair<MatOfPoint, Double> p1, Pair<MatOfPoint, Double> p2) {
136269
* @return
137270
*/
138271
public double getCrescent() {
272+
139273
return 0;
140274
}
141275

@@ -152,4 +286,4 @@ public double getArea() {
152286
double radius = mat.width() / 2;
153287
return Math.PI * Math.pow(radius, 2);
154288
}
155-
}
289+
}

0 commit comments

Comments
 (0)