How do variable decclarations pertaining to struct members work in OpenCV C interface

asked 2013-11-09 21:12:21 -0600

joeish80829 gravatar image

This is the code it uses imageData to set all the pixels in FRAME to red... i could use help understand it though so i can get a better view of opencv and c in general Note : need to use the c interface for project im working on

In the code below it seems FRAME->imageData is added to y multiplied by FRAME->WidthsTEP inthe top for loop and then PTR is altered in the bottom for loop ie ' ptr[3*x+2] = 255;' and that changes the image FRAME to all red but imageData is set to PTR not the other way around so how can changing PTR even effect FRAME->imageData. because when i do this

int a=1; int b=a; b=4;

cout << "a = " << endl << " " << a << endl << endl; // A ends up equaling 1

so it doesnt seem this should happen

#include "cv.h"  
#include "highgui.h"  
using namespace std;

int main (int argc, const char * argv[]) {

IplImage *frame = cvCreateImage(cvSize(41, 41), IPL_DEPTH_8U, 3);

for( int y=0; y<frame->height; y++ ) { 
    uchar* ptr = (uchar*) ( frame->imageData + y * frame->widthStep );

cout << "M = " << endl << " " << frame->imageData+ frame->widthStep << endl << endl; for( int x=0; x<frame->width; x++ ) { ptr[3*x+2] = 255; //Set red to max (BGR format) } }

cvNamedWindow("window", CV_WINDOW_AUTOSIZE);
cvShowImage("window", frame);
cvWaitKey(0);
cvReleaseImage(&frame);
cvDestroyWindow("window");
return 0;

}

edit retag flag offensive close merge delete

Comments

You should really read a C/C++ chapter about pointers / references...

Guanta gravatar imageGuanta ( 2013-11-10 09:55:56 -0600 )edit