6610cac9ccf9f3026a3ef5243068df41fd4d4f4c
[matches/MCTX3420.git] / server / dilatometer.c
1 /**
2  * @file dilatometer.c
3  * @purpose Implementation of dilatometer related functions
4  */
5
6 #include "cv.h"
7 #include "highgui_c.h"
8 #include "dilatometer.h"
9 #include <math.h>
10
11 /** Buffer for storing image data. Stored as a  **/
12 static CvMat * g_data = NULL;
13
14
15 /** Camera capture pointer **/
16 static CvCapture * g_capture = NULL;
17
18
19 /**
20  * Initialise the dilatometer
21  */
22 void Dilatometer_Init()
23 {
24         
25         // Make an initial reading (will allocate memory the first time only).
26         Dilatometer_Read(1); 
27 }
28
29 /**
30  * Cleanup Interferometer stuff
31  */
32 void Dilatometer_Cleanup()
33 {
34         if (g_data != NULL)
35                 cvReleaseMat(&g_data);
36
37         if (g_capture != NULL)
38                 cvReleaseCapture(&g_capture);
39
40 }
41
42 /**
43  * Get an image from the Dilatometer
44  */
45 static void Dilatometer_GetImage()
46 {
47         //Need to supply test image
48
49         //Need to implement camera
50 }
51 /**
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
55  */
56 double Dilatometer_Read(int samples)
57 {
58         //Get the latest image
59         Dilatometer_GetImage();
60
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
64         if( samples > height)
65         {
66                 Log(LOGNOTE, "Number of samples is greater than the dilatometer image height, sampling every row instead.\n");
67                 samples = height;
68         }
69
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
73         double average_width;
74         int sample_height;
75         for (int i=0; i<samples; i++)
76         {
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
81
82                 // Determine the position in the rows to find the edges. 
83                 sample_height = ceil(height * (i + 1) / samples) -1;
84                 
85                 for ( int col = 0; col < width; col++)
86                 {
87                         if ( CV_MAT_ELEM( *g_data, double, col, sample_height) > THRES)
88                         {
89                                 edges[pos] += col;
90                                 num++;
91                         }
92                         // If num > 0 and we're not above threshold, we have left the threshold of the edge
93                         else if( num > 0);
94                         {
95                                 // Find the mid point of the edge
96                                 edges[pos] /= num;
97                                 if( edges[1] == 0) 
98                                 {
99                                         pos = 1;        // Move to the right edge
100                                         num = 0;
101                                 }
102                                 else
103                                         break;          // Exit the for loop
104                         }
105                 }
106                 // Determine the width of the can at this row
107                 //widths[i] = edges[1] - edges[0];
108                 average_width += (edges[1] - edges[0]);
109         }
110                 average_width /= samples;
111                 return average_width;
112 }
113

UCC git Repository :: git.ucc.asn.au