Revision 513
fixed memory leak in vision code
vision.c | ||
---|---|---|
23 | 23 |
int count; |
24 | 24 |
}; |
25 | 25 |
|
26 |
static IplImage *image02, *image03, *image04; |
|
27 | 26 |
static char* filename; |
28 | 27 |
|
29 | 28 |
int vision_init(char* filename_) { |
... | ... | |
32 | 31 |
} |
33 | 32 |
|
34 | 33 |
int vision_get_robot_positions(VisionPosition** positions) { |
35 |
CvMemStorage* stor; |
|
36 |
CvSeq* cont; |
|
37 |
CvBox2D32f* box; |
|
38 |
CvPoint* PointArray; |
|
39 |
CvPoint2D32f* PointArray2D32f; |
|
34 |
IplImage* image03; |
|
40 | 35 |
|
41 |
if ((image03 = cvLoadImage(filename, 0)) == 0) { |
|
42 |
fprintf(stderr, "Failed to load image.\n"); |
|
43 |
return -1; |
|
44 |
} |
|
45 |
|
|
46 | 36 |
// load image and force it to be grayscale |
47 | 37 |
if ((image03 = cvLoadImage(filename, 0)) == 0) { |
48 | 38 |
fprintf(stderr, "Failed to load image.\n"); |
... | ... | |
50 | 40 |
} |
51 | 41 |
|
52 | 42 |
// Create the destination images |
53 |
image02 = cvCloneImage(image03); |
|
54 |
image04 = cvCloneImage(image03); |
|
43 |
IplImage* image02 = cvCloneImage(image03);
|
|
44 |
IplImage* image04 = cvCloneImage(image03);
|
|
55 | 45 |
|
56 | 46 |
if (DEBUG) cvNamedWindow("Result", 1); |
57 | 47 |
|
58 | 48 |
// Create dynamic structure and sequence. |
59 |
stor = cvCreateMemStorage(0); |
|
60 |
cont = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint) , stor); |
|
49 |
CvMemStorage* stor = cvCreateMemStorage(0);
|
|
50 |
CvSeq* cont = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint) , stor);
|
|
61 | 51 |
|
62 | 52 |
struct CenterP bestc[100]; |
63 | 53 |
int index=0; |
64 | 54 |
|
65 |
int h; |
|
66 |
for (h = MINH; h < MAXH; h++) { |
|
55 |
for (int h = MINH; h < MAXH; h++) { |
|
67 | 56 |
// Threshold the source image. This needful for cvFindContours(). |
68 | 57 |
cvThreshold(image03, image02, h, 255, CV_THRESH_BINARY); |
69 | 58 |
|
70 | 59 |
// Find all contours. |
71 |
cvFindContours( image02, stor, &cont, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_NONE, cvPoint(0,0));
|
|
60 |
cvFindContours(image02, stor, &cont, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_NONE, cvPoint(0,0)); |
|
72 | 61 |
|
73 | 62 |
// Clear images. IPL use. |
74 | 63 |
cvZero(image02); |
... | ... | |
85 | 74 |
if (count < 6) continue; |
86 | 75 |
|
87 | 76 |
// Alloc memory for contour point set. |
88 |
PointArray = (CvPoint*) malloc(count * sizeof(CvPoint)); |
|
89 |
PointArray2D32f= (CvPoint2D32f*) malloc(count * sizeof(CvPoint2D32f)); |
|
77 |
CvPoint* PointArray = (CvPoint*) malloc(count * sizeof(CvPoint));
|
|
78 |
CvPoint2D32f* PointArray2D32f= (CvPoint2D32f*) malloc(count * sizeof(CvPoint2D32f));
|
|
90 | 79 |
|
91 | 80 |
// Alloc memory for ellipse data. |
92 |
box = (CvBox2D32f*)malloc(sizeof(CvBox2D32f)); |
|
81 |
CvBox2D32f* box = (CvBox2D32f*)malloc(sizeof(CvBox2D32f));
|
|
93 | 82 |
|
94 | 83 |
// Get contour point set. |
95 | 84 |
cvCvtSeqToArray(cont, PointArray, CV_WHOLE_SEQ); |
... | ... | |
175 | 164 |
// Show image. HighGUI use. |
176 | 165 |
if (DEBUG) cvShowImage( "Result", image04 ); |
177 | 166 |
|
167 |
cvReleaseImage(&image04); |
|
168 |
|
|
169 |
cvReleaseMemStorage(&stor); |
|
170 |
|
|
178 | 171 |
*positions = pos_array; |
179 | 172 |
return count; |
180 | 173 |
} |
Also available in: Unified diff