3 * @purpose Implementation of dilatometer related functions
8 #include "dilatometer.h"
12 static double test_left, test_right;
14 // Canny Edge algorithm variables
17 int const max_lowThreshold = 100;
21 /** Buffer for storing image data. Stored as a **/
22 static CvMat * g_srcRGB = NULL; // Source Image
23 static CvMat * g_srcGray = NULL; // Gray scale of source image
24 static CvMat * g_edges = NULL; // Detected Edges
25 static CvMat * g_data = NULL; // Image to mask edges onto
28 /** Camera capture pointer **/
29 static CvCapture * g_capture = NULL;
32 * Create a test image using left as left edge and right as right edge positions
34 void Dilatometer_TestImage()
37 g_srcRGB = cvCreateMat(480, 640, CV_8UC3);
39 for( int x = 0; x < 640; ++x)
41 for (int y = 0; y < 480; ++y)
44 for( int i = 0; i < 3; ++i)
46 s.val[i] = 210 + (rand() % 1000) * 1e-0 - (rand() % 1000) * 1e-0;
47 // Produce an exponential decay around left edge
49 s.val[i] *= exp( (x - test_left) / 25);
51 s.val[i] *= exp( (test_left - x) / 25);
52 // Produce an exponential decay around right edge
53 else if( x < test_right)
54 s.val[i] *= exp( (x - test_right) / 25);
56 s.val[i] *= exp( (test_right - x) / 25);
58 cvSet2D(g_srcRGB,y,x,s);
59 // if( s.val[0] > 200)
60 // printf("row: %d, col: %d, %f\n", y, x, s.val[0]);
66 g_data = cvCreateMat(g_srcRGB->rows,g_srcRGB->cols,CV_8UC1); //IPL_DEPTH_8U?
68 cvCvtColor(g_srcRGB,g_data,CV_RGB2GRAY);
72 * Initialise the dilatometer
74 void Dilatometer_Init()
77 // Make an initial reading (will allocate memory the first time only).
82 * Cleanup Interferometer stuff
84 void Dilatometer_Cleanup()
87 cvReleaseMat(&g_data);
89 if (g_capture != NULL)
90 cvReleaseCapture(&g_capture);
95 * Get an image from the Dilatometer
97 static void Dilatometer_GetImage()
99 //Need to implement camera
102 void CannyThreshold()
107 g_data = cvCreateMat(g_srcGray->rows,g_srcGray->cols,CV_8UC1);
110 if ( g_edges == NULL)
112 g_edges = cvCreateMat(g_srcGray->rows,g_srcGray->cols,CV_8UC1);
116 cvShowImage("display", g_srcGray);
118 // Reduce noise with a kernel 3x3. Input the grayscale source image, output to edges. (0's mean it's determined from kernel sizes)
119 cvSmooth( g_srcGray, g_edges, CV_GAUSSIAN, 9, 9 ,0 ,0 );
121 cvShowImage("display", g_edges);
124 // Find the edges in the image
125 cvCanny( g_edges, g_edges, lowThreshold, lowThreshold*ratio, kernel_size );
127 cvShowImage("display", g_edges);
130 // Mask the edges over G_data
131 //.copyTo( g_data, g_edges);
135 static void Dilatometer_GetImageTest( )
137 //Generates Test image
138 //Dilatometer_TestImage();
141 g_srcGray = cvLoadImageM ("testimage.jpg",CV_LOAD_IMAGE_GRAYSCALE );
147 * Read the dilatometer; gets the latest image, processes it, THEN DOES WHAT
148 * @param samples - Number of rows to scan (increasing will slow down performance!)
149 * @returns the average width of the can
151 double Dilatometer_Read(int samples)
153 //Get the latest image
154 //Dilatometer_GetImage();
156 Dilatometer_GetImageTest();
158 int width = g_srcGray->cols;
159 int height = g_srcGray->rows;
160 // If the number of samples is greater than the image height, sample every row
161 if( samples > height)
163 //Log(LOGNOTE, "Number of samples is greater than the dilatometer image height, sampling every row instead.\n");
167 // Stores the width of the can at different sample locations. Not necessary unless we want to store this information
168 //double widths[samples];
169 // The average width of the can
170 double average_width;
172 for (int i=0; i<samples; i++)
174 // Contains the locations of the 2 edges
175 double edges[2] = {0.0,0.0};
176 int pos = 0; // Position in the edges array (start at left edge)
177 int num = 0; // Keep track of the number of columns above threshold
179 // Determine the position in the rows to find the edges.
180 sample_height = ceil(height * (i + 1) / samples) -1;
181 //printf("sample height is %d\n", sample_height);
183 //CvScalar test = cvGet2D(g_srcGray, 150,300);
184 //printf("test is %f,%f,%f,%f\n", test.val[0], test.val[1], test.val[2], test.val[3]);
187 for ( int col = 0; col < width; col++)
189 CvScalar value = cvGet2D(g_srcGray, sample_height, col);
190 if( value.val[0]> THRES)
192 edges[pos] += (double) col;
195 // If num > 0 and we're not above threshold, we have left the threshold of the edge
198 // Find the mid point of the edge
202 pos = 1; // Move to the right edge
206 break; // Exit the for loop
209 // Determine the width of the can at this row
210 //widths[i] = edges[1] - edges[0];
211 average_width += (edges[1] - edges[0]);
213 average_width /= (double) samples;
214 return average_width;
218 * For testing purposes
220 int main(int argc, char ** argv)
222 //cvNamedWindow( "display", CV_WINDOW_AUTOSIZE );// Create a window for display.
223 //gettimeofday(&start, NULL);
228 // cvNamedWindow( "display", CV_WINDOW_AUTOSIZE);
229 // cvShowImage("display", g_data);
232 /*for( int i = 0; i < 20; ++i)
234 test_left -= i * (rand() % 1000) * 1e-3;
235 test_right += i * (rand() % 1000) * 1e-3;
237 //Make sure left and right positions are sane
240 if( test_right > 639)
242 if( test_left > test_right)
244 int tmp = test_right;
245 test_right = test_left;
249 width = Dilatometer_Read(5);
250 cvNamedWindow( "display", CV_WINDOW_AUTOSIZE);
251 cvShowImage("display", g_srcGray);
253 double expected = test_right - test_left;
254 double perc = 100 * (expected - width) / expected;
255 printf("%d: Left: %.4f. Width: %.4f.\n Right: %.4f. Expected: %.4f. Percentage: %.4f\n", i, test_left, width, test_right, expected, perc);