3 * @purpose Implementation of dilatometer related functions
8 #include "dilatometer.h"
12 static double test_left, test_right;
14 // Canny Edge algorithm variables
16 int lowThreshold = 30;
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
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
31 * Create a test image using left as left edge and right as right edge positions
33 void Dilatometer_TestImage()
36 g_srcRGB = cvCreateMat(480, 640, CV_8UC3);
38 for( int x = 0; x < 640; ++x)
40 for (int y = 0; y < 480; ++y)
43 for( int i = 0; i < 3; ++i)
45 s.val[i] = 210 + (rand() % 1000) * 1e-0 - (rand() % 1000) * 1e-0;
46 // Produce an exponential decay around left edge
48 s.val[i] *= exp( (x - test_left) / 25);
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);
55 s.val[i] *= exp( (test_right - x) / 25);
57 cvSet2D(g_srcRGB,y,x,s);
61 if (g_srcGray == NULL)
63 g_srcGray = cvCreateMat(g_srcRGB->rows,g_srcRGB->cols,CV_8UC1);
65 cvCvtColor(g_srcRGB,g_srcGray,CV_RGB2GRAY);
69 * Cleanup Dilatometer pointers
71 void Dilatometer_Cleanup()
73 if (g_capture != NULL)
74 cvReleaseCapture(&g_capture);
76 cvReleaseImageHeader(&frame);
77 //if (g_srcRGB != NULL)
78 // cvReleaseMat(&g_srcRGB); // Causing run time error in cvReleaseMat
79 if (g_srcGray != NULL)
80 cvReleaseMat(&g_srcGray);
82 cvReleaseMat(&g_edges);
86 * Get an image from the Dilatometer
88 static bool Dilatometer_GetImage()
91 // If more than one camera is connected, then input needs to be determined, however the camera ID may change after being unplugged
92 if( g_capture == NULL)
94 g_capture = cvCreateCameraCapture(0);
95 //If cvCreateCameraCapture returns NULL there is an error with the camera
96 if( g_capture == NULL)
103 // Get the frame and convert it to CvMat
104 frame = cvQueryFrame(g_capture);
106 g_srcRGB = cvGetMat(frame,&stub,0,0);
108 if( g_srcRGB == NULL)
111 // Convert the image to grayscale
112 if (g_srcGray == NULL)
114 g_srcGray = cvCreateMat(g_srcRGB->rows,g_srcRGB->cols,CV_8UC1);
117 cvCvtColor(g_srcRGB,g_srcGray,CV_RGB2GRAY);
122 void CannyThreshold()
124 if ( g_edges == NULL)
126 g_edges = cvCreateMat(g_srcGray->rows,g_srcGray->cols,CV_8UC1);
129 // Commented out lines are used during testing to show the image to screen, can also save the test images
130 //cvShowImage("display", g_srcGray);
133 // Reduce noise with a kernel blurxblur. Input the grayscale source image, output to edges. (0's mean it's determined from kernel sizes)
134 cvSmooth( g_srcGray, g_edges, CV_GAUSSIAN, blur, blur ,0 ,0 );
137 //cvSaveImage("test_blurred.jpg",g_edges,0);
139 //cvShowImage("display", g_edges);
142 // Find the edges in the image
143 cvCanny( g_edges, g_edges, lowThreshold, lowThreshold*ratio, kernel_size );
146 //cvSaveImage("test_edge.jpg",g_edges,0);
148 //cvShowImage("display", g_edges);
154 * Read the dilatometer image. The value changed will correspond to the new location of the edge.
155 * @param val - Will store the read value if successful
156 * @param samples - Number of rows to scan (increasing will slow down performance!)
157 * @returns true on successful read
159 bool Dilatometer_GetEdge( double * value, int samples)
163 // Get the image from the camera
164 result = Dilatometer_GetImage();
165 // If an error occured when capturing image then return
169 // Apply the Canny Edge theorem to the image
172 int width = g_edges->cols;
173 int height = g_edges->rows;
175 // If the number of samples is greater than the image height, sample every row
176 if( samples > height)
182 int num_edges = 0; // Number of edges. if each sample location has an edge, then num_edges = samples
184 for (int i=0; i<samples; i++)
186 // Determine the position in the rows to find the edges.
187 // This will give you a number of evenly spaced samples
188 sample_height = ceil(height * (i + 1) / samples) -1;
190 // 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).
194 for ( int col = 0; col < width; col++)
196 // Count the number of points
197 // Get the threshold of the pixel at the current location
198 CvScalar value = cvGet2D(g_edges, sample_height, col);
199 if( value.val[0]> THRES)
201 edge_location += col;
207 average += ( edge_location / num );
212 average /= num_edges;
216 result = true; //Successfully found an edge
223 * Read the dilatometer image. The value changed will correspond to the new location of the edge.
224 * @param val - Will store the read value if successful
225 * @returns true on successful read
227 bool Dilatometer_Read( double * value)
229 bool result = Dilatometer_GetEdge(value, SAMPLES);
234 * Initialise the dilatometer
236 void Dilatometer_Init()
238 // Make an initial reading (will allocate memory the first time only).
240 Dilatometer_GetEdge(&val, 1);
243 // Overlays a line over the given edge position
244 void Draw_Edge(double edge)
248 for( int i = 0; i < g_srcGray->rows; i++)
250 cvSet2D(g_edges,i,edge,value);
252 cvShowImage("display", g_edges);
254 //cvSaveImage("test_edge_avg.jpg",g_edges,0);
258 static void Dilatometer_GetImageTest( )
260 //Generates Test image
261 //Dilatometer_TestImage();
264 g_srcGray = cvLoadImageM ("testimage4.jpg",CV_LOAD_IMAGE_GRAYSCALE );
268 * For testing purposes
270 int main(int argc, char ** argv)
272 //cvNamedWindow( "display", CV_WINDOW_AUTOSIZE );// Create a window for display.
273 //gettimeofday(&start, NULL);
278 cvNamedWindow( "display", CV_WINDOW_AUTOSIZE);
282 Dilatometer_GetEdge(&edge,20000);
283 //For testing purposes, overlay the given average line over the image
286 cvDestroyWindow("display");
288 Dilatometer_Cleanup();