Dilatometer stuff. Incomplete.
[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 /*-------------------------------------------------------------------
12
13 compile with:
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 
15
16 --------------------------------------------------------------------*/
17
18 /** Buffer for storing image data. Stored as a  **/
19 static CvMat * g_data = NULL;
20
21
22 /** Camera capture pointer **/
23 static CvCapture * g_capture = NULL;
24
25
26 /**
27  * Initialise the dilatometer
28  */
29 void Dilatometer_Init()
30 {
31         
32         // Make an initial reading (will allocate memory the first time only).
33         Dilatometer_Read(1); 
34 }
35
36 /**
37  * Cleanup Interferometer stuff
38  */
39 void Dilatometer_Cleanup()
40 {
41         if (g_data != NULL)
42                 cvReleaseMat(&g_data);
43
44         if (g_capture != NULL)
45                 cvReleaseCapture(&g_capture);
46
47 }
48
49 /**
50  * Get an image from the Dilatometer
51  */
52 static void Dilatometer_GetImage()
53 {
54         //Need to supply test image
55
56         //Need to implement camera
57 }
58 /**
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
62  */
63 double Dilatometer_Read(int samples)
64 {
65         //Get the latest image
66         Dilatometer_GetImage();
67
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
71         if( samples > height)
72         {
73                 Log(LOGNOTE, "Number of samples is greater than the dilatometer image height, sampling every row instead.\n");
74                 samples = height;
75         }
76
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
80         double average_width;
81         int sample_height;
82         for (int i=0; i<samples; i++)
83         {
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
88
89                 // Determine the position in the rows to find the edges. 
90                 sample_height = ceil(height * (i + 1) / samples) -1;
91                 
92                 for ( int col = 0; col < width; col++)
93                 {
94                         if ( CV_MAT_ELEM( *g_data, double, col, sample_height) > THRES)
95                         {
96                                 edges[pos] += col;
97                                 num++;
98                         }
99                         // If num > 0 and we're not above threshold, we have left the threshold of the edge
100                         else if( num > 0);
101                         {
102                                 // Find the mid point of the edge
103                                 edges[pos] /= num;
104                                 if( edges[1] == 0) 
105                                 {
106                                         pos = 1;        // Move to the right edge
107                                         num = 0;
108                                 }
109                                 else
110                                         break;          // Exit the for loop
111                         }
112                 }
113                 // Determine the width of the can at this row
114                 //widths[i] = edges[1] - edges[0];
115                 average_width += (edges[1] - edges[0]);
116         }
117                 average_width /= samples;
118                 return average_width;
119 }
120

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