3 * @purpose Implementation of dilatometer related functions
8 #include "dilatometer.h"
11 /*-------------------------------------------------------------------
14 -I/usr/include/opencv -I/usr/include/opencv2/highgui -L/usr/lib -lopencv_highgui -lopencv_core -lopencv_ml -lopencv_imgproc -std=c99 -Wall -pedantic
16 --------------------------------------------------------------------*/
18 /** Buffer for storing image data. Stored as a **/
19 static CvMat * g_data = NULL;
22 /** Camera capture pointer **/
23 static CvCapture * g_capture = NULL;
27 * Initialise the dilatometer
29 void Dilatometer_Init()
32 // Make an initial reading (will allocate memory the first time only).
37 * Cleanup Interferometer stuff
39 void Dilatometer_Cleanup()
42 cvReleaseMat(&g_data);
44 if (g_capture != NULL)
45 cvReleaseCapture(&g_capture);
50 * Get an image from the Dilatometer
52 static void Dilatometer_GetImage()
54 //Need to supply test image
56 //Need to implement camera
59 * Read the dilatometer; gets the latest image, processes it, THEN DOES WHAT
60 * @param samples - Number of rows to scan (increasing will slow down performance!)
61 * @returns the average width of the can
63 double Dilatometer_Read(int samples)
65 //Get the latest image
66 Dilatometer_GetImage();
68 int width = g_data->cols;
69 int height = g_data->rows;
70 // If the number of samples is greater than the image height, sample every row
73 Log(LOGNOTE, "Number of samples is greater than the dilatometer image height, sampling every row instead.\n");
77 // Stores the width of the can at different sample locations. Not necessary unless we want to store this information
78 //double widths[samples];
79 // The average width of the can
82 for (int i=0; i<samples; i++)
84 // Contains the locations of the 2 edges
85 double edges[2] = {0};
86 int pos = 0; // Position in the edges array (start at left edge)
87 int num = 0; // Keep track of the number of columns above threshold
89 // Determine the position in the rows to find the edges.
90 sample_height = ceil(height * (i + 1) / samples) -1;
92 for ( int col = 0; col < width; col++)
94 if ( CV_MAT_ELEM( *g_data, double, col, sample_height) > THRES)
99 // If num > 0 and we're not above threshold, we have left the threshold of the edge
102 // Find the mid point of the edge
106 pos = 1; // Move to the right edge
110 break; // Exit the for loop
113 // Determine the width of the can at this row
114 //widths[i] = edges[1] - edges[0];
115 average_width += (edges[1] - edges[0]);
117 average_width /= samples;
118 return average_width;