3 * @purpose Implementation of microscope related functions
8 #include "microscope.h"
12 static double test_left, test_right;
14 // Canny Edge algorithm variables
15 int lowThreshold = 30;
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
26 /** Camera capture pointer **/
27 static CvCapture * g_capture = NULL;
30 * Create a test image using left as left edge and right as right edge positions
32 void Dilatometer_TestImage()
35 g_srcRGB = cvCreateMat(480, 640, CV_8UC3);
37 for( int x = 0; x < 640; ++x)
39 for (int y = 0; y < 480; ++y)
42 for( int i = 0; i < 3; ++i)
44 s.val[i] = 210 + (rand() % 1000) * 1e-0 - (rand() % 1000) * 1e-0;
45 // Produce an exponential decay around left edge
47 s.val[i] *= exp( (x - test_left) / 25);
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);
54 s.val[i] *= exp( (test_right - x) / 25);
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]);
64 g_data = cvCreateMat(g_srcRGB->rows,g_srcRGB->cols,CV_8UC1); //IPL_DEPTH_8U?
66 cvCvtColor(g_srcRGB,g_data,CV_RGB2GRAY);
70 * Initialise the dilatometer
72 void Microscope_Init()
75 // Make an initial reading (will allocate memory the first time only).
77 Microscope_Read(&val, 1);
81 * Cleanup Interferometer stuff
83 void Microscope_Cleanup()
86 cvReleaseMat(&g_data);
88 if (g_capture != NULL)
89 cvReleaseCapture(&g_capture);
94 * Get an image from the Dilatometer
96 static void Microscope_GetImage()
98 //Need to implement camera
101 void CannyThreshold()
106 g_data = cvCreateMat(g_srcGray->rows,g_srcGray->cols,CV_8UC1);
109 if ( g_edges == NULL)
111 g_edges = cvCreateMat(g_srcGray->rows,g_srcGray->cols,CV_8UC1);
115 cvShowImage("display", g_srcGray);
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 );
120 cvShowImage("display", g_edges);
123 // Find the edges in the image
125 cvCanny( g_edges, g_edges, lowThreshold, lowThreshold*ratio, kernel_size );
127 cvShowImage("display", g_edges);
130 // Mask the edges over G_data
131 //.copyTo( g_data, g_edges);
135 static void Microscope_GetImageTest( )
137 //Generates Test image
138 //Dilatometer_TestImage();
141 g_srcGray = cvLoadImageM ("testimage.jpg",CV_LOAD_IMAGE_GRAYSCALE );
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
152 bool Microscope_Read( double * value, int samples)
156 // Get the image from the camera
157 Microscope_GetImageTest();
159 int width = g_edges->cols;
160 int height = g_edges->rows;
162 // If the number of samples is greater than the image height, sample every row
163 if( samples > height)
169 int num_edges = 0; // Number of edges. if each sample location has an edge, then num_edges = samples
171 for (int i=0; i<samples; i++)
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;
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).
181 for ( int col = 0; col < width; col++)
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)
189 edge_location += col;
195 average += ( edge_location / num );
197 printf("average %f\n", average/num_edges);
201 average /= num_edges;
205 result = true; //Successfully found an edge
211 // Overlays a line over the given edge position
212 void Draw_Edge(double edge)
216 for( int i = 0; i < g_srcGray->rows; i++)
218 cvSet2D(g_edges,i,edge,value);
220 cvShowImage("display", g_edges);
225 * For testing purposes
227 int main(int argc, char ** argv)
229 //cvNamedWindow( "display", CV_WINDOW_AUTOSIZE );// Create a window for display.
230 //gettimeofday(&start, NULL);
235 cvNamedWindow( "display", CV_WINDOW_AUTOSIZE);
236 // cvShowImage("display", g_data);
241 Microscope_Read(&edge,15);
242 //For testing purposes, overlay the given average line over the image