四、图像像素读写操作

一、图像像素读写------头文件

在项目的头文件 中,右击添加新建项

创建用于对图像显示进行操作的头文件,我这边是pixel_operate.h

该头文件声明了一个Pixel类(class Pixel),该类下面声明一个函数(void pixel_operate(Mat &image))

pixel_operate.h头文件内容如下:

cpp 复制代码
#pragma once

#include<opencv2/opencv.hpp>

using namespace cv;

class Pixel {
	public:
		void pixel_operate(Mat& image);
};

二、图像像素读写------函数实现

在项目的源文件 中,右击添加新建项

例如我的是pixel.cpp,该文件用于实现图像像素读写操作函数(pixel_operate函数)

之前自己写了一个头文件,先导入刚才的头文件,#include"pixel_operate.h"

然后对定义的函数声明(void pixel_operate(Mat& image);)进行实现

实现方式:类名::函数

Ⅰ,遍历的方式进行图像像素读写

#include"pixel_operate.h",导入头文件
void Pixel::pixel_operate(Mat& image),具体实现通过遍历对图像像素读写的函数
int h = image.rows;,行是高
int w = image.cols;,列是宽
image.channels();,获取传入图片的通道数

单通道灰度图处理:
image.at<uchar>(row, col);

获取图片的像素通过图片对象.at方法操作

图片的每个像素都是字符类型的,通过uchar进行储存

因为要输出显示,故通过int yy = image.at<uchar>(row, col);将字符类型转换为int类型

image.at<uchar>(row, col) = 255 - yy;,为了对图片像素操作效果明显,这里取反

三通道彩色图处理:
image.at<Vec3b>(row, col);,通过Vec3b 一次性存储三颜色通道的像素点值
image.at<Vec3b>(row, col)[0] = 255 - bgr[0];,因为是三颜色通道,为了对像素操作显示效果明显,也取反,其他通道也类似

显示图片:
namedWindow("pixel_operate", WINDOW_FREERATIO);,定义一个窗口名叫hjj,自适应缩放
imshow("pixel_operate", image);,显示即可

cpp 复制代码
#include"pixel_operate.h"

void Pixel::pixel_operate(Mat& image) {
	int h = image.rows;
	int w = image.cols;
	int dims = image.channels();
	for (int row = 0; row < h; row++) {
		for (int col = 0; col < w; col++) {
			if (dims == 1) {//灰度图
				int yy = image.at<uchar>(row, col);
				image.at<uchar>(row, col) = 255 - yy;//为了对图片像素操作效果明显,这里取反
			}
			if (dims == 3) {//彩色图
				Vec3b bgr = image.at<Vec3b>(row, col);
				image.at<Vec3b>(row, col)[0] = 255 - bgr[0];
				image.at<Vec3b>(row, col)[1] = 255 - bgr[1];
				image.at<Vec3b>(row, col)[2] = 255 - bgr[2];
			}
		}
	}
	namedWindow("pixel_operate", WINDOW_FREERATIO);
	imshow("pixel_operate", image);


}

效果图如下:

这边是原图,右边是通过对原图的所有图像像素取反之后的图像

Ⅱ,指针的方式进行图像像素读写

#include"pixel_operate.h",导入头文件
uchar* current_row = image.ptr<uchar>(row);,获取当前行的指针,因为图片数据是uchar类型的,故定义的指针也应该是uchar类型

cpp 复制代码
#include"pixel_operate.h"

void Pixel::pixel_operate(Mat& image) {
	int h = image.rows;
	int w = image.cols;
	int dims = image.channels();
	for (int row = 0; row < h; row++) {
		uchar* current_row = image.ptr<uchar>(row);
		for (int col = 0; col < w; col++) {
			if (dims == 1) {
				int yy = *current_row;
				*current_row++ = 255 - yy;
			}
			if (dims == 3) {//三颜色通道,深度方向移动
				*current_row++ = 255 - *current_row;
				*current_row++ = 255 - *current_row;
				*current_row++ = 255 - *current_row;
			}
		}
	}
	namedWindow("pixel_operate", WINDOW_FREERATIO);
	imshow("pixel_operate", image);

}

效果图如下:

这边是原图,右边是通过对原图的所有图像像素取反之后的图像

三、图像像素读写------主函数调用

在项目的源文件 中,右击添加新建项

例如我的是main.cpp,程序的主函数,用于调用图像像素读写函数

#include"pixel_operate.h",导入头文件
Pixel yy;,定义图像像素读写函数对象
yy.pixel_operate(src);,调用已实现的图像像素读写函数

cpp 复制代码
#include <opencv2/opencv.hpp>
#include <iostream>
#include "pixel_operate.h"

using namespace cv;
using namespace std;

int main(int argc, char** argv) {
	Mat src = cv::imread("E:/C++_workspace/beyond.jpg", IMREAD_COLOR);

	if (src.empty()) {
		printf("load image is false...\n");
		return -1;
	}

	namedWindow("yanyu",WINDOW_FREERATIO);
	imshow("yanyu", src);

	Pixel yy;
	yy.pixel_operate(src);

	waitKey(0);
	destroyAllWindows();

	return 0;
}

项目结构如图所示:

相关推荐
王维同学2 小时前
Winsock 协议与名称空间 Provider 目录
网络·c++·windows·安全
fai厅的秃头姐!4 小时前
OpenCV——进阶
人工智能·opencv·计算机视觉
拳里剑气4 小时前
C++算法:BFS解决FloodFill算法
c++·算法·bfs·宽度优先
爱写代码的小朋友4 小时前
从零开始学 Win32 API:C++ 窗口编程实战(VS Code + MinGW-w64 命令行详解)
开发语言·c++
小小晓.5 小时前
C++:语句和作用域
开发语言·c++
fqbqrr5 小时前
2607C++,soui,xplayer视频播放器
c++·soui
zh路西法6 小时前
【10天速通ROS2-PX4无人机】(四) 关掉GPS和气压计,纯激光定位还能飞吗
c++·无人机·px4·ros2·卡尔曼滤波·fastlio2
yyds_yyd_100867 小时前
1464. 数组中两元素的最大乘积(2026.07.27)
数据结构·c++·算法·leetcode
王老师青少年编程9 小时前
csp信奥赛C++高频考点专项训练:【排序算法】案例7:最高分数的学生姓名
c++·排序算法·csp·高频考点·信奥赛·最高分数的学生姓名
库克克9 小时前
【C++】set 与multiset
开发语言·c++