I want to get the float value of each pixel to do my own operations. getPixels for the ofCvFloatImage class currently returns the IPL_DEPTH_8U image data, not the floats.
For some reason, if I do this:
IplImage * cvImage = floatImage.getCvImage();
I get
error C2275: ‘IplImage’ : illegal use of this type as an expression
\libs\opencv\cxcore\include\cxtypes.h(393) : see declaration of ‘IplImage’
I modified ofCvFloatImage to return the float pixels
char * pix = floatImage.getFloatPixels();
// like so
char * getFloatPixels(){ return cvImage->imageData; };
however, any value eg pix[100] isn’t a number between 0.0-1.0
a) you will need to make the memory continuous (ie use the memcopy to align memory). opencv images are *not* continuous, so we go through the steps of making them continuous. that’s what happening with the memcpys.
b) because of (a), you will need an internal float “pixels” array, such as:
float * floatPixels; // not width stepped
c) you will want to return a float *, not a char * (otherwise asking for pix[100] is like asking for the 100th unsigned char, not the 100th float).
let me know if that helps - if not we can take a look later at it…
sorry, something else i did after this crashed it. Here is the code
// ofCvFloatImage.h
public:
float * getFloatPixels();
private:
unsigned char * pixels;
// ofCvFloatImage.cpp
void ofCvFloatImage::allocate(int _w, int _h){
if (!bAllocated)
{
....
floatPixels = new float[_w*_h];
....
}
}
It actually returns values from 0.0 to 255.0
I forgot, doing it using getPixels first won’t work as they return unsigned chars (loosing decimal data when converted to 8u). Let me try the memcpy way
void ofCvFloatImage::setFromFloatPixels(float * _pixels, int w, int h){
for (int i = 0; i < h; i++){
memcpy(cvImage->imageData + (i * cvImage->widthStep), _pixels + (i * w), sizeof(float)*w);
}
for (int i = 0; i < h; i++){
memcpy(pixels + (i * w), cvImage->imageData + (i * cvImage->widthStep), sizeof(float)*w);
}
}
it’d be good there to check allocation and if w/h are equal to what’s passed in. for example, someone might pass in something wrong. or before you have allocated, in which you could reallocate the cvImage (free and reallocate) or do some error reporting, etc. we can try to add those functions to the cv image classes.
I think there may be a bug in the code I posted above…
void ofCvFloatImage::setFromFloatPixels(float * _pixels, int w, int h){
for (int i = 0; i < h; i++){
memcpy(cvImage->imageData + (i * cvImage->widthStep), _pixels + (i * w), sizeof(float)*w);
}
for (int i = 0; i < h; i++){
memcpy(pixels + (i * w), cvImage->imageData + (i * cvImage->widthStep), sizeof(float)*w);
}
}
should be…
void ofCvFloatImage::setFromFloatPixels(float * _pixels, int w, int h){
for (int i = 0; i < h; i++){
memcpy(cvImage->imageData + (i * cvImage->widthStep), _pixels + (i * w), sizeof(float)*w);
}
for (int i = 0; i < h; i++){
memcpy(floatPixels + (i * w), cvImage->imageData + (i * cvImage->widthStep), sizeof(float)*w);
}
}
only difference is memcpy(pixels needs to be memcpy(floatPixels