How to convert unsigned char* to ofPixels_<unsigned char> correctly?


#1

when I download the source code there are usually some code like that" unsigned char* pixels = image.getPixels();"
and this come up a error that " No viable conversion from ofPixels_ to unsigned char*",
so I fix the code like that
“ofPixels_ pixels_ = previewImage.getPixels();
int num = previewImage.getWidth() * previewImage.getHeight() 3;
unsigned char
pixels = new unsigned char [num];
for (int i=0; i<num; i++) {
unsigned char value = pixels_[num];
pixels[num] = value;
}”
to fix it, it can build succeeded, but there is a new error like the screenshot below:


can anyone tell me the way to convert it correctly?? thx a lot!!


#2

Hi there,

You are not supposed to need ofPixels<unsigned char> pixels usually you should do ofPixels pixels.

I guess your previewImage is an ofImage, not an ofPixels, if that is the case:

Some months ago the access to pixels raw pointer from an image changed from image.getPixels() to image.getPixels().getData(). Please try that and let us know how if that was the problem.


#3

yes not sure what you are trying to do but it seems you only need to change:

warp.pixelsForFrame(warp.getCapacity() - (frameStep*i) - 1, previewImage.getPixels().getData());

all the code to go from ofPixels -> unsigned char* above is unnecesary and the line below, previewImage.setFromPixels, it’s setting the same pixels that you got from the image back into the image which wouldn’t be necesary i imagine if you just pass the pixels from the image drictly to warp as done above


#4

Oh it fixed, previewImage is an ofImage, and I changed from “unsigned char* pixels = previewImage.getpixels( );” to “unsigned char* pixels = previewImage.getPixels( ).getData( );” it Works ,thx u so much!:kissing_heart:


#5

yes i changed it from getPixels(() to getPixels( ).getData( ),then it works!
Thx a lot!
but i stiil confused about ofPixels_ & unsigned char*…could you tell me the differences?:sweat_smile: (I know is sounds stupid…since I don’t have CS background so it may be a little confused for me…


#6

unsigned char*means an array of unsigned chars, or what’s the same an array of numbers between 0…255. ofPixels internally contains an array of unisgned char but it wraps it in an object so you can also access it’s size, with, height…

with unsigned char* you need to keep width, height, number of channels in variables your self, while with ofPixels you can do pixels.getWidth(), pixels.getHeight()…

ofPixels also releases any used memory automatically while if you use unsigned char* you need to delete it your self

it’s really pretty much the same except ofPixels it’s easier to use.


#7

I see it now!! thank you!!


#8

and what’s < btw? is it the same as ofPixels?


#9

yes ofPixels is just an alias for ofPixels_<unsigned char> there’s other types of ofPixels, you can also use for example ofPixels_` which is also called ofFloatPixels if you want to use 32bit images.


#10

I found this thread with the same question. It came up because I’m leaning image processing oF from the ofBook on the website, which apparently hasn’t been updated to reflect changes in the framework. Unfortunately the API docs aren’t much help either… unless I’m missing something.

Here’s the offending page, if anybody has the ability to fix it:

http://openframeworks.cc/ofBook/chapters/image_processing_computer_vision.html

Screenshot:


#11

can you open an issue in the books github? that will help the author of that chapter know there’s a bug in the examples


#12

Hello,

I am working out this exemple as well in ofBook and was curious wether anyone had opened a thread about it (if so, sorry I could not find it)?

I’ve replaced
myVideoGrabber.getPixels();
by
myVideoGrabber.getPixels().getData();

but it doesn’t work quite right.
Any comment appreciated!