3 * @purpose Implementation of dilatometer related functions
8 #include "dilatometer.h"
12 static double test_left, test_right;
14 // Remembers the last position to measure rate of expansion
15 static double lastPosition;
18 /** Buffers for storing image data. **/
19 static CvMat * g_srcRGB = NULL; // Source Image
20 static CvMat * g_srcGray = NULL; // Gray scale of source image
21 static CvMat * g_edges = NULL; // Detected Edges
23 /** Pointers for capturing image **/
24 //static CvCapture * g_capture = NULL;
25 //static IplImage * frame = NULL; // This is required as you can not use capture with CvMat in C
29 * Create a test image using left as left edge and right as right edge positions
31 void Dilatometer_TestImage()
34 g_srcRGB = cvCreateMat(480, 640, CV_8UC3);
36 for( int x = 0; x < 640; ++x)
38 for (int y = 0; y < 480; ++y)
41 for( int i = 0; i < 3; ++i)
43 s.val[i] = 210 + (rand() % 1000) * 1e-0 - (rand() % 1000) * 1e-0;
44 // Produce an exponential decay around left edge
46 s.val[i] *= exp( (x - test_left) / 25);
48 s.val[i] *= exp( (test_left - x) / 25);
49 // Produce an exponential decay around right edge
50 else if( x < test_right)
51 s.val[i] *= exp( (x - test_right) / 25);
53 s.val[i] *= exp( (test_right - x) / 25);
55 cvSet2D(g_srcRGB,y,x,s);
59 if (g_srcGray == NULL)
61 g_srcGray = cvCreateMat(g_srcRGB->rows,g_srcRGB->cols,CV_8UC1);
63 cvCvtColor(g_srcRGB,g_srcGray,CV_RGB2GRAY);
67 * Cleanup Dilatometer pointers
69 bool Dilatometer_Cleanup(int id)
71 //if (g_capture != NULL)
72 // cvReleaseCapture(&g_capture);
74 // cvReleaseImageHeader(&frame);
76 //if (g_srcRGB != NULL)
77 // cvReleaseMat(&g_srcRGB); // Causing run time error in cvReleaseMat
78 if (g_srcGray != NULL)
79 cvReleaseMat(&g_srcGray);
81 cvReleaseMat(&g_edges);
86 * Get an image from the Dilatometer. Replaced by Camera_GetImage in image.c
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 // Convert the RGB source file to grayscale
125 cvCvtColor(g_srcRGB,g_srcGray,CV_RGB2GRAY);
127 if ( g_edges == NULL)
129 g_edges = cvCreateMat(g_srcGray->rows,g_srcGray->cols,CV_8UC1);
132 // Commented out lines are used during testing to show the image to screen, can also save the test images
133 //cvShowImage("display", g_srcGray);
136 // Reduce noise with a kernel blurxblur. Input the grayscale source image, output to edges. (0's mean it's determined from kernel sizes)
137 cvSmooth( g_srcGray, g_edges, CV_GAUSSIAN, BLUR, BLUR ,0 ,0 );
140 //cvSaveImage("test_blurred.jpg",g_edges,0);
142 //cvShowImage("display", g_edges);
145 // Find the edges in the image
146 cvCanny( g_edges, g_edges, LOWTHRESHOLD, LOWTHRESHOLD*RATIO, KERNELSIZE );
149 //cvSaveImage("test_edge.jpg",g_edges,0);
151 //cvShowImage("display", g_edges);
157 * Read the dilatometer image. The value changed will correspond to the rate of expansion. If no edge is found then
158 * @param val - Will store the read value if successful
159 * @param samples - Number of rows to scan (increasing will slow down performance!)
160 * @returns true on successful read
162 bool Dilatometer_GetExpansion( double * value, int samples)
166 // Get the image from the camera
167 result = Camera_GetImage( 0, 1600, 1200 ,&g_srcRGB); // Get a 1600x1200 image and place it into src
169 // If an error occured when capturing image then return
173 // Apply the Canny Edge theorem to the image
176 int width = g_edges->cols;
177 int height = g_edges->rows;
179 // If the number of samples is greater than the image height, sample every row
180 if( samples > height)
186 int num_edges = 0; // Number of edges found. if each sample location has an edge, then num_edges = samples
188 for (int i=0; i<samples; i++)
190 // Determine the position in the rows to find the edges.
191 // This will give you a number of evenly spaced samples
192 sample_height = ceil(height * (i + 1) / samples) -1;
194 // 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).
198 for ( int col = 0; col < width; col++)
200 // Count the number of points
201 // Get the threshold of the pixel at the current location
202 CvScalar value = cvGet2D(g_edges, sample_height, col);
203 if( value.val[0]> THRES)
205 edge_location += col;
211 average += ( edge_location / num );
216 average /= num_edges;
218 return result; // As no edges were found
222 result = true; // Successfully found an edge
223 // If the experiment has already been initialised
224 if( lastPosition > 0)
226 // Find the rate of expansion and convert to mm. Will give a negative result for compression.
227 *value = (average - lastPosition) * SCALE;
228 lastPosition = average; // Current position now becomes the last position
235 * Read the dilatometer image. The value changed will correspond to the new location of the edge.
236 * @param val - Will store the read value if successful
237 * @returns true on successful read
239 bool Dilatometer_Read(int id, double * value)
241 bool result = Dilatometer_GetExpansion(value, SAMPLES);
246 * Initialise the dilatometer
248 bool Dilatometer_Init(const char * name, int id)
250 // Make an initial reading (will allocate memory the first time only).
252 lastPosition = 0; // Reset the last position
253 bool result = Dilatometer_GetExpansion(&val, 1);
257 // Overlays a line over the given edge position
258 void Draw_Edge(double edge)
262 for( int i = 0; i < g_srcGray->rows; i++)
264 cvSet2D(g_edges,i,edge,value);
266 cvShowImage("display", g_edges);
268 //cvSaveImage("test_edge_avg.jpg",g_edges,0);
272 static void Dilatometer_GetImageTest( )
274 //Generates Test image
275 //Dilatometer_TestImage();
278 g_srcGray = cvLoadImageM ("testimage4.jpg",CV_LOAD_IMAGE_GRAYSCALE );
282 * For testing purposes
284 /*int main(int argc, char ** argv)
286 //cvNamedWindow( "display", CV_WINDOW_AUTOSIZE );// Create a window for display.
287 //gettimeofday(&start, NULL);
292 cvNamedWindow( "display", CV_WINDOW_AUTOSIZE);
296 Dilatometer_GetEdge(&edge,20000);
297 //For testing purposes, overlay the given average line over the image
300 cvDestroyWindow("display");
302 Dilatometer_Cleanup();