I'm just added OpenCV (no previous experience with it) to a project of mine to help me retrieve frames from the webcamera. It works well and everything, but I can't really understand the structure of the data in
char *imageData in the
iplimage struct. What I need to do is copy that data into my own representation of an image: an array of floats ( [0,1] ) where the first four (RGBa) elements represent the pixel on last row, first column, the next four the last row, second column etc, in the end reaching the top right pixel of the image.
Some of the problems:
param (in iplimage) doesn't differentiate between signed and unsigned 8 bit integer, how do I know which one is used?
How do I deal with the alignment?
One of the approaches (which doesn't work) is:
1 2 3 4 5 6
|
for (int i = 0; i < mSize.x * mSize.y; ++i){
mPixels[i*4] = (float)frame->imageData[i*3]/255.0f;
mPixels[i*4+1] = (float)frame->imageData[i*3+1]/255.0f;
mPixels[i*4+2] = (float)frame->imageData[i*3+2]/255.0f;
mPixels[i*4+3] = 1.0f;
}
|
Another one, attempting to use alignment (which also doesn't work) is:
1 2 3 4 5 6 7 8 9
|
for( int y=0; y<frame->height; y++ ) {
uchar* ptr = (uchar*) ( frame->imageData + y * frame->widthStep );
for( int x=0; x<frame->width; x++ ) {
mPixels[y * frame->width + x + 2] = (float)ptr[3*x+2]/255.0f;
mPixels[y * frame->width + x + 0] = (float)ptr[3*x+0]/255.0f;
mPixels[y * frame->width + x + 1] = (float)ptr[3*x+1]/255.0f;
mPixels[y * frame->width + x + 3] = 1.0f;
}
}
|
Output: get 0-5 distorted frames (colors all wrong) follow by a crash.
Is there any place where I can find a more detailed description of
imageData than in the official(?) documentation at
http://opencv.willowgarage.com/documentation/basic_structures.html.
Alternatively, can anyone explain/show how it can be done?