3 * @brief Implementation of dilatometer related functions
8 #include "dilatometer.h"
11 /** Buffer for storing image data. Stored as a **/
12 static CvMat * g_data = NULL;
15 /** Camera capture pointer **/
16 static CvCapture * g_capture = NULL;
20 * Initialise the dilatometer
22 void Dilatometer_Init()
25 // Make an initial reading (will allocate memory the first time only).
30 * Cleanup Interferometer stuff
32 void Dilatometer_Cleanup()
35 cvReleaseMat(&g_data);
37 if (g_capture != NULL)
38 cvReleaseCapture(&g_capture);
43 * Get an image from the Dilatometer
45 static void Dilatometer_GetImage()
47 //Need to supply test image
49 //Need to implement camera
52 * Read the dilatometer; gets the latest image, processes it, THEN DOES WHAT
53 * @param samples - Number of rows to scan (increasing will slow down performance!)
54 * @returns the average width of the can
56 double Dilatometer_Read(int samples)
58 //Get the latest image
59 Dilatometer_GetImage();
61 int width = g_data->cols;
62 int height = g_data->rows;
63 // If the number of samples is greater than the image height, sample every row
66 Log(LOGNOTE, "Number of samples is greater than the dilatometer image height, sampling every row instead.\n");
70 // Stores the width of the can at different sample locations. Not necessary unless we want to store this information
71 //double widths[samples];
72 // The average width of the can
75 for (int i=0; i<samples; i++)
77 // Contains the locations of the 2 edges
78 double edges[2] = {0};
79 int pos = 0; // Position in the edges array (start at left edge)
80 int num = 0; // Keep track of the number of columns above threshold
82 // Determine the position in the rows to find the edges.
83 sample_height = ceil(height * (i + 1) / samples) -1;
85 for ( int col = 0; col < width; col++)
87 if ( CV_MAT_ELEM( *g_data, double, col, sample_height) > THRES)
92 // If num > 0 and we're not above threshold, we have left the threshold of the edge
95 // Find the mid point of the edge
99 pos = 1; // Move to the right edge
103 break; // Exit the for loop
106 // Determine the width of the can at this row
107 //widths[i] = edges[1] - edges[0];
108 average_width += (edges[1] - edges[0]);
110 average_width /= samples;
111 return average_width;