C++&OpenCV中读取灰度图像到数组的两种

如标题所言,此处是对于灰度图像而言
///method 1 read the image data one by one
	for (int row = 0, i = 0;row < imgDst.rows;row++)
	{
		for (int col = 0;col < imgDst.cols;col++)
		{
			cout << setw(3) << (int)imgDst.at<uchar>(row, col) << " ";
			arr[i] =imgDst.at<uchar>(row, col) ;
			//cout << (int)arr[i] << " ";
			i++;
		}
		cout << endl;
	}

	//test to print
	for (int q = 0;q < imgDst.rows;q++)
	{
		for (int k = 0;k < imgDst.cols;k++)
		{
			int pos = q*imgDst.cols + k;
			cout << setw(3) << (int)arr[pos] << " ";
		}
		cout << endl;
	}
///method 2 memcpy the image data to uchar arr in rows
	unsigned char *imgData = new unsigned char[vec_Num];//直接将图像数据拷贝到数组中;
	memcpy(imgData, imgDst.data, imgDst.rows*imgDst.cols*sizeof(unsigned char));
	for (int i = 0;i < vec_Num;i++)
	{
		arr[i] = (float)(imgData[i])/255;
	}

	//test to print
	for (int q = 0;q < imgDst.rows;q++)
	{
		for (int k = 0;k < imgDst.cols;k++)
		{
			int pos = q*imgDst.cols + k;
			cout << setw(3) << (int)arr[pos] << " ";
		}
		cout << endl;
	}
	cout << endl;
原文地址:https://www.cnblogs.com/xiaopanlyu/p/5335061.html