functions

I have been using the openCV library for some code I have been working on.

When I create functions I prefer to use void functions and I use them whenever possible.

However I have been unable to successfully create a void function whenever the destination image does not already exist. Here is a quick example

for those not familiar with opencv, IplImage* is the type of an image
and cvCreateImage is used to create a blank image..

This works
1
2
3
4
5
6
7
8
9
IplImage* imagething(IplImage* src)
{
  CvSize s;
     s.width=100;
     s.height=100;
  IplImage* dst = cvCreateImage(s,8,1)
  cvResize(src,dst)
  return dst;
}

This doesn't
1
2
3
4
5
6
7
8
9
void imagething(IplImage* src, IplImage* dst)

{
  CvSize s;
     s.width=100;
     s.height=100;
  dst = cvCreateImage(s,8,1)
  cvResize(src,dst)
}


what can I do differently to make the void function work?
Pass in a reference to the pointer. At the moment, you're passing the pointer by value, which means that when you change that pointer here:

dst = cvCreateImage(s,8,1)

you're changing a local copy of the pointer, which gets destroyed when the function ends, and the original pointer is untouched.
I'm new to OpenCV, I had to look up the documentation on these function to even know what they were.

Would something like

*dst = *(cvCreateImage(s,8,1))

Work?
You can either use a pointer to pointer or a reference to pointer.
For example

void imagething( IplImage *src, IplImage **dst )
{
CvSize s;

s.width =100;
s.height =100;

*dst = cvCreateImage(s,8,1)
cvResize( src, *dst )
}
Last edited on
Topic archived. No new replies allowed.