Commit d2b7bc13c67dcbacff090b0416292a1686d671b8
1 parent
a74689b7
修正内存泄漏,崩潰問題
Showing
3 changed files
with
37 additions
and
11 deletions
src/left_over/MSRegionSurveilanceCpu.cpp
@@ -25,7 +25,8 @@ int IRegionSurveillanceCpu::rs_init(const rs_params &param) | @@ -25,7 +25,8 @@ int IRegionSurveillanceCpu::rs_init(const rs_params &param) | ||
25 | { | 25 | { |
26 | int r = 0; | 26 | int r = 0; |
27 | IplImage * gray = cvCreateImage(cvSize(param.image.w_, param.image.h_), 8, 1); | 27 | IplImage * gray = cvCreateImage(cvSize(param.image.w_, param.image.h_), 8, 1); |
28 | - cvSetData(gray, param.image.data_, param.image.w_); | 28 | + memcpy(gray->imageData, param.image.data_, param.image.w_ * param.image.h_* param.image.c_); |
29 | + //cvSetData(gray, param.image.data_, param.image.w_); | ||
29 | for (int i = 0; i < rect_num; ++i) | 30 | for (int i = 0; i < rect_num; ++i) |
30 | { | 31 | { |
31 | IplImage * img = cvCreateImage(cvSize(rect[i].width_, rect[i].height_), 8, 1); | 32 | IplImage * img = cvCreateImage(cvSize(rect[i].width_, rect[i].height_), 8, 1); |
@@ -35,7 +36,7 @@ int IRegionSurveillanceCpu::rs_init(const rs_params &param) | @@ -35,7 +36,7 @@ int IRegionSurveillanceCpu::rs_init(const rs_params &param) | ||
35 | (unsigned char*)(img->imageData), 1/*param.image.c_*/, 1/*param.channel_deal*/, param.filt_flag, param.min_area, param.max_area); | 36 | (unsigned char*)(img->imageData), 1/*param.image.c_*/, 1/*param.channel_deal*/, param.filt_flag, param.min_area, param.max_area); |
36 | cvReleaseImage(&img); | 37 | cvReleaseImage(&img); |
37 | } | 38 | } |
38 | - //cvReleaseImage(&gray); | 39 | + cvReleaseImage(&gray); |
39 | return r; | 40 | return r; |
40 | } | 41 | } |
41 | int IRegionSurveillanceCpu::rs_init_region(int num_roi, region_info* region_infos/*, bool iflog*/) | 42 | int IRegionSurveillanceCpu::rs_init_region(int num_roi, region_info* region_infos/*, bool iflog*/) |
@@ -87,8 +88,11 @@ int IRegionSurveillanceCpu::rs_detect(const sy_img &img_data, region_info* regio | @@ -87,8 +88,11 @@ int IRegionSurveillanceCpu::rs_detect(const sy_img &img_data, region_info* regio | ||
87 | 88 | ||
88 | int totalObjCount = 0; | 89 | int totalObjCount = 0; |
89 | int totalDelCount = 0; | 90 | int totalDelCount = 0; |
91 | + bool toObjCountBreak = false; | ||
92 | + bool toDelCountBreak = false; | ||
90 | IplImage * gray = cvCreateImage(cvSize(img_data.w_, img_data.h_), 8, 1); | 93 | IplImage * gray = cvCreateImage(cvSize(img_data.w_, img_data.h_), 8, 1); |
91 | - cvSetData(gray, img_data.data_, img_data.w_); | 94 | + memcpy(gray->imageData, img_data.data_, img_data.w_*img_data.h_ * img_data.c_); |
95 | + //cvSetData(gray, img_data.data_, img_data.w_); | ||
92 | for (int k = 0; k < rect_num; ++k) | 96 | for (int k = 0; k < rect_num; ++k) |
93 | //int k = 0; | 97 | //int k = 0; |
94 | { | 98 | { |
@@ -100,12 +104,13 @@ int IRegionSurveillanceCpu::rs_detect(const sy_img &img_data, region_info* regio | @@ -100,12 +104,13 @@ int IRegionSurveillanceCpu::rs_detect(const sy_img &img_data, region_info* regio | ||
100 | 104 | ||
101 | auto * ObjInfo = new MS_ObjectInfo[ObjCount]; | 105 | auto * ObjInfo = new MS_ObjectInfo[ObjCount]; |
102 | IReginCpu[k]->getObjectInfo(ObjCount, ObjInfo); | 106 | IReginCpu[k]->getObjectInfo(ObjCount, ObjInfo); |
103 | - for (int i = 0; i < ObjCount; ++i) | 107 | + for (int i = 0; i < ObjCount && !toObjCountBreak; ++i) |
104 | { | 108 | { |
105 | result->obj_infos[totalObjCount].curPos.x_ = ObjInfo[i].curPos.x+rect[k].left_; // 当前坐标 | 109 | result->obj_infos[totalObjCount].curPos.x_ = ObjInfo[i].curPos.x+rect[k].left_; // 当前坐标 |
106 | result->obj_infos[totalObjCount].curPos.y_ = ObjInfo[i].curPos.y+rect[k].top_; // 当前坐标 | 110 | result->obj_infos[totalObjCount].curPos.y_ = ObjInfo[i].curPos.y+rect[k].top_; // 当前坐标 |
107 | 111 | ||
108 | result->obj_infos[totalObjCount].trace.trace_num = ObjInfo[i].trace.nTraceNum; // 轨迹及报警状态 | 112 | result->obj_infos[totalObjCount].trace.trace_num = ObjInfo[i].trace.nTraceNum; // 轨迹及报警状态 |
113 | + printf("nTraceNum = %d\n", ObjInfo[i].trace.nTraceNum); | ||
109 | for (int j = 0; j < MAXTRACENUM; ++j) | 114 | for (int j = 0; j < MAXTRACENUM; ++j) |
110 | { | 115 | { |
111 | result->obj_infos[totalObjCount].trace.obj_trace[j].x_ = ObjInfo[i].trace.pObjTrace[j].x+rect[k].left_; | 116 | result->obj_infos[totalObjCount].trace.obj_trace[j].x_ = ObjInfo[i].trace.pObjTrace[j].x+rect[k].left_; |
@@ -123,19 +128,26 @@ int IRegionSurveillanceCpu::rs_detect(const sy_img &img_data, region_info* regio | @@ -123,19 +128,26 @@ int IRegionSurveillanceCpu::rs_detect(const sy_img &img_data, region_info* regio | ||
123 | result->obj_infos[totalObjCount].pb_alarm_type[j] = ObjInfo[i].pbAlarmType[j]; | 128 | result->obj_infos[totalObjCount].pb_alarm_type[j] = ObjInfo[i].pbAlarmType[j]; |
124 | } | 129 | } |
125 | ++totalObjCount; | 130 | ++totalObjCount; |
131 | + if (totalObjCount >= MACDETECTOBJNUM) | ||
132 | + { | ||
133 | + toObjCountBreak = true; | ||
134 | + break; | ||
135 | + } | ||
126 | } | 136 | } |
127 | - | 137 | + cvReleaseImage(&img); |
128 | delete ObjInfo; | 138 | delete ObjInfo; |
139 | + | ||
129 | auto delCount = IReginCpu[k]->getDeleteNum(); | 140 | auto delCount = IReginCpu[k]->getDeleteNum(); |
130 | 141 | ||
131 | - auto * DelInfo = new MS_ObjectInfo[delCount]; | 142 | + auto * DelInfo = new MS_ObjectInfo[delCount]{}; |
132 | IReginCpu[k]->getDeleteInfo(delCount, DelInfo); | 143 | IReginCpu[k]->getDeleteInfo(delCount, DelInfo); |
133 | - for (int i = 0; i < delCount; ++i) | 144 | + for (int i = 0; i < delCount && !toDelCountBreak; ++i) |
134 | { | 145 | { |
135 | result->del_infos[totalDelCount].curPos.x_ = DelInfo[i].curPos.x + rect[k].left_; // 当前坐标 | 146 | result->del_infos[totalDelCount].curPos.x_ = DelInfo[i].curPos.x + rect[k].left_; // 当前坐标 |
136 | result->del_infos[totalDelCount].curPos.y_ = DelInfo[i].curPos.y + rect[k].top_; // 当前坐标 | 147 | result->del_infos[totalDelCount].curPos.y_ = DelInfo[i].curPos.y + rect[k].top_; // 当前坐标 |
137 | 148 | ||
138 | result->del_infos[totalDelCount].trace.trace_num = DelInfo[i].trace.nTraceNum; // 轨迹及报警状态 | 149 | result->del_infos[totalDelCount].trace.trace_num = DelInfo[i].trace.nTraceNum; // 轨迹及报警状态 |
150 | + printf("del_infos nTraceNum = %d\n", DelInfo[i].trace.nTraceNum); | ||
139 | for (int j = 0; j < MAXTRACENUM; ++j) | 151 | for (int j = 0; j < MAXTRACENUM; ++j) |
140 | { | 152 | { |
141 | result->del_infos[totalDelCount].trace.obj_trace[j].x_ = DelInfo[i].trace.pObjTrace[j].x + rect[k].left_; | 153 | result->del_infos[totalDelCount].trace.obj_trace[j].x_ = DelInfo[i].trace.pObjTrace[j].x + rect[k].left_; |
@@ -153,11 +165,17 @@ int IRegionSurveillanceCpu::rs_detect(const sy_img &img_data, region_info* regio | @@ -153,11 +165,17 @@ int IRegionSurveillanceCpu::rs_detect(const sy_img &img_data, region_info* regio | ||
153 | result->del_infos[totalDelCount].pb_alarm_type[j] = DelInfo[i].pbAlarmType[j]; | 165 | result->del_infos[totalDelCount].pb_alarm_type[j] = DelInfo[i].pbAlarmType[j]; |
154 | } | 166 | } |
155 | ++totalDelCount; | 167 | ++totalDelCount; |
168 | + if (totalDelCount >= MACDETECTOBJNUM) | ||
169 | + { | ||
170 | + toDelCountBreak = true; | ||
171 | + break; | ||
172 | + } | ||
156 | } | 173 | } |
157 | delete DelInfo; | 174 | delete DelInfo; |
158 | } | 175 | } |
159 | result->obj_count = totalObjCount; | 176 | result->obj_count = totalObjCount; |
160 | result->del_count = totalDelCount; | 177 | result->del_count = totalDelCount; |
178 | + cvReleaseImage(&gray); | ||
161 | return 1; | 179 | return 1; |
162 | } | 180 | } |
163 | //int IRegionSurveillanceCpu::rs_get_arrowdir(const sy_point &p_roi0, const sy_point &p_roi1, const sy_point &dir) | 181 | //int IRegionSurveillanceCpu::rs_get_arrowdir(const sy_point &p_roi0, const sy_point &p_roi1, const sy_point &dir) |
src/left_over/RegionSurveillanceProcess.h
1 | #pragma once | 1 | #pragma once |
2 | //#include "MSRegionSurveilance.h" | 2 | //#include "MSRegionSurveilance.h" |
3 | #include "left_over_det.h" | 3 | #include "left_over_det.h" |
4 | -#include <boost/thread.hpp> | 4 | +//#include <boost/thread.hpp> |
5 | #include "head.h" | 5 | #include "head.h" |
6 | #include <atomic> | 6 | #include <atomic> |
7 | //#include "MSRegionSurveilanceVpt.h" | 7 | //#include "MSRegionSurveilanceVpt.h" |
@@ -43,7 +43,7 @@ public: | @@ -43,7 +43,7 @@ public: | ||
43 | //int long_side; | 43 | //int long_side; |
44 | //int short_side; | 44 | //int short_side; |
45 | std::atomic<bool> thrd_run; | 45 | std::atomic<bool> thrd_run; |
46 | - boost::thread thrd; | 46 | + //boost::thread thrd; |
47 | long frame_num = 0; | 47 | long frame_num = 0; |
48 | bool init_flag = false; | 48 | bool init_flag = false; |
49 | sy_rect rect[MAXVERTEXNUM]; //ÓÐÐ§ÇøÓò | 49 | sy_rect rect[MAXVERTEXNUM]; //ÓÐÐ§ÇøÓò |
src/left_over/ms_region_surveilance.cpp
@@ -242,9 +242,12 @@ int rs_process(void *handle, sy_img img_data, rs_result *result) | @@ -242,9 +242,12 @@ int rs_process(void *handle, sy_img img_data, rs_result *result) | ||
242 | 242 | ||
243 | IplImage * gray = cvCreateImage(cvSize(srcscale->width, srcscale->height), 8, 1); | 243 | IplImage * gray = cvCreateImage(cvSize(srcscale->width, srcscale->height), 8, 1); |
244 | cvCvtColor(srcscale, gray, CV_RGB2GRAY); | 244 | cvCvtColor(srcscale, gray, CV_RGB2GRAY); |
245 | + | ||
245 | sy_img image; | 246 | sy_img image; |
247 | + unsigned char * data = (unsigned char *)malloc(gray->width*gray->height*gray->nChannels); | ||
248 | + memcpy(data, gray->imageData, gray->width*gray->height*gray->nChannels); | ||
246 | //image.set_data(srcImage.cols, srcImage.rows, srcImage.channels(), (unsigned char *)srcImage.data); | 249 | //image.set_data(srcImage.cols, srcImage.rows, srcImage.channels(), (unsigned char *)srcImage.data); |
247 | - image.set_data(gray->width, gray->height, gray->nChannels, (unsigned char *)gray->imageData); | 250 | + image.set_data(gray->width, gray->height, gray->nChannels, (unsigned char *)data); |
248 | if (regin->init_flag == false) | 251 | if (regin->init_flag == false) |
249 | { | 252 | { |
250 | rs_params param; | 253 | rs_params param; |
@@ -258,11 +261,14 @@ int rs_process(void *handle, sy_img img_data, rs_result *result) | @@ -258,11 +261,14 @@ int rs_process(void *handle, sy_img img_data, rs_result *result) | ||
258 | param.min_area = 100; | 261 | param.min_area = 100; |
259 | param.max_area = 1000000; | 262 | param.max_area = 1000000; |
260 | regin->rs_init(param); | 263 | regin->rs_init(param); |
264 | + | ||
261 | regin->init_flag = true; | 265 | regin->init_flag = true; |
262 | int ret = regin->rs_init_region(1, &pRegionInfo/*, false*/); | 266 | int ret = regin->rs_init_region(1, &pRegionInfo/*, false*/); |
263 | - } | ||
264 | 267 | ||
268 | + } | ||
269 | + | ||
265 | int ret = regin->rs_detect(image, &pRegionInfo, result); | 270 | int ret = regin->rs_detect(image, &pRegionInfo, result); |
271 | + | ||
266 | for (int i = 0; i < result->obj_count; ++i) | 272 | for (int i = 0; i < result->obj_count; ++i) |
267 | { | 273 | { |
268 | result->obj_infos[i].curPos.x_ *= regin->scale; | 274 | result->obj_infos[i].curPos.x_ *= regin->scale; |
@@ -296,8 +302,10 @@ int rs_process(void *handle, sy_img img_data, rs_result *result) | @@ -296,8 +302,10 @@ int rs_process(void *handle, sy_img img_data, rs_result *result) | ||
296 | result->del_infos[i].tar_box.left_ *= regin->scale; | 302 | result->del_infos[i].tar_box.left_ *= regin->scale; |
297 | result->del_infos[i].tar_box.top_ *= regin->scale; | 303 | result->del_infos[i].tar_box.top_ *= regin->scale; |
298 | } | 304 | } |
305 | + free(data); | ||
299 | cvReleaseImage(&gray); | 306 | cvReleaseImage(&gray); |
300 | cvReleaseImage(&srcscale); | 307 | cvReleaseImage(&srcscale); |
308 | + //cvReleaseImage(&gray); | ||
301 | //cvReleaseImage(&srcscale2); | 309 | //cvReleaseImage(&srcscale2); |
302 | return ret; | 310 | return ret; |
303 | } | 311 | } |