commit 91a0bfa9fd4c7c4b078b623fab8a9da5c7558195
Author: Nick Stanley <nstanley1020@gmail.com>
Date:   Mon Feb 27 22:26:37 2012 -0500

    Commented code!

diff --git a/vision/src/v2v3_converter.cpp b/vision/src/v2v3_converter.cpp
index 0edc1e8..564d578 100644
--- a/vision/src/v2v3_converter.cpp
+++ b/vision/src/v2v3_converter.cpp
@@ -1,17 +1,21 @@
-#include <ros/ros.h>
+#nclude <ros/ros.h>
 #include <stdio.h>
 #include <vision/TargetDescriptor.h>
 #include <image_transport/image_transport.h>
 #include <geometry_msgs/Point.h>
 #include <tf/transform_listener.h>
 
-ros::Publisher pub;
-ros::Publisher img_pub;
+ros::Publisher pub; // publishes Point
+ros::Publisher img_pub; //publishes Image with only blob showing
 
-int hk = 15; 
+int hk = 15; // target hsv values
 int sk = 255;
 int vk = 255;
 
+/* Given index p and image pointer img, 
+ * determines whether or not the pixel 
+ * in the indicated vicinity is equal to it.
+ */
 bool isRight(sensor_msgs::Image * img, int p){
 	return (img->data[p + 1] == img->data[p]);
 }
@@ -28,6 +32,8 @@ bool isDown(sensor_msgs::Image * img, int p){
 	return (img->data[p + img->width] == img->data[p]);
 }
 
+/* Checks for any adjacent pixel, ensuring a continuous blob.
+ */
 bool hasAnAdjacent(sensor_msgs::Image * image, int j){
 	int nl = image->height; // number of lines
 	int nc = image->width; 
@@ -54,16 +60,18 @@ bool hasAnAdjacent(sensor_msgs::Image * image, int j){
 void detectBlobs(sensor_msgs::Image * image, int * comX, int * comY, int * area){
 	int nl = image->height; // number of lines
 	int nc = image->step; // number of columns
-	int nChannels = image->step / image->width; // effective width
+	int nChannels = image->step / image->width; // effective width of each pixel
 	
 	// process image
 	for (int i = 0; i < nl; i++){
 		for (int j = 0; j < nc; j+= nChannels){
-			if (image->data[i * image->width + j] <= hk + 10 && image->data[j] >= hk - 10){
-				image->data[i * image->width + j] = 255;
+		    // if within bounds...	
+            if (image->data[i * image->width + j] <= hk + 10 && image->data[j] >= hk - 10){ 
+				image->data[i * image->width + j] = 255; // set each to 255
 				image->data[i * image->width + j+1] = 255;
 				image->data[i * image->width + j+2] = 255;
 			}
+            // otherwise set to 0
 			else {
 				image->data[i * image->width + j] = 0;
 				image->data[i * image->width + j+1] = 0;
@@ -72,31 +80,35 @@ void detectBlobs(sensor_msgs::Image * image, int * comX, int * comY, int * area)
 		}
 	}
 
-	int comXSum = 0;
-	int comYSum = 0;
-	int blobPixelCount = 0;
+
+    // The following is to find the average
+	int comXSum = 0; 
+	int comYSum = 0; 
+	int blobPixelCount = 0; 
 	for (int i = 0; i < image->width * image->height; i+=nChannels){
+        // if the pixel is not both the right hue and has adjacents, set to 0
 		if ( !(image->data[i] == 255 && hasAnAdjacent(image, i)) ){
 			image->data[i] = 0;
 			image->data[i + 1] = 0;
 			image->data[i + 2] = 0;
-       			continue;
 		}
-		blobPixelCount++;
-		comXSum += image->data[i] / 255;
-		comYSum += image->data[i] / 255;
+        else {
+		    blobPixelCount++;
+		    comXSum += image->data[i] / 255;
+		    comYSum += image->data[i] / 255;
+        }
 	}
   if (blobPixelCount != 0){
 	// get average
   	comXSum /= blobPixelCount;
   	comYSum /= blobPixelCount;
   }
-
+    // output results
 	*area = blobPixelCount;
-	*comX = comXSum;
-	*comY = comYSum;
+    *comX = comXSum;
+    *comY = comYSum;
 
-  img_pub.publish(*image);
+    img_pub.publish(*image); // publish new image of only black and white
 }
 
 void convertToHSV(sensor_msgs::Image * orig){
@@ -113,27 +125,8 @@ void convertToHSV(sensor_msgs::Image * orig){
       int min;
 
       // get min, max
-      if (orig->data[i] >= orig->data[i+1] && orig->data[i] >= orig->data[i+2]){
-        v = orig->data[i];
-        if (orig->data[i+1] >= orig->data[i+2]){
-            min = orig->data[i+2];
-        }
-        else min = orig->data[i+1];
-      }
-      else if (orig->data[i+1] >= orig->data[i] && orig->data[i+1] >= orig->data[i+2]){
-        v = orig->data[i+1];
-        if (orig->data[i] >= orig->data[i+2]){
-          min = orig->data[i+2];
-        }
-        else min = orig->data[i];
-      }
-      else {
-        v = orig->data[i+2];
-        if (orig->data[i] >= orig->data[i+1]){
-            min = orig->data[i+1];
-        }
-        else min = orig->data[i];
-      }
+      v = max(max(orig->data[i], orig->data[i+1]), orig->data[i+2]);
+      min = min(min(orig->data[i], orig->data[i+1]), orig->data[i+2]);
 
       // set s
       if (v != 0){
@@ -153,6 +146,7 @@ void convertToHSV(sensor_msgs::Image * orig){
         h = 240 + 60 * (orig->data[i] - orig->data[i + 1]) / s;
       }
       
+      // set values
       orig->data[i] = h;
       orig->data[i+1] = s;
       orig->data[i+2] = v;
@@ -174,7 +168,7 @@ void target_cb(const sensor_msgs::Image orig) {
     ROS_ERROR("%s", ex.what());
   }*/
 
-  // convert to OpenCV
+  // image types
 
   sensor_msgs::Image copy = orig;
   sensor_msgs::Image * source = &copy; // pointer to the image
@@ -196,7 +190,7 @@ void target_cb(const sensor_msgs::Image orig) {
   int distance;
 
   // give distance as inversely proportional to area, for (x,y,z) 
-  if (area > 10){
+  if (area > 1){
     
     distance = 100.0 / (float) area;
 
