3 * @brief Implementation of dilatometer related functions
8 #include "dilatometer.h"
11 /** Buffer for storing image data. Stored as a **/
12 static CvMat * g_data = NULL;
15 /** Camera capture pointer **/
16 static CvCapture * g_capture = NULL;
21 void Dilatometer_TestImage()
25 g_dataRGB = cvCreateMat(480, 640, CV_8UC3); //32
27 //Make a rectangle from col=300 to 500, row=150 to 350
28 /*for (int x = 100; x < 500; ++x)
31 s.val[0] = 150; s.val[1] = 233; s.val[2] = 244;
32 cvSet2D(g_dataRGB,150,x,s);
33 cvSet2D(g_dataRGB,350,x,s);
35 for (int y = 0; y < 480; ++y)
38 s.val[0] = 200; s.val[1] = 233; s.val[2] = 244;
39 cvSet2D(g_dataRGB,y,100,s);
40 cvSet2D(g_dataRGB,y,500,s);
44 g_data = cvCreateMat(g_dataRGB->rows,g_dataRGB->cols,CV_8UC1); //IPL_DEPTH_8U?
45 cvCvtColor(g_dataRGB,g_data,CV_RGB2GRAY);
50 * Initialise the dilatometer
52 void Dilatometer_Init()
55 // Make an initial reading (will allocate memory the first time only).
60 * Cleanup Interferometer stuff
62 void Dilatometer_Cleanup()
65 cvReleaseMat(&g_data);
67 if (g_capture != NULL)
68 cvReleaseCapture(&g_capture);
73 * Get an image from the Dilatometer
75 static void Dilatometer_GetImage()
78 Dilatometer_TestImage();
79 //Need to implement camera
82 * Read the dilatometer; gets the latest image, processes it, THEN DOES WHAT
83 * @param samples - Number of rows to scan (increasing will slow down performance!)
84 * @returns the average width of the can
86 double Dilatometer_Read(int samples)
88 //Get the latest image
89 Dilatometer_GetImage();
91 int width = g_data->cols;
92 int height = g_data->rows;
93 // If the number of samples is greater than the image height, sample every row
96 //Log(LOGNOTE, "Number of samples is greater than the dilatometer image height, sampling every row instead.\n");
100 // Stores the width of the can at different sample locations. Not necessary unless we want to store this information
101 //double widths[samples];
102 // The average width of the can
103 double average_width;
105 printf("here2; %d\n", width);
106 for (int i=0; i<samples; i++)
108 // Contains the locations of the 2 edges
109 double edges[2] = {0.0,0.0};
110 printf("edges init %f; %f\n", edges[0], edges[1]);
111 int pos = 0; // Position in the edges array (start at left edge)
112 int num = 0; // Keep track of the number of columns above threshold
114 // Determine the position in the rows to find the edges.
115 sample_height = ceil(height * (i + 1) / samples) -1;
116 printf("sample height is %d\n", sample_height);
118 //CvScalar test = cvGet2D(g_data, 150,300);
119 //printf("test is %f,%f,%f,%f\n", test.val[0], test.val[1], test.val[2], test.val[3]);
122 for ( int col = 0; col < width; col++)
124 //if ( CV_MAT_ELEM( *g_data, double, col, sample_height) > THRES)
126 //printf("val is %f\n", cvGet2D(g_data, col, sample_height));
127 //printf("position is col: %d, row: %d\n",col, sample_height);
128 CvScalar value = cvGet2D(g_data, sample_height, col);
129 //printf("value is %f\n", value.val[0]);
130 if( value.val[0]> THRES)
132 edges[pos] += (double) col;
134 printf("here; %f\n", edges[pos]);
136 // If num > 0 and we're not above threshold, we have left the threshold of the edge
139 // Find the mid point of the edge
143 pos = 1; // Move to the right edge
147 break; // Exit the for loop
150 // Determine the width of the can at this row
151 //widths[i] = edges[1] - edges[0];
152 average_width += (edges[1] - edges[0]);
154 average_width /= samples;
155 printf("the average width is %f\n", average_width);
156 return average_width;
160 * For testing purposes
162 int main(int argc, char ** argv)
164 //cvNamedWindow( "display", CV_WINDOW_AUTOSIZE );// Create a window for display.
165 //gettimeofday(&start, NULL);
169 cvNamedWindow( "display", CV_WINDOW_AUTOSIZE);
170 cvShowImage("display", g_data);