OpenCV 读取像素值的常用方法

OpenCV常用读取像素方法及读取速度对比

法一 指针法(效率最高)

void main()
{
	//灰度图
	Mat src = imread("src.jpg", 0);
	//图像行、列
	int row = src.rows, col = src.cols;
	for (int i = 0; i < row; i++)
	{
		//读取整行数据
		uchar* ptr = src.ptr<uchar>(i);
		for (int j = 0; j < col; j++)
		{
			int pixel = (int)ptr[j];
			cout << pixel << "    ";
		}
		cout << endl;
	}
}

法二

void main()
{
	//灰度图
	Mat src = imread("src.jpg", 0);
	//图像行、列
	int row = src.rows, col = src.cols;
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			int pixel = (int)src.ptr<uchar>(i, j);
			cout << pixel << "    ";
		}
		cout << endl;
	}
}

法三

void main()
{
	//灰度图
	Mat src = imread("src.jpg", 0);
	//图像行、列
	int row = src.rows, col = src.cols;
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			int pixel = (int)src.at<uchar>(i,j);
			cout << pixel << "    ";
		}
		cout << endl;
	}
}
相关推荐
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页