69f263aa0d2507af3c5ccdb093c401ff27b06b7f
[matches/MCTX3420.git] / server / dilatometer.c
1 /**
2  * @file dilatometer.c
3  * @brief Implementation of dilatometer related functions
4  */
5
6 #include "cv.h"
7 #include "highgui_c.h"
8 #include "dilatometer.h"
9 #include <math.h>
10
11 /** Buffer for storing image data. Stored as a  **/
12 static CvMat * g_data = NULL;
13
14
15 /** Camera capture pointer **/
16 static CvCapture * g_capture = NULL;
17
18 /**
19  * Create a test image
20  */
21 void Dilatometer_TestImage()
22 {
23         
24         CvMat *g_dataRGB;
25         g_dataRGB = cvCreateMat(480, 640, CV_8UC3); //32
26
27         //Make a rectangle from col=300 to 500, row=150 to 350
28         /*for (int x = 100; x < 500; ++x)
29         {
30                 CvScalar s; 
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);
34         }*/
35         for (int y = 0; y < 480; ++y)
36         {
37                 CvScalar s; 
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);
41         }
42         if (g_data == NULL)
43         {
44                 g_data = cvCreateMat(g_dataRGB->rows,g_dataRGB->cols,CV_8UC1); //IPL_DEPTH_8U?
45                 cvCvtColor(g_dataRGB,g_data,CV_RGB2GRAY);
46         }
47 }       
48
49 /**
50  * Initialise the dilatometer
51  */
52 void Dilatometer_Init()
53 {
54         
55         // Make an initial reading (will allocate memory the first time only).
56         Dilatometer_Read(1); 
57 }
58
59 /**
60  * Cleanup Interferometer stuff
61  */
62 void Dilatometer_Cleanup()
63 {
64         if (g_data != NULL)
65                 cvReleaseMat(&g_data);
66
67         if (g_capture != NULL)
68                 cvReleaseCapture(&g_capture);
69
70 }
71
72 /**
73  * Get an image from the Dilatometer
74  */
75 static void Dilatometer_GetImage()
76 {       
77         //Test image
78         Dilatometer_TestImage();
79         //Need to implement camera
80 }
81 /**
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
85  */
86 double Dilatometer_Read(int samples)
87 {
88         //Get the latest image
89         Dilatometer_GetImage();
90
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
94         if( samples > height)
95         {
96                 //Log(LOGNOTE, "Number of samples is greater than the dilatometer image height, sampling every row instead.\n");
97                 samples = height;
98         }
99
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;
104         int sample_height;
105         printf("here2; %d\n", width);
106         for (int i=0; i<samples; i++)
107         {
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
113
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);
117
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]);
120
121
122                 for ( int col = 0; col < width; col++)
123                 {
124                         //if ( CV_MAT_ELEM( *g_data, double, col, sample_height) > THRES)
125                         
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)
131                         {
132                                 edges[pos] += (double) col;
133                                 num++;
134                                 printf("here; %f\n", edges[pos]);
135                         }
136                         // If num > 0 and we're not above threshold, we have left the threshold of the edge
137                         else if( num > 0)
138                         {
139                                 // Find the mid point of the edge
140                                 edges[pos] /= num;
141                                 if( edges[1] == 0) 
142                                 {
143                                         pos = 1;        // Move to the right edge
144                                         num = 0;
145                                 }
146                                 else
147                                         break;          // Exit the for loop
148                         }
149                 }
150                 // Determine the width of the can at this row
151                 //widths[i] = edges[1] - edges[0];
152                 average_width += (edges[1] - edges[0]);
153         }
154                 average_width /= samples;
155                 printf("the average width is %f\n", average_width);
156                 return average_width;
157 }
158
159 /**
160  * For testing purposes
161  */
162 int main(int argc, char ** argv)
163 {
164         //cvNamedWindow( "display", CV_WINDOW_AUTOSIZE );// Create a window for display.
165         //gettimeofday(&start, NULL);
166         
167         Dilatometer_Init();
168
169         cvNamedWindow( "display", CV_WINDOW_AUTOSIZE);
170         cvShowImage("display", g_data);
171         cvWaitKey(0);   
172         Dilatometer_Read(5);
173 }
174

UCC git Repository :: git.ucc.asn.au