3

I am making Langton's Ant in C++, when I try to draw squares, I can. But I can't make this in loop.

for(int i = 0;i<=100;i++){
        rectangle( image, Point( i*5, 0 ), Point( (i*5)+5, 5), Scalar( 0, 55, 255 ), CV_FILLED, 4 );
        imshow("kare",image);
        Sleep(100);
    }

It waits for 10 seconds, then draw all the squares same time. If I add cvWaitKey(0); before sleep, I get same problem. When I "touch" the key, it draw, but when I hold, it doesn't draw. When I back off my finger, it draw.

How can I solve it? Regards.

Atakan Erbaş
  • 51
  • 1
  • 3
  • Please post the whole source code. `Sleep` can be pretty much anything, as you don't show us your includes. It's a rather common name (and not part of the obvious candidate OpenCV). Why you'd call `cvWaitKey` (or `waitKey`) before doing a Sleep also seems redundant, so what the function does or where it comes from is even more unclear... – Creat Jun 02 '14 at 15:10

1 Answers1

6

You are mixing C and C++ API, cvWaitKey(0) belongs to deprecated C. Also cvWaitKey(0) waits until user press key.

So just use

waitKey(33) instead of sleep(), which will wait 33 ms after each imshow().

Haris
  • 13,645
  • 12
  • 90
  • 121
  • Thanks. But why Sleep makes problem? – Atakan Erbaş Jun 02 '14 at 15:18
  • Sleep doesn’t work for imshow(), you should use waitKey() for HighGui operation like imshow(). Also these links http://docs.opencv.org/modules/highgui/doc/user_interface.html#waitkey http://stackoverflow.com/questions/5217519/opencv-cvwaitkey – Haris Jun 02 '14 at 16:17
  • How can I make it faster? When I write `waitKey(10)`, I can't get 100 square in second. Is there any way? – Atakan Erbaş Jun 02 '14 at 19:27
  • Use minimum delay, but the speed depends on precessing time between two imshow(). Also the Docs says, since the OS has a minimum time between switching threads, the function will not wait exactly delay ms, it will wait at least delay ms, depending on what else is running on your computer at that time. – Haris Jun 04 '14 at 14:48