OpenCV 使用 waitKey() 函数处理箭头键



我想处理箭头键。 但是当我打印出waitKey((函数的输入值时,它是0。 我不知道为什么。 我尝试从"int"更改为"char",但它不起作用。 我该如何解决这个问题。

int pos = 100;
imshow("image", image);
onChange(pos, (void *)&image);
createTrackbar("threshold", "image", &pos, 255, onChange, (void*)&image);
while (1) {
int Key = waitKey();
cout << Key << endl;
if (Key == 27) break;
if (Key == 2490368) {
pos--;
onChange(pos, (void *)&image);
}
if(Key == 2621440){
pos++;
onChange(pos, (void *)&image);
}
if (pos < 0 || pos > 255) pos = 0;
}

改用waitKeyEx()函数。正如文档所说:

与waitKey((类似,但返回完整的密钥代码。

关键代码是特定于实现的,取决于使用的后端: QT/GTK/Win32

在我的系统上,它提供: 左:2424832 向上: 2490368 右:2555904 下: 2621440

尽管有许多在线资源说waitKey()可以使用箭头,但它也没有在我的 Windows 系统上返回正确的键代码(始终返回 0(。猜测这也是特定于实现的。也许是因为waitKey()返回 ASCII 代码,但箭头键没有它们(如此处所述(。

请注意,这取决于版本,在具有 3.0 的 Windows 上,waitKey(( 给出了完整的键代码,但是当我更改为 3.3 时,它突然为箭头键返回 0。

以下是我的临时解决方法

#ifndef _WIN32
int myWaitKey(int wait) {
int c = cv::waitKey(wait);
return c;
}
#pragma message("linux..")
#else
#include <conio.h> // to support _getch
int myWaitKey(int wait) {
int c = cvWaitKey(wait);
if (c == 0) {
int c = _getch();   //capture the key code and insert into c
if (c == 0 || c == 224)
c = _getch();
}
//if (c!=-1) printf("%dn",c);
return c;
}
#endif

在代码中使用 waitkey((,按住 cv2.trackbar 图标上的左键并使用箭头键以 1 为增量移动

最新更新