四、图像像素读写操作

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

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

创建用于对图像显示进行操作的头文件,我这边是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;
}

项目结构如图所示:

相关推荐
小飞在努力8 小时前
【实现负载均匀在线判题③】从零散模块到统一,编译运行全链路封装
c语言·开发语言·c++·笔记
星恒随风12 小时前
从0实现C++ AI大模型接入SDK(四):大模型API接入、公共数据结构设计与日志库封装
linux·服务器·数据结构·c++·人工智能
炸膛坦客12 小时前
单片机/C/C++八股:(三十四)C 语言函数参数入栈顺序
c语言·开发语言·c++
码途行者13 小时前
每天10分钟学Go:第20课 泛型
c++·算法·golang
AIgorithmGEEK14 小时前
算法·哈希表|从“逐个比”到“按 key 算位置”
数据结构·c++·算法·leetcode·hash
星恒随风16 小时前
从0实现C++ AI大模型接入SDK(三):环境搭建与ChatSDK快速上手——实现我们的第一个AI聊天程序
linux·开发语言·c++·人工智能·笔记
h_a_o777oah17 小时前
【数据结构】FHQ Treap:无旋转的平衡 BST
数据结构·c++·算法·acm·平衡树·treap·fhq treap
撸码猿17 小时前
Ladybird 进入 alpha:从零写的浏览器引擎现在能下载了
c++·rust·开源·浏览器·ladybird
ShineWinsu19 小时前
对于Qt:重写事件步骤的解析
开发语言·c++·qt·客户端·事件·鼠标事件·步骤
爱和冰阔落20 小时前
【Linux网络】HTTP 服务器怎么返回网页和图片?从 404、301 到 Content-Type 与静态资源完整实战
linux·c++·http·网络编程·web服务器