Ask Your Question
0

How to show every image in every loop

asked 2017-11-17 12:56:16 -0600

yode gravatar image

updated 2017-11-17 12:56:45 -0600

I have such code

#include <opencv.hpp>
#include <filesystem>
using namespace cv;
using namespace std;

int main() {
    for (auto mark_name : experimental::filesystem::directory_iterator("E:/test")) {
        string filename = mark_name.path().string();
        Mat testImg = imread(filename.c_str(), 0);
        //Some steps about image-processing
        imshow(filename.c_str(), testImg);
    }
    waitKey();
    return 0;
    }

Suppose I have 10 or more images in my driectory E:/test. I found all image have to show noramlly after all evaluation is completed, before that all window is gray status like this

I want the imshow(filename.c_str(), testImg); work to show the image directly in every loop, is possible?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
2

answered 2017-11-17 13:19:51 -0600

berak gravatar image

updated 2017-11-17 13:20:36 -0600

pretty simple:

move the waitKey() call inside the loop. (if you do not want to "wait forever", you also want to add a timestep in milliseconds, like waitKey(500); // half a second

(imshow() just copies a pointer to the image, the actual drawing/blitting (whatever you might call it) is triggered from waitKey(). think of it like this: any gui prog needs a message loop, and waitKey() is implementing exactly that)

((it's doing far more than waiting for a keypress, which pretty regularily confuses noobs))

edit flag offensive delete link more

Comments

ohh, btw, imshow(filename.c_str(), testImg); -- will open a new window each time. for sure a bad idea

berak gravatar imageberak ( 2017-11-17 13:25:31 -0600 )edit

let me just add -- this is the most common noob pifall ever.

if you would have used the search button here, you'd never asked, in the 1st place..

berak gravatar imageberak ( 2017-11-17 13:40:56 -0600 )edit

let me be honest with you here -- ill-researched questions like this one are actually driving anyone away from helping out. (you do that 4 times, you do that 157 times, then you just get desparate about noobs in general)

berak gravatar imageberak ( 2017-11-17 13:55:02 -0600 )edit

You mean waitKey() doing far more than waiting for a keypress? It push a message to the loop??

yode gravatar imageyode ( 2017-11-17 14:17:23 -0600 )edit

Thanks your lesson very much, actually the reason is my poor English result to I often don't know how to search my question I encoutered in English. I have find your answer here just. If have understood it rightly, waitKey(1); will send a enter into the loop to have the imshow..

yode gravatar imageyode ( 2017-11-17 14:29:01 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-11-17 12:56:16 -0600

Seen: 6,327 times

Last updated: Nov 17 '17