Linux环境下的Opencv安装与基础使用

安装

Ctrl+Shift+T 新建终端,并在终端中输入:

cd ~ # 切换到家目录

touch install_opencv.sh # 新建脚本文件

chmod +x install_opencv.sh # 给刚刚的脚本文件添加可执行权限

然后把以下代码直接复制粘贴到 ~/install_opencv.sh 。

#!/bin/bash

# 设置日志格式
readonly COLOR_RED='\033[31m'
readonly COLOR_FULL_RED='\033[41m'
readonly COLOR_GREY='\033[90m'
readonly COLOR_GREEN='\033[92m'
readonly COLOR_YELLOW='\033[93m'
# define style
readonly STYLE_BOLD='\033[1m'
# define reset
readonly RESET='\033[0m'
# logger level
log_debug() { echo -e "${COLOR_GREY}  [DEBUG] $1${RESET}" | tee -a ../log/log_debug.log; }
log_info() { echo -e "${COLOR_GREEN}  [INFO] $1${RESET}" | tee -a ../log/log_info.log; }
log_warning() { echo -e "${COLOR_YELLOW}  [WARNING] $1${RESET}" | tee -a ../log/log_warning.log; }
log_error() { echo -e "${STYLE_BOLD}${COLOR_RED}  [ERROR] $1${RESET}" | tee -a ../log/log_error.log; }
log_critical() { echo -e "${COLOR_FULL_RED}  [CRITICAL] $1${RESET}" | tee -a ./log/log_critical.log; }
# log_info "日志输出配置"

# 通过交互式脚本读取获取密码
export password
log_info "请输入密码 :"
read password

# 用户名 username
uid=1000   # 替换为你要查询的 uid
export username=$(id $uid | awk -F'(' '{print $2}' | cut -d')' -f1)

# 下载路径 downFinder
export downFinder=~/Downloads/

log_info "opencv depend installing..."
echo ${password} | sudo -S apt update
echo ${password} | sudo -S apt install -y \
                    git \
                    vim \
                    htop \
                    cmake \
                    cmake-gui \
                    screen \
                    qtbase5-dev \
                    libavcodec-dev \
                    libavformat-dev \
                    libavutil-dev \
                    libeigen3-dev \
                    libglew-dev \
                    libgtk2.0-dev \
                    libgtk-3-dev \
                    libjpeg-dev \
                    libpostproc-dev \
                    libswscale-dev \
                    libtbb-dev \
                    libtiff5-dev \
                    libv4l-dev \
                    libxvidcore-dev \
                    libx264-dev \
                    zlib1g-dev \
                    pkg-config \
                    libxcb*
echo ${password} | sudo -S apt install -y python3-pip
echo ${password} | sudo -S -H pip3 install Cython -i https://pypi.tuna.tsinghua.edu.cn/simple
echo ${password} | sudo -S -H pip3 install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
log_info "opencv 依赖项 已安装完成 !!"

# 原路径
current_dir=$PWD
cd /tmp && \
git clone https://github.com/opencv/opencv.git --depth=1 && \
cd /tmp/opencv/ && \
mkdir build && cd build && \
cmake .. && make -j10 && \
echo ${password} | sudo -S make install
log_info "OpenCV 安装完毕 !!!"
# 返回原路径
cd ${current_dir}

现在OpenCV已经在你的电脑本地安装完成了!

基础使用

项目结构

.
├── CMakeLists.txt
└── main.cpp

CMakeLists.txt

cmake_minimum_required(VERSION 3.16)
project(opencv_test)

set(CMAKE_CXX_STANDARD 17)

find_package(OpenCV REQUIRED) # 找OPENCV4.5.0的安装路径
include_directories(${OpenCV_INCLUDE_DIRS}) #加载OPENCV4.5.0的头文件

add_executable(opencv_test main.cpp)
target_link_libraries(opencv_test ${OpenCV_LIBS})

main.cpp

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main() {
    VideoCapture video;
    // 打卡摄像头
    video.open(0);

    // 验证视频是否正常读取
    //如果读取成功  video.isOpened()会返回true
    if(!video.isOpened()){
        cout<<"请检查文件路径"<<endl;
        return -1;
    }

    //输出数据
    cout<<"视频帧率"<<video.get(CAP_PROP_FPS)<<endl;
    cout<<"视频宽度"<<video.get(CAP_PROP_FRAME_WIDTH)<<endl;

    //显示视频
    //通过每一帧的读取,显示每一帧
    while(1){
        Mat frame;  //存贮帧的Mat类
        video>>frame;
        if(frame.empty()){
            break;
        }
        imshow("hhh",frame);//窗口叫做hhh
        uchar c = waitKey(1000/video.get(CAP_PROP_FPS));
        //按q按键退出
        if(c=='q'){
            break;
        }
    }
    return 0;
}

运行

在终端中输入以下内容

mkdir build 
cd build
cmake ..
make 
./opencv_test 

最终运行效果: 使用OpenCV打开电脑摄像头并输出视频流相关信息

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值