OpenCV学习笔记之遍历图像像素点

 

通过行地址来逐行遍历像素点的值。

 

代码:

#include
#include
using namespace cv;
using namespace std;

int main() {
	//读取单通道图像
	Mat lena_gray = imread("D:\\lena.jpg", 0);
	for (int y = 0; y < lena_gray.rows; y++) {
		for (int x = 0; x < lena_gray.cols; x++) {
			uchar *data = lena_gray.ptr(y);
			int pixel = data[x];
			cout << "gray_pixel = " << pixel << endl;
		}
	}
	//读取RGB彩色图像
	Mat lena = imread("D:\\lena.jpg");
	for (int y = 0; y < lena.rows; y++) {
		for (int x = 0; x < lena.cols; x++) {
			uchar *data = lena.ptr(y);
			int b = data[3 * x];
			cout << "blue_pixel = " << b << endl;
			int g = data[3 * x + 1];
			cout << "green_pixel = " << g << endl;
			int r = data[3 * x + 2];
			cout << "red_pixel = " << r << endl;
		}
	}

	system("pause");
	return 0;
}

 

你可能感兴趣的:(Opencv)