码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-03-29 22:23:12      阅读:2067      评论:0      收藏:0      [点我收藏+]

标签:

///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;

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

标签:

原文地址:http://www.cnblogs.com/Steven-Love-Arlene/p/5335061.html

(0)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!