I have a const std::vector<cv::Mat> containing 3 matrices (3 images), and in order to use each image further in my program I need to save them in separate matrices cv::Mat.
I know that I need to iterate over vector elements, since this vector is a list of matrices but somehow I can't manage it. At the end, I also need to push 3 matrices back to a vector.
I would appreciate if someone could help me out with this. I am still learning it.
std::vector<cv::Mat> imagesRGB;
cv::Mat imgR, imgG, imgB;
for(size_t i=0; i<imagesRGB.size(); i++)
{
   imagesRGB[i].copyTo(imgR);
}
				
                        
In your code, note that
imagesRGBis uninitialized, and its size is0. Theforloop is not evaluated. Additionally, thecopyTomethod copies matrix data into another matrix (like a paste function), it is not used to store acv::Matinto astd::vector.Your description is not clear enough, however here's an example of what I think you might be needing. If you want to split an
RGB(3 channels) image into three individual mats, you can do it using thecv::splitfunction. If you want to merge 3 individual channels into anRGBmat, you can do that via thecv::mergefunction. Let's see the example using this test image:Note that I'm already storing the channels in an
array. However, if you want to store the individual mats into astd::vector, you can use thepush_backmethod:This is the result: