e3880736aa6d81cff265ddc5d387fc96fe0804dc
[matches/MCTX3420.git] / server / microscope.c
1 /**
2  * @file microscope.c
3  * @purpose Implementation of microscope related functions
4  */
5
6 #include "cv.h"
7 #include "highgui_c.h"
8 #include "microscope.h"
9 #include <math.h>
10
11 // test positions
12 static double test_left, test_right;
13
14 // Canny Edge algorithm variables
15 int lowThreshold = 30;
16 int ratio = 3;
17 int kernel_size = 3;
18
19 /** Buffer for storing image data. Stored as a  **/
20 static CvMat * g_srcRGB  = NULL; // Source Image
21 static CvMat * g_srcGray = NULL; // Gray scale of source image
22 static CvMat * g_edges   = NULL; // Detected Edges
23 static CvMat * g_data    = NULL; // Image to mask edges onto
24
25
26 /** Camera capture pointer **/
27 static CvCapture * g_capture = NULL;
28
29 /**
30  * Create a test image using left as left edge and right as right edge positions
31  */
32 void Dilatometer_TestImage()
33 {
34         
35         g_srcRGB = cvCreateMat(480, 640, CV_8UC3);
36
37         for( int x = 0; x < 640; ++x)
38         {
39                 for (int y = 0; y < 480; ++y)
40                 {
41                         CvScalar s; 
42                         for( int i = 0; i < 3; ++i)
43                         {
44                                 s.val[i]  =  210 + (rand() % 1000) * 1e-0 - (rand() % 1000) * 1e-0;
45                                 // Produce an exponential decay around left edge
46                                 if( x < test_left)
47                                         s.val[i] *= exp( (x - test_left) / 25);
48                                 else if( x < 320)
49                                         s.val[i] *= exp( (test_left - x) / 25); 
50                                 // Produce an exponential decay around right edge
51                                 else if( x < test_right)
52                                         s.val[i] *= exp( (x - test_right) / 25); 
53                                 else
54                                         s.val[i] *= exp( (test_right - x) / 25);                                
55                         }       
56                         cvSet2D(g_srcRGB,y,x,s);
57                 //      if( s.val[0] > 200)
58                 //              printf("row: %d, col: %d, %f\n", y, x, s.val[0]); 
59                 }
60                 
61         }
62         if (g_data == NULL)
63         {
64                 g_data = cvCreateMat(g_srcRGB->rows,g_srcRGB->cols,CV_8UC1); //IPL_DEPTH_8U?
65         }
66         cvCvtColor(g_srcRGB,g_data,CV_RGB2GRAY);
67 }       
68
69 /**
70  * Initialise the dilatometer
71  */
72 void Microscope_Init()
73 {
74         
75         // Make an initial reading (will allocate memory the first time only).
76         double val;
77         Microscope_Read(&val, 1); 
78 }
79
80 /**
81  * Cleanup Interferometer stuff
82  */
83 void Microscope_Cleanup()
84 {
85         if (g_data != NULL)
86                 cvReleaseMat(&g_data);
87
88         if (g_capture != NULL)
89                 cvReleaseCapture(&g_capture);
90
91 }
92
93 /**
94  * Get an image from the Dilatometer
95  */
96 static void Microscope_GetImage()
97 {       
98         //Need to implement camera
99 }
100
101 void CannyThreshold()
102 {
103         
104         if (g_data == NULL)
105         {
106                 g_data = cvCreateMat(g_srcGray->rows,g_srcGray->cols,CV_8UC1);
107         }
108
109         if ( g_edges == NULL)
110         {
111                 g_edges = cvCreateMat(g_srcGray->rows,g_srcGray->cols,CV_8UC1);
112         }
113         
114         //g_data = 0;
115         cvShowImage("display", g_srcGray);
116         cvWaitKey(0);   
117         // Reduce noise with a kernel 3x3. Input the grayscale source image, output to edges. (0's mean it's determined from kernel sizes)
118         cvSmooth( g_srcGray, g_edges, CV_GAUSSIAN, 9, 9 ,0 ,0 );
119         
120         cvShowImage("display", g_edges);
121         cvWaitKey(0);   
122         
123         // Find the edges in the image
124         lowThreshold = 35;
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 Microscope_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 microscope image. The value changed will correspond to the new location of the edge.
148  * @param val - Will store the read value if successful
149  * @param samples - Number of rows to scan (increasing will slow down performance!)
150  * @returns true on successful read
151  */
152 bool Microscope_Read( double * value, int samples)
153 {
154         bool result = false; 
155         double average = 0;
156         // Get the image from the camera
157         Microscope_GetImageTest();
158         
159         int width = g_edges->cols;
160         int height = g_edges->rows;
161         
162         // If the number of samples is greater than the image height, sample every row
163         if( samples > height)
164         {
165                 samples = height;
166         }
167         
168         int sample_height;
169         int num_edges = 0;      // Number of edges. if each sample location has an edge, then num_edges = samples
170
171         for (int i=0; i<samples; i++)
172         {
173                 // Determine the position in the rows to find the edges. 
174                 // This will give you a number of evenly spaced samples
175                 sample_height = ceil(height * (i + 1) / samples) -1;
176                 
177                 // 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).
178                 
179                 int edge_location=0;
180                 int num=0;
181                 for ( int col = 0; col < width; col++)
182                 {
183                         // Count the number of points
184                         // Get the threshold of the pixel at the current location
185                         CvScalar value = cvGet2D(g_edges, sample_height, col);
186                         //printf("row: %d, col: %d, value: %f\n",sample_height, col, value.val[0]);
187                         if( value.val[0]> THRES)
188                         {
189                                 edge_location += col;
190                                 num++;
191                         }
192                 }
193                 if( num > 0)
194                 {
195                         average += ( edge_location / num );
196                         num_edges++;
197                         printf("average %f\n", average/num_edges);
198                 }
199         }
200         if (num_edges > 0)
201                 average /= num_edges;
202         
203         if( average > 0)
204         {       
205                 result = true; //Successfully found an edge
206                 *value = average;
207         }
208         return result;
209 }
210
211 // Overlays a line over the given edge position
212 void Draw_Edge(double edge)
213 {
214         CvScalar value;
215         value.val[0]=244;
216         for( int i = 0; i < g_srcGray->rows; i++)
217         {
218                 cvSet2D(g_edges,i,edge,value);
219         }
220         cvShowImage("display", g_edges);
221         cvWaitKey(0);   
222 }
223
224 /**
225  * For testing purposes
226  */
227 int main(int argc, char ** argv)
228 {
229         //cvNamedWindow( "display", CV_WINDOW_AUTOSIZE );// Create a window for display.
230         //gettimeofday(&start, NULL);
231         test_left = 100;
232         test_right = 500;
233         Microscope_Init();
234         
235         cvNamedWindow( "display", CV_WINDOW_AUTOSIZE);
236 //      cvShowImage("display", g_data);
237 //      cvWaitKey(0);   
238         double width;
239         
240         double edge;
241         Microscope_Read(&edge,15);
242         //For testing purposes, overlay the given average line over the image
243         Draw_Edge(edge);
244
245 }
246

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