AI正在绞尽脑汁想思路ING···
BiのAI摘要
HunYuan-Lite

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

图片读取与展示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 读取图像
#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。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// 读取像素
#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;

}

修改像素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// 修改像素
#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;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// 图像通道分离与合并
#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);
}