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

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

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

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/231076

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 下午2:53
下一篇 2023年2月13日 下午2:53

相关推荐