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

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