Add pressure sensors
[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 // test positions
12 static double test_left, test_right;
13
14 /** Buffer for storing image data. Stored as a  **/
15 static CvMat * g_data = NULL;
16
17
18 /** Camera capture pointer **/
19 static CvCapture * g_capture = NULL;
20
21 /**
22  * Create a test image using left as left edge and right as right edge positions
23  */
24 void Dilatometer_TestImage()
25 {
26         
27         CvMat *g_dataRGB;
28         g_dataRGB = cvCreateMat(480, 640, CV_8UC3);
29         //Make sure left and right positions are sane
30         if( test_left < 0)
31                 test_left = 0;
32         if( test_right > 639)
33                 test_right = 639;
34         if( test_left > test_right)
35         {
36                 int tmp = test_right;
37                 test_right = test_left;
38                 test_left = tmp;
39         }
40
41         for( int x = 0; x < 640; ++x)
42         {
43                 for (int y = 0; y < 480; ++y)
44                 {
45                         CvScalar s; 
46                         for( int i = 0; i < 3; ++i)
47                         {
48                                 s.val[i]  =  220 + (rand() % 1000) * 1e-2 - (rand() % 1000) * 1e-2;
49                                 // Produce an exponential decay around left edge
50                                 if( x < test_left)
51                                         s.val[i] *= exp( (x - test_left) / 25);
52                                 else if( x < 320)
53                                         s.val[i] *= exp( (test_left - x) / 25); 
54                                 // Produce an exponential decay around right edge
55                                 else if( x < test_right)
56                                         s.val[i] *= exp( (x - test_right) / 25); 
57                                 else
58                                         s.val[i] *= exp( (test_right - x) / 25);                                
59                         }       
60                         cvSet2D(g_dataRGB,y,x,s);
61                 //      if( s.val[0] > 200)
62                 //              printf("row: %d, col: %d, %f\n", y, x, s.val[0]); 
63                 }
64                 
65         }
66         if (g_data == NULL)
67         {
68                 g_data = cvCreateMat(g_dataRGB->rows,g_dataRGB->cols,CV_8UC1); //IPL_DEPTH_8U?
69         }
70         cvCvtColor(g_dataRGB,g_data,CV_RGB2GRAY);
71 }       
72
73 /**
74  * Initialise the dilatometer
75  */
76 void Dilatometer_Init()
77 {
78         
79         // Make an initial reading (will allocate memory the first time only).
80         Dilatometer_Read(1); 
81 }
82
83 /**
84  * Cleanup Interferometer stuff
85  */
86 void Dilatometer_Cleanup()
87 {
88         if (g_data != NULL)
89                 cvReleaseMat(&g_data);
90
91         if (g_capture != NULL)
92                 cvReleaseCapture(&g_capture);
93
94 }
95
96 /**
97  * Get an image from the Dilatometer
98  */
99 static void Dilatometer_GetImage()
100 {       
101         //Need to implement camera
102 }
103
104 // Test algorithm
105 static void Dilatometer_GetImageTest( )
106 {       
107         //Test image
108         Dilatometer_TestImage();
109 }
110
111
112 /**
113  * Read the dilatometer; gets the latest image, processes it, THEN DOES WHAT
114  * @param samples - Number of rows to scan (increasing will slow down performance!)
115  * @returns the average width of the can
116  */
117 double Dilatometer_Read(int samples)
118 {
119         //Get the latest image
120         //Dilatometer_GetImage();
121
122         Dilatometer_GetImageTest();
123         
124         int width = g_data->cols;
125         int height = g_data->rows;
126         // If the number of samples is greater than the image height, sample every row
127         if( samples > height)
128         {
129                 //Log(LOGNOTE, "Number of samples is greater than the dilatometer image height, sampling every row instead.\n");
130                 samples = height;
131         }
132
133         // Stores the width of the can at different sample locations. Not necessary unless we want to store this information
134         //double widths[samples];
135         // The average width of the can
136         double average_width;
137         int sample_height;
138         for (int i=0; i<samples; i++)
139         {
140                 // Contains the locations of the 2 edges
141                 double edges[2] = {0.0,0.0};
142                 int pos = 0;    // Position in the edges array (start at left edge)
143                 int num = 0;    // Keep track of the number of columns above threshold
144
145                 // Determine the position in the rows to find the edges. 
146                 sample_height = ceil(height * (i + 1) / samples) -1;
147                 //printf("sample height is %d\n", sample_height);
148
149                 //CvScalar test = cvGet2D(g_data, 150,300);
150                 //printf("test is %f,%f,%f,%f\n", test.val[0], test.val[1], test.val[2], test.val[3]);
151
152
153                 for ( int col = 0; col < width; col++)
154                 {
155                         CvScalar value = cvGet2D(g_data, sample_height, col);
156                         if( value.val[0]> THRES)
157                         {
158                                 edges[pos] += (double) col;
159                                 num++;
160                         }
161                         // If num > 0 and we're not above threshold, we have left the threshold of the edge
162                         else if( num > 0)
163                         {
164                                 // Find the mid point of the edge
165                                 edges[pos] /= num;
166                                 if( edges[1] == 0) 
167                                 {
168                                         pos = 1;        // Move to the right edge
169                                         num = 0;
170                                 }
171                                 else
172                                         break;          // Exit the for loop
173                         }
174                 }
175                 // Determine the width of the can at this row
176                 //widths[i] = edges[1] - edges[0];
177                 average_width += (edges[1] - edges[0]);
178         }
179         average_width /= (double) samples;
180         return average_width;
181 }
182
183 /**
184  * For testing purposes
185  */
186 int main(int argc, char ** argv)
187 {
188         //cvNamedWindow( "display", CV_WINDOW_AUTOSIZE );// Create a window for display.
189         //gettimeofday(&start, NULL);
190         test_left = 100;
191         test_right = 500;
192         Dilatometer_Init();
193
194         cvNamedWindow( "display", CV_WINDOW_AUTOSIZE);
195         cvShowImage("display", g_data);
196         cvWaitKey(0);   
197         double width;
198         for( int i = 0; i < 20; ++i)
199         {
200                 test_left  -= i * (rand() % 1000) * 1e-3;
201                 test_right += i * (rand() % 1000) * 1e-3;
202                 width = Dilatometer_Read(5);
203                 cvNamedWindow( "display", CV_WINDOW_AUTOSIZE);
204                 cvShowImage("display", g_data);
205                 cvWaitKey(0); 
206                 double expected = test_right - test_left;
207                 double perc = 100 * (expected - width) / expected;
208                 printf("%d: Left: %.4f.    Width: %.4f.\n  Right: %.4f. Expected: %.4f. Percentage: %.4f\n", i, test_left, width, test_right, expected, perc);
209         }
210 }
211

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