简体   繁体   中英

How to assign a filename with a pointer one?

I do have a function that I have created with a pointer as an argument. That argument will be used to save at the end an image. Every time when I would like to call that function with the desired parameter, I want that the saved image file would be with the specified name. I show you how:

void SaveImage(IplImage *img)
{
...
cvSaveImage("C:/img.png", img);
...
}

when calling the function: SaveImage(image1), I want to have an image on my C:/ whose name is image.png

Can you help me with that?

Apparently, you can't answer this question with the only answer that is actually viable, because you get downvoted...

So, I'll rephrase my answer:

You need to pass two variables to SaveImage:

void SaveImage(const char *name, IplImage *img)
{
   ...
   cvSameImage(name, img);
}

Then your calling code will have to produce the correct name, such as:

SaveImage("c:\image.png", image);

SaveImage("c:\other.png", other);

Now, of course, if all you actually want is a unique name, rather than one that reflects the name of the variable in your code, then there are plenty of other possibilities, such as using a formatted string that contains a serial number, a random number, tmpnam() or other similar schemes.

As I understand, you want save the file with the name of passed object.

For example, after SaveImage(myImage); it should save the image with name myImage.png , am I right? Well. It's impossible since C++ hasn't such a reflection to retrieve object's name. You should try some dirty jobs, using MACROs to stringify the object.

BUT, If you want save images in different names, you can use a static variable:

void SaveImage(IplImage *img)
{
...
static int num = 1;
cvSaveImage("C:/image"+ to_string(num++) +".png", img);
...
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM