opencv读取摄像头

版权声明:学无止境,好好学习 https://blog.csdn.net/m0_38116269/article/details/88767135

这个不错:
https://www.cnblogs.com/little-monkey/p/7162340.html

出错误了:

terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(3.4.2) /tmp/build/80754af9/opencv-suite_1535558553474/work/modules/highgui/src/window.cpp:615: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvNamedWindow'

Aborted (core dumped)

参考:https://blog.csdn.net/a694262054/article/details/86376002

直接用conda便捷安装的opencv无法实现视频和图像的读取,需要自己手动下载编译才可以。
自己动手:
https://blog.csdn.net/aaon22357/article/details/81913465

原因分析:由于anaconda安装的是阉割版的opencv,无法读取摄像头,因此需要自己源码编译opencv。经过下一篇博客的尝试,成功了。
代码如下,

#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/videoio.hpp"
#include <iostream>

using namespace cv;
using namespace std;

void drawText(Mat & image);

int main()
{
    cout << "Built with OpenCV " << CV_VERSION << endl;
    Mat image;
    VideoCapture capture;
    capture.open(0);
    if(capture.isOpened())
    {
        cout << "Capture is opened" << endl;
        for(;;)
        {
            capture >> image;
            if(image.empty())
                break;
            drawText(image);
            imshow("Sample", image);
            if(waitKey(10) >= 0)
                break;
        }
    }
    else
    {
        cout << "No capture" << endl;
        image = Mat::zeros(480, 640, CV_8UC1);
        drawText(image);
        imshow("Sample", image);
        waitKey(0);
    }
    return 0;
}

void drawText(Mat & image)
{
    putText(image, "Hello OpenCV",
            Point(20, 50),
            FONT_HERSHEY_COMPLEX, 1, // font face and scale
            Scalar(255, 255, 255), // white
            1, LINE_AA); // line thickness and type
}

对应的,CMakeLists.txt

# cmake needs this line
cmake_minimum_required(VERSION 3.1)

# Enable C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

# Define project name
project(opencv_example_project)

#set(CMAKE_PREFIX_PATH "/usr/local/share/opencv4")

#set my opencv4.0 path

#-------------not use tem----------------------------------------------------------------------
#set(OPENCV_DIR "/home/wwh/Downloads/opencv-4.0.1/build")
#set(OpenCV_INCLUDE_DIRS "/home/wwh/Downloads/opencv-4.0.1/build" "/home/wwh/Downloads/opencv-4.0.1/include" "/home/wwh/Downloads/opencv-4.0.1/modules/core/include" "/home/wwh/Downloads/opencv-4.0.1/modules/flann/include" "/home/wwh/Downloads/opencv-4.0.1/modules/imgproc/include" "/home/wwh/Downloads/opencv-4.0.1/modules/ml/include" "/home/wwh/Downloads/opencv-4.0.1/modules/photo/include" "/home/wwh/Downloads/opencv-4.0.1/modules/dnn/include" "/home/wwh/Downloads/opencv-4.0.1/modules/gapi/include" "/home/wwh/Downloads/opencv-4.0.1/modules/imgcodecs/include" "/home/wwh/Downloads/opencv-4.0.1/modules/videoio/include" "/home/wwh/Downloads/opencv-4.0.1/modules/highgui/include" "/home/wwh/Downloads/opencv-4.0.1/modules/ts/include" "/home/wwh/Downloads/opencv-4.0.1/modules/features2d/include" "/home/wwh/Downloads/opencv-4.0.1/modules/calib3d/include" "/home/wwh/Downloads/opencv-4.0.1/modules/objdetect/include" "/home/wwh/Downloads/opencv-4.0.1/modules/stitching/include" "/home/wwh/Downloads/opencv-4.0.1/modules/video/include")
#----------------------------------------------------------------------------------------------

# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI

find_package(OpenCV REQUIRED)

# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS "    config: ${OpenCV_DIR}")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")

# Declare the executable target built from your sources
add_executable(opencv_example example.cpp)

# Link your application with OpenCV libraries
target_link_libraries(opencv_example ${OpenCV_LIBS})

效果:
不展示了,开个摄像头没啥好看的。

猜你喜欢

转载自blog.csdn.net/m0_38116269/article/details/88767135