关于图像读取函数imread()的一点使用经验,注意默认参数的赋值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于图像读取函数imread()的一点使用经验,注意默认参数的赋值相关的知识,希望对你有一定的参考价值。

读入数字图像到数组,用CNN进行训练,发现关于图像读取的一个问题。

问题描述:读取灰度数字图像,在验证时发现存在错误,从图像到数组中的值不完全一样?

main code as follows:

        int dst_width = 12, dst_height = 17;//set the dst size
	int vec_Num = dst_width*dst_height;
	/*the second parameter must set when read gray image, 
	//the default value=1 which return a 3-channel color image*/
	Mat imgSrc = imread(img_path);
	if (imgSrc.empty())
	{
		cout << "read " << img_path.c_str() << " failed!" << endl;
	}

	Size dstSize = Size(dst_width, dst_height);
	Mat imgDst = Mat(dstSize, CV_8UC1);
	resize(imgSrc, imgDst, dstSize);

	vector<float> arr(vec_Num);
        int dst_width = 12, dst_height = 17;//set the dst size
	int vec_Num = dst_width*dst_height;
	/*the second parameter must set when read gray image, 
	//the default value=1 which return a 3-channel color image*/
	Mat imgSrc = imread(img_path, 0);
	if (imgSrc.empty())
	{
		cout << "read " << img_path.c_str() << " failed!" << endl;
	}

	Size dstSize = Size(dst_width, dst_height);
	Mat imgDst = Mat(dstSize, CV_8UC1);
	resize(imgSrc, imgDst, dstSize);

	vector<float> arr(vec_Num);
        ///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;

	delete[] imgData;
	imgSrc.release();
	imgDst.release();

the result1 as follows:

技术分享                 技术分享

      左图为读入到数组以后,print出来的                                                                                  右图为原始图像

差异很明显,同时,错误也很明显。

现在修改代码:

Mat imgSrc = imread(img_path,0);

the result2 as follows:

技术分享            技术分享

         左图为修改代码后读入到数组,print出来的                                                                                      右图为原始图像

 

conclusion很明显得到的结果是不同的,所以,通过是这次使用imread()函数,告诉我们要注意一些缺省的默认参数是否与自己当前所解决的问题一致。

 

Appendix:

The OpenCV API reference introduce as follows:

C++: Mat imread(const string& filename, int flags=1 )
Parameters:
filename – Name of file to be loaded.
flags –
        Flags specifying the color type of a loaded image:
                 CV_LOAD_IMAGE_ANYDEPTH - If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
                 CV_LOAD_IMAGE_COLOR - If set, always convert image to the color one
                 CV_LOAD_IMAGE_GRAYSCALE - If set, always convert image to the grayscale one
        >0 Return a 3-channel color image.
                Note In the current implementation the alpha channel, if any, is stripped from the output image. Use negative value if you need the alpha channel.
       =0 Return a grayscale image.
       <0 Return the loaded image as is (with alpha channel).












以上是关于关于图像读取函数imread()的一点使用经验,注意默认参数的赋值的主要内容,如果未能解决你的问题,请参考以下文章

详解OpenCV的函数imread()和函数imshow(),并利用它们实现对图像的读取和显示

在 OpenCv 中使用“cv::imread”函数读取图像的问题

OpenCV学习笔记1基础:读取显示保存图像

OpenCV学习笔记1基础:读取显示保存图像

OpenCV学习笔记1基础:读取显示保存图像

OpenCV学习笔记1基础:读取显示保存图像