First time here? Check out the FAQ!

Ask Your Question
1

Loading multiple images from file to the same Mat?

asked Nov 7 '18

Heniek45 gravatar image

Hello,

Currently if I want to load two images from two different files I have to do this:

cv::Mat image1 = cv::imread("C:\\image1.png"); 
cv::Mat image2 = cv::imread("C:\\image2.png");

Is it possible to reuse the same image1 object and load second image to it when I've finished with first? Function like this would be handy:

cv::Mat image1 = cv::imread("C:\\image1.png"); 
cv::imread(image1, "C:\\image2.png");
Preview: (hide)

1 answer

Sort by » oldest newest most voted
1

answered Nov 7 '18

berak gravatar image

simply do it like:

cv::Mat image = cv::imread("C:\\image1.png");
// do something with image, then recycle it: 
image = cv::imread("C:\\image2.png");
Preview: (hide)

Comments

Thank you. I don't know opencv well, but isn't this causes a new memory allocation and release of old memory?

Heniek45 gravatar imageHeniek45 (Nov 7 '18)edit

yes it does, but it's imread() already, where the allocation happens, and you can't avoid it.

and you should not worry too much about allocating memory, nowadays, that's cheap. the number crunching is, where things get interesting...

berak gravatar imageberak (Nov 7 '18)edit
1

The thing is that when I am processing images in realtime from camera 24/7 it adds up. It would be good to avoid this if it is not necessary. Since images always come with the same size it would be nice to have an overload like I've shown and to reuse already allocated buffer. But I guess I will have to live with it, thanks.

Heniek45 gravatar imageHeniek45 (Nov 7 '18)edit

why would it add up ? reassigning a Mat means freeing the old memory first.

berak gravatar imageberak (Nov 7 '18)edit

Question Tools

1 follower

Stats

Asked: Nov 7 '18

Seen: 605 times

Last updated: Nov 07 '18