OpenCv / imageData

hey guys,

can anybody help me to get pixels out of IplImages or ofImages after running an algorithm like cvCanny oder cvThreshhold. i dont mean getPixels or something like that. i want to get them pixel by pixel like with the imageData pointer to create objects with the coords of the pixels:

for(int row = 0; row < silh->height;row+=2,pixelNr++){  
	uchar* ptr = (uchar*)(silh->imageData + row * silh->widthStep);  
	for(int column = 0; column < silh->width;column+=2,pixelNr++){  
		char pixel = ptr[column];  
			particle[particleCount++] = new Particle(column,row);  

the problem is that after running cvCanny or cvThreshold the imageData is NULL.
does anybody know where openCv store the results to use/draw them later?

thanks & greetz

Could you post a full example, including a call to cvCanny?


Here is a piece of code working for me to acces pixel comming from imageData of a ofImage (with 3 channels (RGB) ) :

To use it with ofCvImage i just add:

IplImage *img=cvCloneImage(colorImg.getCvImage());
IplImage *mask=cvCloneImage(colorImg.getCvImage());

//----Pixel Acces-------

unsigned char* pteur=(unsigned char*)img->imageData;//pointer to img pixels

int end=img->width*img->height;

for(int i=0;i<end;i++)

//here you can retrieve imageData values in values[0],[1],[2]
int values[3]={*(pteur++),*(pteur++),*(pteur++)};

//or you can change there like this

//and the pixels coordonate should be


I don’t know if it could help…

It might help a bit?

I still don’t see a call to cvCanny, so I’m not sure why that won’t work.

Something that might be a problem is that you’re making a copy of the original image? If you clone it, and then change the clone, if you try and access the original there won’t be any change.


i tried a lot (also studied the internal opencv code for threshold and canny) but the only hack which works for me is to use the contourfinder on my cvcanny image and then take the blob-points. but i think this slows all down. i did not find where canny or other algorithm store the data.

i have no more examples ( because of cleaning up ) but i just tried thinks like this:

//OpenCV color => gray  

after this imageData is empty and the code a posted first does not work.

in cvThreshold OpenCv cast the IplImage to CvMat. i also tried this, but without success.

cloning the img after cvcanny make no difference and cloning before is useless like mcdonald explained.

Yeah, it’s still hard to tell without a complete chunk of code as there are a ton of things that could be going on. Maybe you’re not allocating space, maybe you’re not iterating through the IplImage correctly, maybe the if/else should be sequential rather than being separated into two parts of an if statement… I can’t really tell unless you post an complete chunk of code that “doesn’t work”.

hey kyle,

i dont think there is something going on wrong. it is the internal opencv behavior. after alogirthm like cvcanny the destination image imageData is empty. try it out yourself. you can see that in debug mode(vs 2008) or not?

the image information must be stored else where. because my images are not corrupted. i can print them and work with them after cvcanny. i only can not use them pixel by pixel or there blobs(like with contours) because i dont know where to find the data.

I was asking for an example exactly because I haven’t run into this problem :slight_smile: Maybe if I share a snippet instead, you’ll notice something different from the way you’ve been doing it?

// Load an image from a file.  
ofImage ofimg;  
// Create and allocate images for cvCanny.  
ofxCvGrayscaleImage cvimg;  
ofxCvGrayscaleImage edges;  
cvimg.allocate(ofimg.width, ofimg.height);  
edges.allocate(ofimg.width, ofimg.height);  
// Run cvCanny.  
  cvimg.getCvImage(), // input image  
  edges.getCvImage(), // edges output  
  aperture); // default = 3  
// Let ofxOpenCv know that the internal texture  
// needs to be reloaded before drawing it to the screen.  
// This does not modify the IplImage.  

Let me know if this helps.

thanks for your help kyle

but im sorry. thats what im talking about. i don’t need flagImageChanged. i don’t use ofxOpenCv here. just OpenCv and all runs fine. can use cvcanny, can draw the canny image. just no data where im searching for it.

you are right. i have to make an example with screenshots in debugging mode. have to rebuild the example first but not until next week. i’ll post it here.


I have the same problem:

After I call cvCanny or cvThreshold the imageData is NULL.

But the problem is not with my code becouse:

(1) When I use the same code in another PC everything runs OK.

(2) When I use the exe in another PC everything runs OK too.

(3) The Opencv Exemple that uses cvThreshold doesn’t work in my PC but works in others.

(4) cvThreshold Programs that works in others PCs doesn´t work in my PC.

All PCs use windows XP (same version) and the same version of visual studio.

The only difference is the processor. The bad OpenCV runs on a Phenom Qud-Core. (Mult Core - is it the reason?)

Explain better

I have the same problem.

OpenCV Binarization problem (cvThreshold, cvCanny…)

I installed OpenCV 2.3 in my new Phenom Quad-Core, Windows XP, to work on Visual Studio 2005 framework.

All OpenCV examples, that use binaryzation functions (cvThreshold, cvCanny…) and my own applications, that have already work correctly in others computers, start failing in my Phenom.

The problem is: after I call cvThreshold function the result image is NULL.
This problem occurs in design mode and running the exe file too.

My code is OK because if I run it in other computer, everything works well. cvThreshold Applications that run well in others computers doesn’t work in my PC (design and executable).

Applications that DON’T use cvThreshold and cvCanny…works correctly in my PC.

So, I believe it is a OpenCV comparing values problem (cvThreshold) in my PC configuration. The others functions works well, only cvThreshold , cvCanny
(binaryzation functions) don’t work.

I have a notebook where cvThreshold applications work well. This way I can test the same code and the same executable.



Wow, this is way deeper than I realized. If it works on one computer, but not another, that’s an issue with OpenCV rather than open frameworks. You might want to try running a different version of OpenCV, or asking on the OpenCV Yahoo group