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

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