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
imagesRGB
is uninitialized, and its size is0
. Thefor
loop is not evaluated. Additionally, thecopyTo
method copies matrix data into another matrix (like a paste function), it is not used to store acv::Mat
into 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::split
function. If you want to merge 3 individual channels into anRGB
mat, you can do that via thecv::merge
function. 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_back
method:This is the result: