Reputation: 175
I have a 2D array for which I allocate memory using 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;
}
}
In my case TotalTrainCnt
is 15 and VECTOR_SIZE
is 154. I converted the landmarksList
to Mat
object as shown below.
Mat trainingDataMat(TotalTrainCnt, VECTOR_SIZE, CV_32FC1, landmarksList);
After this conversion, I see some junk data added in the 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;
The Mat
contains some junk data at the beginning. Because of this the some of the values of first row run into the second row and this happens for all the rows.
When I tried the following,
Mat trainingDataMat(TotalTrainCnt, VECTOR_SIZE, CV_32FC1, &landmarksList[0][0]);
the first row has the correct data, but the second row starts with junk values.
------- 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;
I am new to OpenCV and doing a project using SVM. My SVM do not predict the test data correctly because of this corruption. Please help me to fix this issue.
Upvotes: 0
Views: 150
Reputation: 13836
The Mat
constructor expect a continuous block of memory, so that it can step to certain row and column by advance the pointer to the memory, the way you allocated the memory is not a single block, it's discontinued TotalTrainCnt
blocks of memory.
To fix this, use this to allocate the memory:
// define a pointer to array of VECTOR_SIZE floats
float (*landmarksList)[VECTOR_SIZE];
// allocate TotalTrainCnt of such arrays
landmarksList = malloc(sizeof(*landmarksList) * TotalTrainCnt);
After this landmarksList
is a memory block of TotalTrainCnt
rows and VECTOR_SIZE
columns of flats, you can access each element the same as before, like landmarksList[i][j]
is the jth data in ith row. Now you can do
Mat trainingDataMat(TotalTrainCnt, VECTOR_SIZE, CV_32FC1, landmarksList);
to create a Mat
object. It's also better this way because when you are done it requires one call to free
to free the entire memory, it does not need to free each row separately.
Upvotes: 1