【问题标题】:opencv handling arrow keys with waitKey() functionopencv 使用 waitKey() 函数处理箭头键
【发布时间】:2018-01-16 18:29:40
【问题描述】:

我想处理箭头键。但是当我打印出 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;
}

【问题讨论】:

    标签: opencv keyboard


    【解决方案1】:

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

    类似于waitKey(),但返回完整的键码。

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

    在我的系统上它给出: 左:2424832 上:2490368 右:2555904 下:2621440

    尽管有许多在线消息来源说waitKey() 可以使用箭头,但它在我的 Windows 系统上也没有返回正确的键码(总是返回 0)。猜猜这也是特定于实现的。可能是因为 waitKey() 返回 ASCII 码,但箭头键没有它们(如 here 解释的那样)。

    【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      以下是我的临时解决方法

      #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("%d\n",c);
          return c;
      }
      #endif
      

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多