opcv笔记4-图像像素的读写操作

图像像素的读写操作

#include
#include

using namespace std;
using namespace cv;

//图像像素的读写操作
void pixel_visit_demo(Mat& image)
{
	for (int row = 0; row < image.rows; row++)
	{
		uchar* current_row = image.ptr<uchar>(row);//当前行的指针
		for (int col = 0; col < image.cols; col++)
		{
			if (image.channels() == 1)//通道数为1(灰度图像)
			{
				//int pv = image.at(row, col);//像素值
				//image.at(row, col) = 255 - pv;

				//指针获取修改
				int pv1 = *current_row;
				*current_row++ = 255 - pv1;
			}
			if (image.channels() == 3)//通道数为3(彩色图像)
			{
				//Vec3b bgr= image.at(row, col);//像素值(3个值)
				基于数组修改像素值
				//image.at(row, col) = 255 - bgr[0];
				//image.at(row, col) = 255 - bgr[1];
				//image.at(row, col) = 255 - bgr[2];

				//基于指针修改
				*current_row++ = 255 - *current_row;
				*current_row++ = 255 - *current_row;
				*current_row++ = 255 - *current_row;

			}
		}
	}
	imshow("像素读写显示", image);
}

int main()
{
	string path = "C:\\Users\\四明\\Pictures\\QQ图片20200608155343.jpg";
	Mat img = imread(path);
	if (img.empty())
	{
		cout << "图片加载失败!" << endl;
		return -1;
	}
	namedWindow("image", WINDOW_FREERATIO);
	imshow("image", img);
	pixel_visit_demo(img);
	waitKey(0);
	destroyAllWindows();
	return 0;
}

效果图
opcv笔记4-图像像素的读写操作_第1张图片

你可能感兴趣的:(opencv笔记,笔记,c++,opencv)