3 * @purpose Implementation of dilatometer related functions
8 #include "dilatometer.h"
12 static double test_left, test_right;
14 /** Buffer for storing image data. Stored as a **/
15 static CvMat * g_data = NULL;
18 /** Camera capture pointer **/
19 static CvCapture * g_capture = NULL;
22 * Create a test image using left as left edge and right as right edge positions
24 void Dilatometer_TestImage()
28 g_dataRGB = cvCreateMat(480, 640, CV_8UC3);
29 //Make sure left and right positions are sane
34 if( test_left > test_right)
37 test_right = test_left;
41 for( int x = 0; x < 640; ++x)
43 for (int y = 0; y < 480; ++y)
46 for( int i = 0; i < 3; ++i)
48 s.val[i] = 220 + (rand() % 1000) * 1e-2 - (rand() % 1000) * 1e-2;
49 // Produce an exponential decay around left edge
51 s.val[i] *= exp( (x - test_left) / 25);
53 s.val[i] *= exp( (test_left - x) / 25);
54 // Produce an exponential decay around right edge
55 else if( x < test_right)
56 s.val[i] *= exp( (x - test_right) / 25);
58 s.val[i] *= exp( (test_right - x) / 25);
60 cvSet2D(g_dataRGB,y,x,s);
61 // if( s.val[0] > 200)
62 // printf("row: %d, col: %d, %f\n", y, x, s.val[0]);
68 g_data = cvCreateMat(g_dataRGB->rows,g_dataRGB->cols,CV_8UC1); //IPL_DEPTH_8U?
70 cvCvtColor(g_dataRGB,g_data,CV_RGB2GRAY);
74 * Initialise the dilatometer
76 void Dilatometer_Init()
79 // Make an initial reading (will allocate memory the first time only).
84 * Cleanup Interferometer stuff
86 void Dilatometer_Cleanup()
89 cvReleaseMat(&g_data);
91 if (g_capture != NULL)
92 cvReleaseCapture(&g_capture);
97 * Get an image from the Dilatometer
99 static void Dilatometer_GetImage()
101 //Need to implement camera
105 static void Dilatometer_GetImageTest( )
108 Dilatometer_TestImage();
113 * Read the dilatometer; gets the latest image, processes it, THEN DOES WHAT
114 * @param samples - Number of rows to scan (increasing will slow down performance!)
115 * @returns the average width of the can
117 double Dilatometer_Read(int samples)
119 //Get the latest image
120 //Dilatometer_GetImage();
122 Dilatometer_GetImageTest();
124 int width = g_data->cols;
125 int height = g_data->rows;
126 // If the number of samples is greater than the image height, sample every row
127 if( samples > height)
129 //Log(LOGNOTE, "Number of samples is greater than the dilatometer image height, sampling every row instead.\n");
133 // Stores the width of the can at different sample locations. Not necessary unless we want to store this information
134 //double widths[samples];
135 // The average width of the can
136 double average_width;
138 for (int i=0; i<samples; i++)
140 // Contains the locations of the 2 edges
141 double edges[2] = {0.0,0.0};
142 int pos = 0; // Position in the edges array (start at left edge)
143 int num = 0; // Keep track of the number of columns above threshold
145 // Determine the position in the rows to find the edges.
146 sample_height = ceil(height * (i + 1) / samples) -1;
147 //printf("sample height is %d\n", sample_height);
149 //CvScalar test = cvGet2D(g_data, 150,300);
150 //printf("test is %f,%f,%f,%f\n", test.val[0], test.val[1], test.val[2], test.val[3]);
153 for ( int col = 0; col < width; col++)
155 CvScalar value = cvGet2D(g_data, sample_height, col);
156 if( value.val[0]> THRES)
158 edges[pos] += (double) col;
161 // If num > 0 and we're not above threshold, we have left the threshold of the edge
164 // Find the mid point of the edge
168 pos = 1; // Move to the right edge
172 break; // Exit the for loop
175 // Determine the width of the can at this row
176 //widths[i] = edges[1] - edges[0];
177 average_width += (edges[1] - edges[0]);
179 average_width /= (double) samples;
180 return average_width;
184 * For testing purposes
186 int main(int argc, char ** argv)
188 //cvNamedWindow( "display", CV_WINDOW_AUTOSIZE );// Create a window for display.
189 //gettimeofday(&start, NULL);
194 cvNamedWindow( "display", CV_WINDOW_AUTOSIZE);
195 cvShowImage("display", g_data);
198 for( int i = 0; i < 20; ++i)
200 test_left -= i * (rand() % 1000) * 1e-3;
201 test_right += i * (rand() % 1000) * 1e-3;
202 width = Dilatometer_Read(5);
203 cvNamedWindow( "display", CV_WINDOW_AUTOSIZE);
204 cvShowImage("display", g_data);
206 double expected = test_right - test_left;
207 double perc = 100 * (expected - width) / expected;
208 printf("%d: Left: %.4f. Width: %.4f.\n Right: %.4f. Expected: %.4f. Percentage: %.4f\n", i, test_left, width, test_right, expected, perc);