danieltakeshi danieltakeshi - 10 months ago 62
C++ Question

Using a loop to declare a Mat class

The camera stores images to a FTP server with the filename pattern as "folder"+"program_name"+ index +"format", where index is a number from one to almost ten billion (0000000001 to 9999999999). The path and filename are needed to display the images. It is required to show 10 images on OpenCV, so I tried to make the following testcode using the function ShowManyImages.

int main() {
int img_ini = 1;
std::string img_path = "../data/";
std::string img_name = "img";
std::string img_format = ".bmp";
std::string var;
var = img_path + img_name + std::to_string(img_ini) + img_format;

Mat img1 = imread(var);
Mat img2 = imread("../data/img2.bmp");
Mat img3 = imread("../data/img3.bmp");
Mat img4 = imread("../data/img4.bmp");
Mat img5 = imread("../data/img5.bmp");
Mat img6 = imread("../data/img6.bmp");
Mat img7 = imread("../data/img6.bmp");
Mat img8 = imread("../data/img6.bmp");
Mat img9 = imread("../data/img6.bmp");
Mat img10 = imread("../data/img6.bmp");
ShowManyImages("Image", 10, img1, img2, img3, img4, img5, img6, img7, img8, img9, img10);

return 0;
}


To this point i could show the image 1 creating the
var
variable. However, this code is a mess and i didn't want to create
img1,img2, ..., img10
.

So i tried to make something similar to what was done with
var
to the Mat img, creating a loop like For i=1 To 10
Mat img + i = imread(img_path + img_name + std::to_string(img_ini + i) + img_format;)
. But wasn't able, because Mat () isn't as imread ().


  • Is there a way to make something like the for loop described?

  • Or is it better to create a void function like:

    Mat img1 = imread(var(1));
    Mat img2 = imread(var(2));
    Mat img3 = imread(var(3));
    Mat img4 = imread(var(4));
    Mat img5 = imread(var(5));
    Mat img6 = imread(var(6));
    Mat img7 = imread(var(7));
    Mat img8 = imread(var(8));
    Mat img9 = imread(var(9));
    Mat img10 = imread(var(10));

  • Or is there another/better way?


Answer Source

Try using a vector of Mats. There are a few ways to load them and in show them.

std::vector<cv::Mat> myMatArray;
for(int i = 0; i < numberOfPicturesToLoad; i++){
   std::string imgToLoad = img_path + img_name + std::to_string(i) + img_format;
   cv::Mat temp = imread(imageToLoad);
   myMatArray.push_back(temp);
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download