---
title: opencv应用-基础操作
categories:
  - 技术
series: opencv
tags:
  - opencv
abbrlink: e8f95ead
summary: >-
  这篇文章介绍了使用C++和OpenCV库进行图像处理的基础操作，包括图片的读取与展示、像素的读取与修改、图像的基本变换（缩放、旋转、平移、翻转）、以及图像通道的分离与合并。文章详细讲解了每个操作的代码实现，并展示了如何通过OpenCV库提供的函数来完成这些任务。这对于初学者来说是非常有用的，因为它帮助他们理解如何使用OpenCV进行图像处理。
date: 2026-03-15 09:56:51
---

这里用C++进行编程，发现菜鸟教程只有python的版本，那就记录一下。

# 图片读取与展示

```cpp
// 读取图像
#include<opencv2/opencv.hpp>
#include<iostream>

using namespace std;
using namespace cv;

int main(){
    Mat src = imread("../img/1.png");
    imshow("input",src);
    waitKey(0);
    destroyAllWindows();
    return 0;
}
```

# 图像基本操作
## 读取像素
需要用到三维向量数组Vect3b，这里需要注意的是，Opencv是BGR而不是我们常用的RGB。
```cpp
// 读取像素
#include<opencv2/opencv.hpp>
#include<iostream>

using namespace std;
using namespace cv;

int main()
{
    string image_path = "../img/1.png";
    Mat image = imread(image_path);

    if (image.empty()) {
        cout << "错误：无法加载图像，请检查路径是否正确。" << endl;
        return -1;
    }

    Vec3b pixel_value = image.at<Vec3b>(100, 150);

    cout << "B: " << (int)pixel_value[0] << " "
         << "G: " << (int)pixel_value[1] << " "
         << "R: " << (int)pixel_value[2] << endl;

}
```

## 修改像素

```cpp
// 修改像素
#include<opencv2/opencv.hpp>
#include<iostream>

using namespace std;
using namespace cv;

int main()
{
    string image_path = "../img/1.png";
    Mat image = imread(image_path);
    Mat result = image.clone();

    if (image.empty()) {
        cout << "错误：无法加载图像，请检查路径是否正确。" << endl;
        return -1;
    }

    Rect roi_rect(0, 0, 100, 100);
    Mat roi = result(roi_rect);
    roi.setTo(Scalar(0, 255, 0));
    imshow("Original (Unchanged)", image);
    imshow("Modified Copy", result);
    waitKey(0);

    return 0;

}
```

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

int main() {
    // 读取图像
    cv::Mat img = cv::imread("../img/1.png");
    if (img.empty()) {
        std::cout << "无法读取图像" << std::endl;
        return -1;
    }

    // 1. 缩放
    cv::Mat resized_img;
    cv::resize(img, resized_img, cv::Size(200, 200));
    cv::imshow("resized_img", resized_img);

    // 2. 旋转
    cv::Mat rotated_img, M_rot;
    cv::Point2f center(img.cols / 2.0, img.rows / 2.0);
    M_rot = cv::getRotationMatrix2D(center, 45, 1.0);
    cv::warpAffine(img, rotated_img, M_rot, img.size());
    cv::imshow("rotated_img", rotated_img);

    // 3. 平移
    cv::Mat translated_img;
    cv::Mat M_trans = (cv::Mat_<float>(2, 3) << 1, 0, 100, 0, 1, 50);
    cv::warpAffine(img, translated_img, M_trans, img.size());
    cv::imshow("translated_img", translated_img);

    // 4. 翻转
    cv::Mat flipped_img;
    cv::flip(img, flipped_img, 1);
    cv::imshow("flipped", flipped_img);

    // 显示结果
    cv::imshow("Original", img);
    cv::waitKey(0);

    return 0;
}
```

```cpp
// 图像通道分离与合并
#include<opencv2/opencv.hpp>
#include<iostream>

using namespace std;
using namespace cv;

int main()
{
    string image_path = "../img/1.png";
    Mat image = imread(image_path);
    Mat result = image.clone();
    // 定义向量数组接收通道
    vector<Mat> channels;
    // 拆分
    split(result,channels);

    Mat b = channels[0];
    Mat g = channels[1];
    Mat r = channels[2];

    imshow("Blue Channel (Grayscale)", channels[0]);
    imshow("Green Channel (Grayscale)", channels[1]);
    imshow("Red Channel (Grayscale)", channels[2]);

    // merge
    Mat merged_img;
    vector<cv::Mat> channels_to_merge;
    channels_to_merge.push_back(b);
    channels_to_merge.push_back(g);
    channels_to_merge.push_back(r);

    merge(channels_to_merge, merged_img);
    imshow("merged",merged_img);

    waitKey(0);

    return(0);
}
```