四、图像像素读写操作

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

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

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

项目结构如图所示:

相关推荐
jimy128 分钟前
构造函数表达式的值类别是:prvalue
开发语言·c++
wuminyu31 分钟前
JVM规范学习总结-Class文件结构
java·linux·c语言·jvm·c++
多弗朗皮卡丘35 分钟前
C++特殊类设计
开发语言·c++·设计模式
Yyyyyy~2 小时前
【C++】String
c++
会编程的小孩10 小时前
C++ qt的安装
c++
再__努力1点15 小时前
RK3576/RV1126B多路视频 AI 开源引擎 CosmoEdge:从模型到交付,轻松搞定!
人工智能·opencv·计算机视觉·架构·课程设计
小成2023032026516 小时前
C++~07补充
开发语言·c++
hold?fish:palm16 小时前
40 二叉树的直径
c++·leetcode
解决小子17 小时前
【虚幻引擎】【C++】【GAS框架】构建角色基类、属性集、数值钳制
c++·笔记·游戏引擎·虚幻·gas·能力系统
lunar_coder18 小时前
【C++】如何解决智能指针的析构函数,如何实现shared_ptr的引用计数以及解决shared_ptr的循环引用
c++·1024程序员节