阶段任务一
1、问题引入
1). 如何把不同的图形绘制在一起;
2). 如何获取图像像素的RGB值;
3). 如何修改图像像素的RGB值;
2、简介
掌握Mat像素访问的方法;
3、知识点
图像像素在内存中的存放结构
Mat像素访问的方法
方法一 at(i,j)访问
Vec3b & pixel = img.at<Vec3b>(h, w);
pixel[0] = 255;
pixel[1] = 0;
pixel[2] = 0;
方法二 ptr(i) [j]访问
uchar *ptr = img.ptr<uchar>(h, w);
ptr[0] = ptr[0] / 2;
ptr[1] = ptr[1] / 2;
ptr[2] = ptr[2] / 2;
阶段任务二
基本任务
- 读取图像素的RGB值,并打印输出;
- 设置图像部分区域指定颜色;
示例代码:
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main() {
// 读入一张图片
Mat img = imread("pic.jpg");
// 创建一个名为 "img" 窗口
namedWindow("img");
for (int h = 0; h < 200; h++) {
for (int w = 0; w < 200; w++) {
Vec3b & pixel = img.at<Vec3b>(h, w);
pixel[0] = 128;
pixel[1] = 128;
pixel[2] = 128;
}
}
// 在窗口中显示游戏原画
imshow("img", img);
// 等待 6000 ms 后窗口⾃自动关闭
waitKey(0);
}
运行结果:
扩展任务
- 修改图像矩形区域的像素的RGB值,使其颜色变深或变浅;
- 修改图像圆形区域的像素的RGB值,使其颜色变深或变浅;
实现截图: