Mac 安装 OpenCV,并配置 Clion

  • Mac下更换Homebrew镜像源 - CSDN
  1. 使用 tuna 清华源
cd "$(brew --repo)"
git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
cd 
brew update
  1. brew 安装 OpenCV
brew update
brew install opencv
  1. Clion 配置 CMakeLists.txt
cmake_minimum_required(VERSION 3.12)
project(Video)

# 添加OpenCV库
find_package(OpenCV)
include_directories(${OpenCV_INCLUDE_DIRS})

set(CMAKE_CXX_STANDARD 14)
set(SOURCE_FILES main.cpp)

add_executable(Video ${SOURCE_FILES})
target_link_libraries(Video ${OpenCV_LIBS}) # 链接库
  1. 实例 Video
#include 
#include 
#include 
#include 
#include 

using namespace std;
using namespace cv;

#define WIDTH 640
#define HEIGHT 480
#define FPS 10

int main() {

    VideoCapture capture(0); // 0表示笔记本自带摄像头,1表示外接

    if (!capture.isOpened()) return -1;

    capture.set(CV_CAP_PROP_FRAME_WIDTH, WIDTH);
    capture.set(CV_CAP_PROP_FRAME_HEIGHT, HEIGHT);

    // 保存视频
    VideoWriter vw;
    vw.open("out.avi", VideoWriter::fourcc('x', '2', '6', '4'), FPS, Size(WIDTH, HEIGHT));

    Mat frame;

    // 录制
    while (true) {
        capture >> frame;
        imshow("video", frame);
        vw.write(frame);
        if (waitKey(1) & 0xFF == 'q') {
            destroyAllWindows();
            break;
        }
    }

    // 释放
    vw.release();
    capture.release();

    return 0;
}

你可能感兴趣的:(Mac 安装 OpenCV,并配置 Clion)