Ask Your Question
0

Why is imread slowing down in a for loop?

asked 2018-01-22 04:58:40 -0600

lemmy2018 gravatar image

Hi!

I have about 3500 tiff files (unsigned 16bit, 2048 x 2048) stored in a directory. I wrote a c++ program to read the files using the imread function in a for loop. When I compile and run the program I am finding that the for loop runs nice and fast up to a certain point and then suddenly slows down to a bit of a crawl.

To be more specific, I declare a cv::Mat object before the loop and the Mat is just reallocated during each iteration. In other words I can't see how it could be a memory issue (I don't see my RAM usage increasing during run time).

Here is what the offending code looks like:

std::vector<cv::String> filenames;
cv::glob(path, filenames);
cv::Mat im;

for (size_t i = 1; i<(filenames.size()); i++)
{
    im = imread(filenames[i], CV_LOAD_IMAGE_ANYDEPTH);      
    std::cout << i << std::endl;
}

I am using openCV 3.4 and Visual Studio 2017 in release mode.

Can anybody help?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
2

answered 2018-01-24 06:52:57 -0600

lemmy2018 gravatar image

Hi. Thanks for your answer! I think I found what was happening - the hard disk was caching images that had already been loaded before in a previous execution of the program and so the for loop was running super quick for those images and then slowing down suddenly when it hit new images. All normal behavior I suppose...

edit flag offensive delete link more
1

answered 2018-01-23 03:50:24 -0600

LBerger gravatar image

I haven't got any problem (vs2017 -opencv 3.4-dev) using this program :

#include <opencv2/opencv.hpp> 
#include <iostream>

using namespace cv;
using namespace std;

#define NBCAMERA 5

int main (int argc,char **argv)
{
    Mat img1(2048, 2048, CV_16UC1);
    RNG r;
    Mat img2(2048, 2048, CV_16UC1, Scalar(1058));
    r.fill(img1, RNG::UNIFORM, 0, 32000);
    r.fill(img2, RNG::UNIFORM, 0, 32000);
    imwrite("test1.tif", img1);
    imwrite("test2.tif", img2);
    std::vector<cv::String> filenames{ "test1.tif","test2.tif" };
    cv::Mat im;
    TickMeter t;
    t.reset();
    t.start();
    for (size_t i = 0; i<3500; i++)
    {
        im = imread(filenames[i%2], CV_LOAD_IMAGE_ANYDEPTH);
        if (i>0 && i % 100 == 0)
        {
            t.stop();
            std::cout << "Ellapsed Time  "<< i<<" -> "<<t.getTimeMilli()<< std::endl;
            t.reset();
            t.start();
        }
    }return 0;
}

results are

Ellapsed Time  100 -> 4947.4
Ellapsed Time  200 -> 4875.03
Ellapsed Time  300 -> 4823
Ellapsed Time  400 -> 4814.94
Ellapsed Time  500 -> 4768.13
Ellapsed Time  600 -> 4818.58
Ellapsed Time  700 -> 4759.5
Ellapsed Time  800 -> 4605.62
Ellapsed Time  900 -> 4641.3
Ellapsed Time  1000 -> 4631.22
Ellapsed Time  1100 -> 4650.53
Ellapsed Time  1200 -> 4633.75
Ellapsed Time  1300 -> 4649.75
Ellapsed Time  1400 -> 4681.7
Ellapsed Time  1500 -> 4647.45
Ellapsed Time  1600 -> 4663.46
Ellapsed Time  1700 -> 4642.76
Ellapsed Time  1800 -> 4634.68
Ellapsed Time  1900 -> 4502.76
Ellapsed Time  2000 -> 4539.41
Ellapsed Time  2100 -> 4735.48
Ellapsed Time  2200 -> 4743.72
Ellapsed Time  2300 -> 4708.38
Ellapsed Time  2400 -> 4735.22
Ellapsed Time  2500 -> 4717.13
Ellapsed Time  2600 -> 4726.73
Ellapsed Time  2700 -> 4785.9
Ellapsed Time  2800 -> 4685.31
Ellapsed Time  2900 -> 4674.4
Ellapsed Time  3000 -> 4732.16
Ellapsed Time  3100 -> 4682.91
Ellapsed Time  3200 -> 4843.31
Ellapsed Time  3300 -> 4730.63
Ellapsed Time  3400 -> 4674.49
Appuyez sur une touche pour continuer...
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-01-22 04:58:40 -0600

Seen: 3,728 times

Last updated: Jan 24 '18