OpenCV:在2d数组到MAT转换后的MAT中的垃圾值

时间:2016-05-06 00:58:28

标签: c++ c arrays opencv image-processing

我有一个2D数组,我使用malloc为其分配内存。

float **landmarksList; // x,y coords

/* Allocate memory for the training ASM landmarks list */
landmarksList = (float **) malloc(sizeof(float *) * TotalTrainCnt);
for (int i = 0; i < TotalTrainCnt; i++)
{   
    landmarksList[i] = (float *)malloc(sizeof(float) * VECTOR_SIZE); 
}   
/* Initialize to 0 */
for (int i = 0; i < TotalTrainCnt; i++)
{   
    for (int j = 0; j < VECTOR_SIZE; j++)
    {
        landmarksList[i][j] = 0;
    }
}   

在我的情况下,TotalTrainCnt为15,VECTOR_SIZE为154.我将landmarksList转换为Mat对象,如下所示。

Mat trainingDataMat(TotalTrainCnt, VECTOR_SIZE, CV_32FC1, landmarksList);

转换完成后,我会看到Mat中添加了一些垃圾数据。

------- landmarksList[0] ------- 
195 223 194 257 199 291 207 323 236 368 280 396 312 401 345 393 381     362 401 319 408 286 412 255 410 223 358 117 305 106 253 116 260 194 234 189 212 202 234 202 257 206 284 208 333 210 356 195 380 190 401 202 380 202 358 207 362 211 254 210 275 225 264 217 254 214 245 217 235 223 245 228 255 230 265 229 256 221 362 222 342 225 352 218 362 215 371 218 380 224 371 229 362 231 352 229 324 251 309 251 294 252 294 279 311 270 327 280 340 269 333 285 311 290 286 285 278 268 268 320 286 311 302 307 311 308 320 306 335 310 350 319 329 318 311 318 292 318 292 321 311 324 329 321 339 328 326 333 311 335 295 333 281 329 
-------- Mat --------
[8.1469179e-34, 8.1474909e-34, 8.148064e-34, 8.148637e-34, 8.1492101e-34, 8.1497831e-34, 8.1503562e-34, 8.1509293e-34, 8.1515023e-34, 8.1520754e-34, 8.1526484e-34, 8.1532215e-34, 8.1537945e-34, 8.1543676e-34, 8.1549406e-34, 8.7581154e-43, 195, 223, 194, 257, 199, 291, 207, 323, 236, 368, 280, 396, 312, 401, 345, 393, 381, 362, 401, 319, 408, 286, 412, 255, 410, 223, 358, 117, 305, 106, 253, 116, 260, 194, 234, 189, 212, 202, 234, 202, 257, 206, 284, 208, 333, 210, 356, 195, 380, 190, 401, 202, 380, 202, 358, 207, 362, 211, 254, 210, 275, 225, 264, 217, 254, 214, 245, 217, 235, 223, 245, 228, 255, 230, 265, 229, 256, 221, 362, 222, 342, 225, 352, 218, 362, 215, 371, 218, 380, 224, 371, 229, 362, 231, 352, 229, 324, 251, 309, 251, 294, 252, 294, 279, 311, 270, 327, 280, 340, 269, 333, 285, 311, 290, 286, 285, 278, 268, 268, 320, 286, 311, 302, 307, 311, 308, 320, 306, 335, 310, 350, 319, 329, 318, 311, 318, 292, 318;
 292, 321, 311, 324, 329, 321, 339, 328, 326, 333, 311, 335, 295, 333, 281, 329, 0, 8.7581154e-43, 168, 228, 170, 264, 175, 303, 189, 353, 220, 397, 261, 427, 298, 434, 336, 426, 374, 395, 400, 349, 412, 306, 417, 270, 420, 231, 344, 115, 286, 101, 230, 110, 242, 187, 213, 187, 191, 207, 215, 200, 242, 200, 270, 202, 322, 205, 350, 191, 379, 193, 401, 215, 376, 206, 350, 205, 351, 212, 236, 208, 260, 228, 248, 219, 236, 216, 226, 220, 215, 227, 226, 233, 237, 235, 248, 232, 235, 224, 350, 228, 329, 231, 341, 224, 352, 220, 363, 224, 373, 232, 363, 237, 352, 239, 341, 237, 315, 262, 297, 261, 280, 261, 278, 296, 298, 285, 316, 297, 336, 292, 324, 303, 298, 310, 270, 302, 259, 291, 244, 346, 265, 335, 286, 332, 298, 333, 310, 332, 330, 336, 350, 348, 322, 343, 298, 343;

Mat在开头包含一些垃圾数据。因此,第一行的某些值会进入第二行,并且所有行都会发生这种情况。

当我尝试以下操作时,

Mat trainingDataMat(TotalTrainCnt, VECTOR_SIZE, CV_32FC1, &landmarksList[0][0]);

第一行有正确的数据,但第二行以垃圾值开头。

------- landmarksList[0] ------- 
195 223 194 257 199 291 207 323 236 368 280 396 312 401 345 393 381 362 401 319 408 286 412 255 410 223 358 117 305 106 253 116 260 194 234 189 212 202 234 202 257 206 284 208 333 210 356 195 380 190 401 202 380 202 358 207 362 211 254 210 275 225 264 217 254 214 245 217 235 223 245 228 255 230 265 229 256 221 362 222 342 225 352 218 362 215 371 218 380 224 371 229 362 231 352 229 324 251 309 251 294 252 294 279 311 270 327 280 340 269 333 285 311 290 286 285 278 268 268 320 286 311 302 307 311 308 320 306 335 310 350 319 329 318 311 318 292 318 292 321 311 324 329 321 339 328 326 333 311 335 295 333 281 329 
------- landmarksList[1] ------- 
168 228 170 264 175 303 189 353 220 397 261 427 298 434 336 426 374 395 400 349 412 306 417 270 420 231 344 115 286 101 230 110 242 187 213 187 191 207 215 200 242 200 270 202 322 205 350 191 379 193 401 215 376 206 350 205 351 212 236 208 260 228 248 219 236 216 226 220 215 227 226 233 237 235 248 232 235 224 350 228 329 231 341 224 352 220 363 224 373 232 363 237 352 239 341 237 315 262 297 261 280 261 278 296 298 285 316 297 336 292 324 303 298 310 270 302 259 291 244 346 265 335 286 332 298 333 310 332 330 336 350 348 322 343 298 343 274 342 274 362 299 368 323 364 340 370 322 382 299 385 276 381 256 368 
-------- Mat --------
[195, 223, 194, 257, 199, 291, 207, 323, 236, 368, 280, 396, 312, 401, 345, 393, 381, 362, 401, 319, 408, 286, 412, 255, 410, 223, 358, 117, 305, 106, 253, 116, 260, 194, 234, 189, 212, 202, 234, 202, 257, 206, 284, 208, 333, 210, 356, 195, 380, 190, 401, 202, 380, 202, 358, 207, 362, 211, 254, 210, 275, 225, 264, 217, 254, 214, 245, 217, 235, 223, 245, 228, 255, 230, 265, 229, 256, 221, 362, 222, 342, 225, 352, 218, 362, 215, 371, 218, 380, 224, 371, 229, 362, 231, 352, 229, 324, 251, 309, 251, 294, 252, 294, 279, 311, 270, 327, 280, 340, 269, 333, 285, 311, 290, 286, 285, 278, 268, 268, 320, 286, 311, 302, 307, 311, 308, 320, 306, 335, 310, 350, 319, 329, 318, 311, 318, 292, 318, 292, 321, 311, 324, 329, 321, 339, 328, 326, 333, 311, 335, 295, 333, 281, 329;
 0, 8.7581154e-43, 168, 228, 170, 264, 175, 303, 189, 353, 220, 397, 261, 427, 298, 434, 336, 426, 374, 395, 400, 349, 412, 306, 417, 270, 420, 231, 344, 115, 286, 101, 230, 110, 242, 187, 213, 187, 191, 207, 215, 200, 242, 200, 270, 202, 322, 205, 350, 191, 379, 193, 401, 215, 376, 206, 350, 205, 351, 212, 236, 208, 260, 228, 248, 219, 236, 216, 226, 220, 215, 227, 226, 233, 237, 235, 248, 232, 235, 224, 350, 228, 329, 231, 341, 224, 352, 220, 363, 224, 373, 232, 363, 237, 352, 239, 341, 237, 315, 262, 297, 261, 280, 261, 278, 296, 298, 285, 316, 297, 336, 292, 324, 303, 298, 310, 270, 302, 259, 291, 244, 346, 265, 335, 286, 332, 298, 333, 310, 332, 330, 336, 350, 348, 322, 343, 298, 343, 274, 342, 274, 362, 299, 368, 323, 364, 340, 370, 322, 382, 299, 385, 276, 381;
 256, 368, 0, 8.7581154e-43, 264, 236, 266, 276, 271, 314, 283, 356, 313, 398, 362, 418, 402, 420, 439, 412, 485, 386, 510, 339, 517, 301, 519, 264, 519, 221, 440, 111, 383, 103, 327, 118, 339, 204, 315, 203, 295, 215, 317, 214, 339, 215, 364, 214, 432, 208, 458, 196, 481, 194, 499, 204, 479, 205, 459, 207, 453, 213, 337, 220, 359, 233, 348, 226, 337, 223, 328, 227, 319, 233, 329, 238, 338, 240, 349, 237, 337, 231, 450, 224, 433, 228, 443, 220, 453, 216, 462, 219, 472, 225, 462, 230, 453, 233, 443, 232, 418, 266, 400, 266, 382, 267, 383, 299, 401, 287, 419, 298, 436, 292, 424, 306, 401, 311, 376, 307, 364, 296, 358, 349, 375, 339, 391, 335, 401, 335, 411, 334, 425, 338, 440, 347, 418, 344, 400, 344, 382, 345, 382, 347, 400, 348, 417, 345, 428, 355, 415, 357, 400, 359;

我是OpenCV的新手并使用SVM进行项目。由于这种损坏,我的SVM无法正确预测测试数据。请帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

Mat构造函数需要一个连续的内存块,以便它可以通过指向内存的指针前进到某个行和列,分配内存的方式不是单个块,它已经停止{ {1}}内存块。

要解决此问题,请使用此方法分配内存:

TotalTrainCnt

在此// define a pointer to array of VECTOR_SIZE floats float (*landmarksList)[VECTOR_SIZE]; // allocate TotalTrainCnt of such arrays landmarksList = malloc(sizeof(*landmarksList) * TotalTrainCnt); landmarksList行和TotalTrainCnt列单元的内存块之后,您可以像以前一样访问每个元素,例如VECTOR_SIZE是第j个第i行中的数据。现在你可以做到

landmarksList[i][j]

创建一个Mat trainingDataMat(TotalTrainCnt, VECTOR_SIZE, CV_32FC1, landmarksList); 对象。这样做也更好,因为当你完成它需要一次调用Mat来释放整个内存,它不需要单独释放每一行。