第二阶段:OpenCV 基础

1. OpenCV 安装与配置

目标:

内容:

实践:

1.1 安装 OpenCV

方式一:使用包管理器安装(以Ubuntu为例)
sudo apt update
sudo apt install libopencv-dev
方式二:从源码编译
# 安装依赖项
sudo apt update
sudo apt install build-essential cmake git pkg-config libjpeg-dev libpng-dev libtiff-dev libjasper-dev libeigen3-dev

# 下载OpenCV源码
git clone https://github.com/opencv/opencv.git
cd opencv
mkdir build
cd build

# 配置CMake并编译
cmake ..
make -j4
sudo make install

1.2 配置CMake以使用OpenCV

cmake_minimum_required(VERSION 3.10)
project(OpenCVExample)

# 设置C++标准
set(CMAKE_CXX_STANDARD 11)

# 设置OpenCV的路径(根据安装路径调整)
find_package(OpenCV REQUIRED)

# 添加可执行文件
add_executable(OpenCVExample main.cpp)

# 链接OpenCV库
target_link_libraries(OpenCVExample ${OpenCV_LIBS})

2. 图像读取与显示

目标 :

内容:

实践:

2.1 读取并显示图像

#include <opencv2/opencv.hpp>
#include <iostream>

int main() {
    // 读取图像(默认以BGR格式读取)
    cv::Mat image = cv::imread("example.jpg");

    // 检查图像是否成功读取
    if (image.empty()) {
        std::cerr << "Could not open or find the image!" << std::endl;
        return -1;
    }

    // 显示图像
    cv::imshow("Display Image", image);

    // 0为等待用户按键,也可以创建延时,你们懂
    cv::waitKey(0);

    // 销毁所有窗口
    cv::destroyAllWindows();

    return 0;
}
解释:

2.2 将图像保存到文件

cv::imwrite("output.jpg", image);

图像处理基础

目标:

内容:

实践:

3.1 将彩色图像转换为灰度图像

cv::Mat grayImage;
cv::cvtColor(image, grayImage, cv::COLOR_BGR2GRAY);
cv::imshow("Gray Image", grayImage);
cv::waitKey(0);
解释:

3.2 图像缩放和旋转

// 缩放图像
cv::Mat resizedImage;
cv::resize(image, resizedImage, cv::Size(300, 300));  // 将图像缩放为300x300大小

// 显示缩放后的图像
cv::imshow("Resized Image", resizedImage);

// 旋转图像
cv::Mat rotatedImage;
cv::Point2f center(resizedImage.cols / 2, resizedImage.rows / 2);  // 旋转中心
cv::Mat rotationMatrix = cv::getRotationMatrix2D(center, 45, 1);  // 旋转45度,缩放因子为1
cv::warpAffine(resizedImage, rotatedImage, rotationMatrix, resizedImage.size());  // 执行旋转变换

// 显示旋转后的图像
cv::imshow("Rotated Image", rotatedImage);
cv::waitKey(0);
解释:

3.3 像素操作

// 访问图像的第(100,100)个像素
cv::Vec3b color = image.at<cv::Vec3b>(100, 100);  // BGR顺序,这很细节
std::cout << "Pixel at (100, 100): " 
          << "Blue: " << (int)color[0] << " "
          << "Green: " << (int)color[1] << " "
          << "Red: " << (int)color[2] << std::endl;

// 修改图像的某个像素值
image.at<cv::Vec3b>(100, 100) = cv::Vec3b(0, 255, 0);  // 将该像素修改为绿色
cv::imshow("Modified Image", image);
cv::waitKey(0);
解释:

拓展

关于我们会用到的编程思想:面向对象编程。

面向对象编程与C++中的面向对象

1. 面向对象编程(OOP)的概述

什么是面向对象编程?

面向对象编程(Object-Oriented Programming,简称OOP)是一种程序设计范式,它通过对象来组织程序结构。每个对象代表了程序中的一个实体,它将数据和操作这些数据的方法结合在一起,形成了“类”的概念。

在面向对象编程中,程序是由对象和对象之间的交互组成的,核心思想是:

面向对象编程的优点:

  1. 代码重用性:继承允许子类重用父类的代码,减少了重复代码。
  2. 可维护性:封装和抽象使得对象的内部实现与外部使用分离,增加了代码的可维护性。
  3. 灵活性:多态性使得代码更加灵活,可以通过相同的接口调用不同的实现。

2. C++中的面向对象编程

C++是一种支持面向对象编程的语言,它通过类和对象来实现OOP的概念。

2.1 类与对象

类的定义和对象的创建:

#include <iostream>
using namespace std;

// 定义一个类
class Car {
private:
    string brand;
    int year;

public:
    // 构造函数:用于创建对象时初始化属性
    Car(string b, int y) : brand(b), year(y) {}

    // 成员函数:获取品牌信息
    void displayInfo() {
        cout << "Brand: " << brand << ", Year: " << year << endl;
    }

    // 设定品牌
    void setBrand(string b) {
        brand = b;
    }

    // 获取品牌
    string getBrand() {
        return brand;
    }
};

int main() {
    // 创建对象
    Car myCar("Toyota", 2020);
    myCar.displayInfo();

    // 修改品牌
    myCar.setBrand("Honda");
    cout << "Updated Brand: " << myCar.getBrand() << endl;

    return 0;
}

解释:

2.2 封装

class BankAccount {
private:
    double balance;

public:
    // 构造函数:初始化账户余额
    BankAccount(double initial_balance) : balance(initial_balance) {}

    // 存款操作
    void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
        }
    }

    // 取款操作
    bool withdraw(double amount) {
        if (amount > 0 && balance >= amount) {
            balance -= amount;
            return true;
        }
        return false;
    }

    // 获取账户余额
    double getBalance() {
        return balance;
    }
};

解释:

2.3 继承

class Vehicle {
public:
    string brand;

    Vehicle(string b) : brand(b) {}

    void honk() {
        cout << "Beep! Beep!" << endl;
    }
};

// 子类继承自Vehicle类
class Car : public Vehicle {
public:
    int doors;

    Car(string b, int d) : Vehicle(b), doors(d) {}

    void displayInfo() {
        cout << "Brand: " << brand << ", Doors: " << doors << endl;
    }
};

int main() {
    Car myCar("Toyota", 4);
    myCar.honk();
    myCar.displayInfo();

    return 0;
}

解释:

2.4 多态

class Shape {
public:
    virtual void draw() {
        cout << "Drawing a shape" << endl;
    }
};

class Circle : public Shape {
public:
    void draw() override {
        cout << "Drawing a circle" << endl;
    }
};

class Square : public Shape {
public:
    void draw() override {
        cout << "Drawing a square" << endl;
    }
};

int main() {
    Shape* shape1 = new Circle();
    Shape* shape2 = new Square();

    shape1->draw();
    shape2->draw(); 

    delete shape1;
    delete shape2;

    return 0;
}

解释: