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);
30 for( int x = 0; x < 640; ++x)
32 for (int y = 0; y < 480; ++y)
35 for( int i = 0; i < 3; ++i)
37 s.val[i] = 210 + (rand() % 1000) * 1e-0 - (rand() % 1000) * 1e-0;
38 // Produce an exponential decay around left edge
40 s.val[i] *= exp( (x - test_left) / 25);
42 s.val[i] *= exp( (test_left - x) / 25);
43 // Produce an exponential decay around right edge
44 else if( x < test_right)
45 s.val[i] *= exp( (x - test_right) / 25);
47 s.val[i] *= exp( (test_right - x) / 25);
49 cvSet2D(g_dataRGB,y,x,s);
50 // if( s.val[0] > 200)
51 // printf("row: %d, col: %d, %f\n", y, x, s.val[0]);
57 g_data = cvCreateMat(g_dataRGB->rows,g_dataRGB->cols,CV_8UC1); //IPL_DEPTH_8U?
59 cvCvtColor(g_dataRGB,g_data,CV_RGB2GRAY);
63 * Initialise the dilatometer
65 void Dilatometer_Init()
68 // Make an initial reading (will allocate memory the first time only).
73 * Cleanup Interferometer stuff
75 void Dilatometer_Cleanup()
78 cvReleaseMat(&g_data);
80 if (g_capture != NULL)
81 cvReleaseCapture(&g_capture);
86 * Get an image from the Dilatometer
88 static void Dilatometer_GetImage()
90 //Need to implement camera
94 static void Dilatometer_GetImageTest( )
97 Dilatometer_TestImage();
102 * Read the dilatometer; gets the latest image, processes it, THEN DOES WHAT
103 * @param samples - Number of rows to scan (increasing will slow down performance!)
104 * @returns the average width of the can
106 double Dilatometer_Read(int samples)
108 //Get the latest image
109 //Dilatometer_GetImage();
111 Dilatometer_GetImageTest();
113 int width = g_data->cols;
114 int height = g_data->rows;
115 // If the number of samples is greater than the image height, sample every row
116 if( samples > height)
118 //Log(LOGNOTE, "Number of samples is greater than the dilatometer image height, sampling every row instead.\n");
122 // Stores the width of the can at different sample locations. Not necessary unless we want to store this information
123 //double widths[samples];
124 // The average width of the can
125 double average_width;
127 for (int i=0; i<samples; i++)
129 // Contains the locations of the 2 edges
130 double edges[2] = {0.0,0.0};
131 int pos = 0; // Position in the edges array (start at left edge)
132 int num = 0; // Keep track of the number of columns above threshold
134 // Determine the position in the rows to find the edges.
135 sample_height = ceil(height * (i + 1) / samples) -1;
136 //printf("sample height is %d\n", sample_height);
138 //CvScalar test = cvGet2D(g_data, 150,300);
139 //printf("test is %f,%f,%f,%f\n", test.val[0], test.val[1], test.val[2], test.val[3]);
142 for ( int col = 0; col < width; col++)
144 CvScalar value = cvGet2D(g_data, sample_height, col);
145 if( value.val[0]> THRES)
147 edges[pos] += (double) col;
150 // If num > 0 and we're not above threshold, we have left the threshold of the edge
153 // Find the mid point of the edge
157 pos = 1; // Move to the right edge
161 break; // Exit the for loop
164 // Determine the width of the can at this row
165 //widths[i] = edges[1] - edges[0];
166 average_width += (edges[1] - edges[0]);
168 average_width /= (double) samples;
169 return average_width;
173 * For testing purposes
175 int main(int argc, char ** argv)
177 //cvNamedWindow( "display", CV_WINDOW_AUTOSIZE );// Create a window for display.
178 //gettimeofday(&start, NULL);
183 cvNamedWindow( "display", CV_WINDOW_AUTOSIZE);
184 cvShowImage("display", g_data);
187 for( int i = 0; i < 20; ++i)
189 test_left -= i * (rand() % 1000) * 1e-3;
190 test_right += i * (rand() % 1000) * 1e-3;
192 //Make sure left and right positions are sane
195 if( test_right > 639)
197 if( test_left > test_right)
199 int tmp = test_right;
200 test_right = test_left;
204 width = Dilatometer_Read(5);
205 cvNamedWindow( "display", CV_WINDOW_AUTOSIZE);
206 cvShowImage("display", g_data);
208 double expected = test_right - test_left;
209 double perc = 100 * (expected - width) / expected;
210 printf("%d: Left: %.4f. Width: %.4f.\n Right: %.4f. Expected: %.4f. Percentage: %.4f\n", i, test_left, width, test_right, expected, perc);