544c358570e7fd9254b95fe6a0763c4a40b94ddf
[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 blur = 5;
16 int lowThreshold = 30;
17 int ratio = 3;
18 int kernel_size = 3;
19
20 /** Buffers for storing image data.  **/
21 static CvMat * g_srcRGB  = NULL;        // Source Image
22 static CvMat * g_srcGray = NULL;        // Gray scale of source image
23 static CvMat * g_edges   = NULL;        // Detected Edges
24
25 /** Pointers for capturing image **/
26 static CvCapture * g_capture = NULL;
27 static IplImage * frame  = NULL;        // This is required as you can not use capture with CvMat in C
28
29
30 /**
31  * Create a test image using left as left edge and right as right edge positions
32  */
33 void Dilatometer_TestImage()
34 {
35         
36         g_srcRGB = cvCreateMat(480, 640, CV_8UC3);
37
38         for( int x = 0; x < 640; ++x)
39         {
40                 for (int y = 0; y < 480; ++y)
41                 {
42                         CvScalar s; 
43                         for( int i = 0; i < 3; ++i)
44                         {
45                                 s.val[i]  =  210 + (rand() % 1000) * 1e-0 - (rand() % 1000) * 1e-0;
46                                 // Produce an exponential decay around left edge
47                                 if( x < test_left)
48                                         s.val[i] *= exp( (x - test_left) / 25);
49                                 else if( x < 320)
50                                         s.val[i] *= exp( (test_left - x) / 25); 
51                                 // Produce an exponential decay around right edge
52                                 else if( x < test_right)
53                                         s.val[i] *= exp( (x - test_right) / 25); 
54                                 else
55                                         s.val[i] *= exp( (test_right - x) / 25);                                
56                         }       
57                         cvSet2D(g_srcRGB,y,x,s);
58                 }
59                 
60         }
61         if (g_srcGray == NULL)
62         {
63                 g_srcGray = cvCreateMat(g_srcRGB->rows,g_srcRGB->cols,CV_8UC1);
64         }
65         cvCvtColor(g_srcRGB,g_srcGray,CV_RGB2GRAY);
66 }       
67
68 /**
69  * Cleanup Dilatometer pointers
70  */
71 void Dilatometer_Cleanup()
72 {
73         if (g_capture != NULL)
74                 cvReleaseCapture(&g_capture);
75         if (g_srcRGB != NULL)
76                 cvReleaseMat(&g_srcRGB);
77         if (g_srcGray != NULL)
78                 cvReleaseMat(&g_srcGray);
79         if (g_edges != NULL)
80                 cvReleaseMat(&g_edges);
81         if (frame != NULL)
82                 cvReleaseImageHeader(&frame);
83 }
84
85 /**
86  * Get an image from the Dilatometer
87  */
88 static bool Dilatometer_GetImage()
89 {       
90         bool result = true;
91         // If more than one camera is connected, then input needs to be determined, however the camera ID may change after being unplugged
92         g_capture = cvCreateCameraCapture(0);
93         //If cvCreateCameraCapture returns NULL there is an error with the camera
94         if( g_capture == NULL)
95                 result = false;
96         
97         // Get the frame and convert it to CvMat
98         frame =  cvQueryFrame(g_capture);
99         CvMat stub;
100         g_srcRGB = cvGetMat(frame,&stub,0,0);
101
102         if( g_srcRGB == NULL)
103                 result = false;
104         
105         // Convert the image to grayscale
106         if (g_srcGray == NULL)
107         {
108                 g_srcGray = cvCreateMat(g_srcRGB->rows,g_srcRGB->cols,CV_8UC1);
109         }
110
111         cvCvtColor(g_srcRGB,g_srcGray,CV_RGB2GRAY);
112         
113         return result;
114 }
115
116 void CannyThreshold()
117 {
118         if ( g_edges == NULL)
119         {
120                 g_edges = cvCreateMat(g_srcGray->rows,g_srcGray->cols,CV_8UC1);
121         }
122         
123         // Commented out lines are used during testing to show the image to screen, can also save the test images
124         //cvShowImage("display", g_srcGray);
125         //cvWaitKey(0);         
126         
127         // Reduce noise with a kernel blurxblur. Input the grayscale source image, output to edges. (0's mean it's determined from kernel sizes)
128         cvSmooth( g_srcGray, g_edges, CV_GAUSSIAN, blur, blur ,0 ,0 );
129         
130         //Save the image
131         //cvSaveImage("test_blurred.jpg",g_edges,0);
132                 printf("what about here?\n");
133
134         cvShowImage("display", g_edges);
135         cvWaitKey(0);   
136         // Find the edges in the image
137         cvCanny( g_edges, g_edges, lowThreshold, lowThreshold*ratio, kernel_size );
138         
139         //Save the image
140         //cvSaveImage("test_edge.jpg",g_edges,0);
141
142         cvShowImage("display", g_edges);
143         cvWaitKey(0);   
144                         printf("hopeful\n");
145
146 }
147
148  /**
149  * Read the dilatometer image. The value changed will correspond to the new location of the edge.
150  * @param val - Will store the read value if successful
151  * @param samples - Number of rows to scan (increasing will slow down performance!)
152  * @returns true on successful read
153  */
154 bool Dilatometer_GetEdge( double * value, int samples)
155 {
156         bool result = false; 
157         double average = 0;
158         // Get the image from the camera
159         result = Dilatometer_GetImage();
160         // If an error occured when capturing image then return
161         if (!result)
162                 return result;
163         
164         // Apply the Canny Edge theorem to the image
165         CannyThreshold();
166
167         int width = g_edges->cols;
168         int height = g_edges->rows;
169         
170         // If the number of samples is greater than the image height, sample every row
171         if( samples > height)
172         {
173                 samples = height;
174         }
175         
176         int sample_height;
177         int num_edges = 0;      // Number of edges. if each sample location has an edge, then num_edges = samples
178
179         for (int i=0; i<samples; i++)
180         {
181                 // Determine the position in the rows to find the edges. 
182                 // This will give you a number of evenly spaced samples
183                 sample_height = ceil(height * (i + 1) / samples) -1;
184                 
185                 // Need to go through each pixel of a row and find all the locations of a line. If there is more than one pixel, average it. note this only works if the canny edge algorithm returns lines about the actual line (no outliers).
186                 
187                 int edge_location=0;
188                 int num=0;
189                 for ( int col = 0; col < width; col++)
190                 {
191                         // Count the number of points
192                         // Get the threshold of the pixel at the current location
193                         CvScalar value = cvGet2D(g_edges, sample_height, col);
194                         if( value.val[0]> THRES)
195                         {
196                                 edge_location += col;
197                                 num++;
198                         }
199                 }
200                 if( num > 0)
201                 {
202                         average += ( edge_location / num );
203                         num_edges++;
204                 }
205         }
206         if (num_edges > 0)
207                 average /= num_edges;
208         
209         if( average > 0)
210         {       
211                 result = true; //Successfully found an edge
212                 *value = average;
213         }
214         return result;
215 }
216
217  /**
218  * Read the dilatometer image. The value changed will correspond to the new location of the edge.
219  * @param val - Will store the read value if successful
220  * @returns true on successful read
221  */
222 bool Dilatometer_Read( double * value)
223 {
224         bool result = Dilatometer_GetEdge(value, SAMPLES);
225         return result;
226 }
227
228 /**
229  * Initialise the dilatometer
230  */
231 void Dilatometer_Init()
232 {
233         // Make an initial reading (will allocate memory the first time only).
234         double val;
235         Dilatometer_GetEdge(&val, 1); 
236 }
237
238 // Overlays a line over the given edge position
239 void Draw_Edge(double edge)
240 {
241         CvScalar value;
242         value.val[0]=244;
243         for( int i = 0; i < g_srcGray->rows; i++)
244         {
245                 cvSet2D(g_edges,i,edge,value);
246         }
247         cvShowImage("display", g_edges);
248         cvWaitKey(0);   
249         //cvSaveImage("test_edge_avg.jpg",g_edges,0);
250 }
251
252 /* // Test algorithm
253 static void Dilatometer_GetImageTest( )
254 {       
255         //Generates Test image
256         //Dilatometer_TestImage();
257         
258         //Load Test image
259         g_srcGray = cvLoadImageM ("testimage4.jpg",CV_LOAD_IMAGE_GRAYSCALE );
260 }*/
261
262 /**
263  * For testing purposes
264  */
265 int main(int argc, char ** argv)
266 {
267         //cvNamedWindow( "display", CV_WINDOW_AUTOSIZE );// Create a window for display.
268         //gettimeofday(&start, NULL);
269         test_left = 100;
270         test_right = 500;
271         Dilatometer_Init();
272         
273         cvNamedWindow( "display", CV_WINDOW_AUTOSIZE);
274         //double width;
275         
276         double edge;
277         Dilatometer_GetEdge(&edge,20000);
278         //For testing purposes, overlay the given average line over the image
279         Draw_Edge(edge);
280         
281         cvDestroyWindow("display");
282
283         Dilatometer_Cleanup();
284
285 }
286

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