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
24 static CvMat * g_data = NULL;
26 /** Pointers for capturing image **/
27 static CvCapture * g_capture = NULL;
28 static IplImage * frame = NULL; // This is required as you can not use capture with CvMat in C
32 * Create a test image using left as left edge and right as right edge positions
34 void Dilatometer_TestImage()
37 g_srcRGB = cvCreateMat(480, 640, CV_8UC3);
39 for( int x = 0; x < 640; ++x)
41 for (int y = 0; y < 480; ++y)
44 for( int i = 0; i < 3; ++i)
46 s.val[i] = 210 + (rand() % 1000) * 1e-0 - (rand() % 1000) * 1e-0;
47 // Produce an exponential decay around left edge
49 s.val[i] *= exp( (x - test_left) / 25);
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);
56 s.val[i] *= exp( (test_right - x) / 25);
58 cvSet2D(g_srcRGB,y,x,s);
64 g_data = cvCreateMat(g_srcRGB->rows,g_srcRGB->cols,CV_8UC1);
66 cvCvtColor(g_srcRGB,g_data,CV_RGB2GRAY);
70 * Cleanup Dilatometer pointers
72 void Dilatometer_Cleanup()
75 cvReleaseMat(&g_data);
76 if (g_capture != NULL)
77 cvReleaseCapture(&g_capture);
79 cvReleaseMat(&g_srcRGB);
80 if (g_srcGray != NULL)
81 cvReleaseMat(&g_srcGray);
83 cvReleaseMat(&g_edges);
85 cvReleaseImageHeader(&frame);
89 * Get an image from the Dilatometer
91 static bool Dilatometer_GetImage()
94 // If more than one camera is connected, then input needs to be determined, however the camera ID may change after being unplugged
95 g_capture = cvCreateCameraCapture(0);
97 //If cvCreateCameraCapture returns NULL there is an error with the camera
98 if( g_capture == NULL)
101 // Get the frame and convert it to CvMat
102 frame = cvQueryFrame(g_capture);
104 g_srcRGB = cvGetMat(frame,&stub,0,0);
106 if( g_srcRGB == NULL)
112 void CannyThreshold()
117 g_data = cvCreateMat(g_srcGray->rows,g_srcGray->cols,CV_8UC1);
120 if ( g_edges == NULL)
122 g_edges = cvCreateMat(g_srcGray->rows,g_srcGray->cols,CV_8UC1);
125 // Commented out lines are used during testing to show the image to screen, can also save the test images
126 //cvShowImage("display", g_srcGray);
129 // Reduce noise with a kernel blurxblur. Input the grayscale source image, output to edges. (0's mean it's determined from kernel sizes)
130 cvSmooth( g_srcGray, g_edges, CV_GAUSSIAN, blur, blur ,0 ,0 );
133 //cvSaveImage("test_blurred.jpg",g_edges,0);
135 //cvShowImage("display", g_edges);
138 // Find the edges in the image
139 cvCanny( g_edges, g_edges, lowThreshold, lowThreshold*ratio, kernel_size );
142 //cvSaveImage("test_edge.jpg",g_edges,0);
144 //cvShowImage("display", g_edges);
150 * Read the dilatometer image. The value changed will correspond to the new location of the edge.
151 * @param val - Will store the read value if successful
152 * @param samples - Number of rows to scan (increasing will slow down performance!)
153 * @returns true on successful read
155 bool Dilatometer_GetEdge( double * value, int samples)
159 // Get the image from the camera
160 result = Dilatometer_GetImage();
161 // If an error occured when capturing image then return
165 // Apply the Canny Edge theorem to the image
168 int width = g_edges->cols;
169 int height = g_edges->rows;
171 // If the number of samples is greater than the image height, sample every row
172 if( samples > height)
178 int num_edges = 0; // Number of edges. if each sample location has an edge, then num_edges = samples
180 for (int i=0; i<samples; i++)
182 // Determine the position in the rows to find the edges.
183 // This will give you a number of evenly spaced samples
184 sample_height = ceil(height * (i + 1) / samples) -1;
186 // 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).
190 for ( int col = 0; col < width; col++)
192 // Count the number of points
193 // Get the threshold of the pixel at the current location
194 CvScalar value = cvGet2D(g_edges, sample_height, col);
195 if( value.val[0]> THRES)
197 edge_location += col;
203 average += ( edge_location / num );
208 average /= num_edges;
212 result = true; //Successfully found an edge
219 * Read the dilatometer image. The value changed will correspond to the new location of the edge.
220 * @param val - Will store the read value if successful
221 * @returns true on successful read
223 bool Dilatometer_Read( double * value)
225 bool result = Dilatometer_GetEdge(value, SAMPLES);
230 * Initialise the dilatometer
232 void Dilatometer_Init()
234 // Make an initial reading (will allocate memory the first time only).
236 Dilatometer_GetEdge(&val, 1);
239 // Overlays a line over the given edge position
240 void Draw_Edge(double edge)
244 for( int i = 0; i < g_srcGray->rows; i++)
246 cvSet2D(g_edges,i,edge,value);
248 cvShowImage("display", g_edges);
250 cvSaveImage("test_edge_avg.jpg",g_edges,0);
254 static void Dilatometer_GetImageTest( )
256 //Generates Test image
257 //Dilatometer_TestImage();
260 g_srcGray = cvLoadImageM ("testimage4.jpg",CV_LOAD_IMAGE_GRAYSCALE );
264 * For testing purposes
266 /*int main(int argc, char ** argv)
268 //cvNamedWindow( "display", CV_WINDOW_AUTOSIZE );// Create a window for display.
269 //gettimeofday(&start, NULL);
274 cvNamedWindow( "display", CV_WINDOW_AUTOSIZE);
275 // cvShowImage("display", g_data);
280 Dilatometer_Read(&edge,20000);
281 //For testing purposes, overlay the given average line over the image
284 cvDestroyWindow("display");