c++ - OpenCV waitKey() 函数在 mac 上总是返回 255

标签 c++ opencv

  1. 我在 Mac(版本 10.12.2)上测试了 OpenCV waitKey() 函数,但它 < b>似乎没有正常工作。无论是否按下任何键,它总是返回 255!我想知道它是安装不正确 还是 OpenCV 本身的错误。任何修复它的想法将不胜感激。
  2. 这是我的程序的源代码:

    #include "opencv2/highgui/highgui.hpp"
    #include <iostream>
    
    int main() {
        int ch;
        cv::namedWindow("Test waitKey()", cv::WINDOW_AUTOSIZE);
        cv::Mat image = cv::Mat(100,100,CV_8U, cv::Scalar(0));
        cv::imshow("Test waitKey()", image);
    
        for (;;){
            ch = cv::waitKey(1000);
            std::cout << ch << std::endl;  // always return 255
    
            // and this if statement will never be executed!
            if (ch == 'q')    break;
        }
    
        return 0;
    }
    
  3. 这里是 cv::getBuildInformation() 函数的输出:

    General configuration for OpenCV 3.2.0 
    =====================================
    Version control:               unknown
    
    Platform:
    Timestamp:                   2017-01-14T14:50:25Z
    Host:                        Darwin 16.3.0 x86_64
    CMake:                       3.7.2
    CMake generator:             Unix Makefiles
    CMake build tool:            /usr/bin/make
    Configuration:               RELEASE
    
    C/C++:
    Built as dynamic libs?:      YES
    C++ Compiler:                /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++  (ver 8.0.0.8000042)
    C++ flags (Release):         -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -fno-omit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
    C++ flags (Debug):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -fno-omit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
    C Compiler:                  /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
    C flags (Release):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -fno-omit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
    C flags (Debug):             -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -fno-omit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
    Linker flags (Release):
    Linker flags (Debug):
    ccache:                      NO
    Precompiled headers:         NO
    Extra dependencies:          -framework OpenCL -framework Cocoa -framework AVFoundation -framework CoreGraphics -framework CoreMedia -framework CoreVideo -framework QuartzCore -framework Accelerate
    3rdparty dependencies:       libjpeg libwebp libpng libtiff libjasper IlmImf zlib
    
    OpenCV modules:
    To be built:                 core flann imgproc ml photo video imgcodecs shape videoio highgui objdetect superres ts features2d calib3d stitching videostab python2
    Disabled:                    world
    Disabled by dependency:      -
    Unavailable:                 cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev java python3 viz
    
    GUI: 
    QT:                          NO
    Cocoa:                       YES
    OpenGL support:              NO
    VTK support:                 NO
    
    Media I/O: 
    ZLib:                        build (ver 1.2.8)
    JPEG:                        build (ver 90)
    WEBP:                        build (ver 0.3.1)
    PNG:                         build (ver 1.6.24)
    TIFF:                        build (ver 42 - 4.0.2)
    JPEG 2000:                   build (ver 1.900.1)
    OpenEXR:                     build (ver 1.7.1)
    GDAL:                        NO
    GDCM:                        NO
    
    Video I/O:
    DC1394 1.x:                  NO
    DC1394 2.x:                  NO
    FFMPEG:                      NO
      avcodec:                   NO
      avformat:                  NO
      avutil:                    NO
      swscale:                   NO
      avresample:                NO
    GStreamer:                   NO
    OpenNI:                      NO
    OpenNI PrimeSensor Modules:  NO
    OpenNI2:                     NO
    PvAPI:                       NO
    GigEVisionSDK:               NO
    Aravis SDK:                  NO
    AVFoundation:                YES
    V4L/V4L2:                    NO/NO
    XIMEA:                       NO
    gPhoto2:                     NO
    
    Parallel framework:            GCD
    
    Other third-party libraries:
    Use IPP:                     9.0.1 [9.0.1]
         at:                     /Users/ngoclinhng/opencv/release/3rdparty/ippicv/ippicv_osx
    Use IPP Async:               NO
    Use VA:                      NO
    Use Intel VA-API/OpenCL:     NO
    Use Lapack:                  YES (-framework Accelerate)
    Use Eigen:                   NO
    Use Cuda:                    NO
    Use OpenCL:                  YES
    Use OpenVX:                  NO
    Use custom HAL:              NO
    
    OpenCL:                        <Link with OpenCL library>
    Link libraries:              -framework OpenCL
    Use AMDFFT:                  NO
    Use AMDBLAS:                 NO
    
    Python 2:
    Interpreter:                 /usr/bin/python2.7 (ver 2.7.10)
    Libraries:                   /usr/lib/libpython2.7.dylib (ver 2.7.10)
    numpy:                       /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/include (ver 1.8.0rc1)
    packages path:               lib/python2.7/site-packages
    
    Python 3:
    Interpreter:                 /Users/ngoclinhng/anaconda/bin/python3 (ver 3.5.2)
    
    Python (for build):            /usr/bin/python2.7
    
    Java:
    ant:                         NO
    JNI:                         /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/System/Library/Frameworks/JavaVM.framework/Headers /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/System/Library/Frameworks/JavaVM.framework/Headers /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/System/Library/Frameworks/JavaVM.framework/Headers
    Java wrappers:               NO
    Java tests:                  NO
    
    Matlab:                        Matlab not found or implicitly disabled
    
    Documentation:
    Doxygen:                     NO
    
    Tests and samples:
    Tests:                       YES
    Performance tests:           YES
    C/C++ Examples:              NO
    
    Install path:                  /usr/local
    
    cvconfig.h is in:              /Users/ngoclinhng/opencv/release
    -----------------------------------------------------------------
    

最佳答案

你应该改用cvWaitKey(n)~ 在 OpenCV 3.1 之后,当在 n 毫秒内没有按键按下时,cv::waitKey(n) 的返回值从 -1 变为 255。

关于c++ - OpenCV waitKey() 函数在 mac 上总是返回 255,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41750171/

相关文章:

除非在特定目录中启动,否则 python 不会加载模块

python - 如何在 OpenCV 中模板匹配简单的 2D 形状?

C++:如何在 for 循环中排除对称对

c++ - Cocos2dx v.3.0 人像模式

c++ - 匿名命名空间

c++ - 将OpenCV::Mat作为BLOB存储到SQLite DB

image-processing - 相机位置和角度与3d数据的两个图像?

android - OpenCV 示例 - 颜色 Blob 检测

c++ - 将 C++ 中的文件读取到字符串数组中,不断重复最后一个单词

c++ - c++对象中静态变量的默认初始化值是多少?