四、图像像素读写操作

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

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

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

项目结构如图所示:

相关推荐
红米煮粥35 分钟前
OpenCV-直方图
人工智能·opencv·计算机视觉
weixin_4866811439 分钟前
C++系列-STL容器中统计算法count, count_if
开发语言·c++·算法
基德爆肝c语言39 分钟前
C++入门
开发语言·c++
怀九日1 小时前
C++(学习)2024.9.18
开发语言·c++·学习·面向对象·引用·
一道秘制的小菜1 小时前
C++第七节课 运算符重载
服务器·开发语言·c++·学习·算法
代码小狗Codog2 小时前
C++独立开发开源大数计算库 CBigNum
数据结构·c++
WenGyyyL2 小时前
力扣最热一百题——二叉树的直径
java·c++·算法·二叉树·深度优先
sdlkjaljafdg2 小时前
vector<bool>性能测试
开发语言·c++·算法
嵌入式杂谈2 小时前
计算机视觉——基于OpenCV和Python进行模板匹配
python·opencv·计算机视觉
yery3 小时前
Ubuntu24.04下编译OpenCV + OpenCV Contrib 4.10.0
人工智能·opencv·计算机视觉