going from ofxCvGrayscaleImage to ofPixels and back?

Hello again!
I’ve been trying out this problem in which it is easier to analize a scene from a webcam on a per pixel basis so I’ve been trying like this:

  
ofxCvGrayscaleImage 	grayBg;  
ofPixels                monoPixels;  
  
grayBg.allocate(320,240);  
monoPixels.allocate(320,240,OF_PIXELS_MONO);  
  
monoPixels.setFromPixels(grayBg.getPixels(), 320,240, OF_PIXELS_MONO);  

The result is a scene which isn’t rendered properly and when I tweak the code I also get:
[tt]no matching function for call to ‘ofPixels_::setFromPixels(ofxCvGrayscaleImage&)’[/tt]

I tried doing it with ofFbo and it works great but I also think it is overkill.
I think I’m doing the conversion from ofxCvGrayscaleImage to ofPixels wrong, after that I’d like to know how to take those modified pixels and get them back to an ofxCvGrayscaleImage.

Is this even possible? Any suggestions would be great and thanks! =D

Should this:

  
monoPixels.setFromPixels(grayBg.getPixels(), 320,240, OF_PIXELS_MONO);  

be this:

  
monoPixels.setFromPixels(grayBg.getPixels(), 320,240, 1);  

so that it corresponds with the signature from ofPixels

  
void setFromPixels(const PixelType * newPixels,int w, int h, int channels);  

Will try it out tomorrow and get back to you.
As always thank you very much Joshua!

Edit—>
It worked great! thank you! =D

Ok so everything went ok going from ofxCvGrayscaleImage to ofPixels but how about going back?

I used the following code but it may be redundant. It works great for going from ofPixels to ofxCvGrayscaleImage. It uses an intermediate ofTexture, but any suggestions will be welcome! Thanks!
testapp.h

  
  
unsigned char              *grayPixels;  
ofTexture                    texGray;  
ofPixels                       grayPixelsTwo;  
ofxCvGrayscaleImage     step_Tex_openCV;  

setup in testapp.cpp

  
  
grayPixels	= new unsigned char [320*240];  
texGray.allocate(320,240,GL_LUMINANCE);  
grayPixelsTwo.allocate(320,240,OF_PIXELS_MONO);  
step_Tex_openCV.allocate(320,240);  

update in testapp.cpp

  
  
texGray.loadData(grayPixels, 320,240, GL_LUMINANCE);  
texGray.readToPixels(grayPixelsTwo);  
step_Tex_openCV.setFromPixels(grayPixelsTwo);  

If you want to set the pixels in the ofxCvGrayscaleImage you just need to call setFromPixels(), like:

  
cvGS.setFromPixels( pixels.getPixels() ); // untested  

That gets the unsigned char* for the data in the ofPixels object.

Will do.
Thank you very much =D

Edit-> Now I get it, I was using “textureExample” as template using “unsigned char * colorPixels;” in order to use the pixels, but I think this is an old example that could use ofPixels very well or am I wrong?

That may be an old example. In any case, there’s really never a reason to copy pixel data from a texture (at least that I can think of, there’s probably one, but it’s not so important).

you can do:

  
cvimg.setFromPixels(pixels);  

and

  
pixels.setFromPixels(cvimg.getPixelsRef());  

where cvimg is an ofxCvImage and pixels an ofPixels

in the second case there’s also no need to allocate previously, in the first case also if you are using the develop branch.

1 Like

Thanks! :stuck_out_tongue:

I’ll use it for my next project then!

Hi,

I don’t know were to post this on the forum.
I need your help with ofxCv…

I’m using ofxOpenNI and I want to use the depth pixels in a Mat image.
when I do something like this,

ofImage depthPixels;
Mat depth = toCv(depthPixels)

I obtain strange results…
With the color image works ok, but not with the depth image. Can you help me?

best regards

@paulo.trigueros
I don’t mean to be disrespectful but you have asked this many times in the forum and you ask for quick solutions too which might never happen. I’ve also gotten your email by the way. I don’t know what the others think about this but anyways I’ll make a big suggestion: Start from the beggining with the simpler examples, search the forum first for older threads and if you cannot find one MAKE a new one with a clear Subject so it can be understood.

This is a fact, openFrameworks is in constant development and there are many bugs and functionality issues that have not been addressed. Sometimes things go very wrong. I had asked a similar question to yours in the past:
http://forum.openframeworks.cc/t/get-depth-pixels-from-ofxmskinect-into-ofxopencv-in-007/8047/7
I might have programmed the code wrong but I never resolved that issue and I switched to another addon, sometimes these things happen and you move on. BUT “how do you know you programmed it wrong or you have come across a bug?”, this is where your experience as a programmer is important and it is why I would suggest you try sticking to ofxOpenCv instead of ofxCv since there are many more examples for the former and much more people will be able to guide you.

Now this thread has little to do with your problem thought it may be similar, so I will tell you what I think might be wrong (I’m not an expert as is Arturo and many others). There are more technical explanations for what might be happening to you but here a simple one:
Every frame of the depthImage is made up of a certain amount of information that defines it. In the case of ofxOpenNI I believe the depthImage is made up of 320x240 unsigned chars (I don’t work with ofxOpenNI) and that is enough to define that greyscale image in the computer memory. A color image may be made up of 3x 320x240 unsigned chars, three because of the colors RGB needed for it to be a color image. This may change of course.

Now you have two posible scenarios here:
You have to correctly convert the depth image to something at least similar to the color image (which is inefficient but functional) or you should wait for such functionality to be added to the addon which may or may not happen.
Either way, I suggest you read up on data types and how images are stored in memory because this is what I think makes a difference here.
By the way I have NO idea how MAT in ofxCv works but your problem seems similar to ones I’ve had many times.

Lastly, start simple. ofxOpenNI + ofxOpenCv. Why? Because ofxCv is AWESOME but it is also very big and more complex. I’m not trying to discourage you by the way, I do hope you get everything working. I would have never posted this for you if I didn’t care about this community.

Good luck! :smiley:

Thanks for your suggestions.
I will look at the code and ofxOpenCv. Sometimes the pressure is so high that we forget other things.
I didn’t want to disrespect the forum…Sorry if I did.
I see known that I face problems that others are facing also.
Best regards,

@paulo.trigueros: as I’ve posted here I think your problem is that you are assuming that the depthPixels are grayscale when in fact they are RGBA - so you get a ‘weird’ looking image with several ‘ghosts’ in it…

There is a long discussion about different approaches to creating depth and mask pixels here and I am using the method I describe to generate the mask pixels in ofxOpenNI experimental branch.

@irregular: not sure if your issue with ofxMSKinect was related to this (color -> gray), but…

…I do agree with you about paulo’s problems: I’m finding it hard to give sensible responses because the questions are very specific, and little example code is supplied.

Much more importantly paulo (as I’ve requested several times on the forum and in PM) you need to tell us exactly what you are trying to achieve with your program. That is, tell us the big picture of what you want to achieve, rather than asking straight away about how one line of your code isn’t behaving as you’d expect or like. More often than not the latter is user error, sometimes it’s a bug, but if you want to get more useful help we/I need to know exactly what you want your program to do…

You can either post the description of what you want to achieve in the ofxOpenNI-Development-thread, or start your own thread - something like “ofxOpenNI and hand tracking” - and in that thread post a concise description of what you’d like your program to do.

@paulo.trigueros
Don’t worry. =)

@gameover
I’m not sure either, I’m just guessing. I’m currently in windows working with ofxKinectNui that works with the official Microsoft SDK, although I should definitely switch to ofxOpenNI.
So much to learn! xD