Data-array "organisation" of image Mat. (Mat->GIF)
I'm trying to export some frames as a GIF by using this library: https://github.com/ginsweater/gif-h
Unfortunately, it seems that the char-array the library requires for each frame is ordered differently than Mat#data. Which led me to memory errors when trying to simply pass the Mat-data-array as argument.
I did read the documentation of "Mat", but can't say that I truly understood, and thus don't know how to properly convert it.
I tried to convert it like this:
const int dim = resolution.width * resolution.height * 4;
uint8_t* pixels = (uint8_t*)alloca(sizeof(uint8_t) * dim);
for (int y = 1; y <= res.width; y++){
for (int x = 1; x <= res.height; x++){
uint8_t* pixel = NULL;
f->pixelAt(x-1, y-1, pixel);
pixels[(y*x-1)] = pixel[0];
pixels[(y*x - 1) + 1] = pixel[1];
pixels[(y*x - 1) + 2] = pixel[2];
pixels[(y*x - 1) + 3] = 255;
}
}
Where f->pixelAt(x, y, pixel)
equals to:
void OpenCV_Frame::pixelAt(int x, int y, uint8_t* retVal){
retVal = new uint8_t[3];
cv::Vec3b vec = at<cv::Vec3b>(y, x);
retVal[0] = vec[0];
retVal[1] = vec[1];
retVal[2] = vec[2];
}
I get a stackoverflow error when allocating the uint8_t array. I know that the array is ridiculously big, but I'm not sure how to approach this problem otherwise.
Help appreciated, thanks!
try to convert to rgba first (it seems to expect 4 channels, not 3 and clearly says so in the readme ;) )
@berak Right, just noticed that too - Thanks! I actually tried converting it now, but failed horribly at doing so (Stack Overflow error)... I'll put the code I wrote in my post.
bloody, use cvtColor(src,dst,COLOR_BGR2RGBA), not your shoddy loop !
( there's not a single item correct there ! )
@berak It works. OMG. Thank you so much! Just for the record though, what exactly are all the things that are wrong with my loop :D?
(apologies again, for being so harsh, but i really owe you some explanation here)
so for the record:
pixels[(4 * (y*W + x) + channel]
(not your blunder)again, - lesson is simply: never write that kind of loop, there's always something better, faster, less error-prone already builtin. try to use opencv, not defeat it.