c++imread 函数_关于图像读取函数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 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 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:

20180110183623680577.png                 20180110183623684483.png

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

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

现在修改代码:Mat imgSrc = imread(img_path,0);

the result2 as follows:

20180110183623687413.png            20180110183623689366.png

左图为修改代码后读入到数组,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).

标签:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenCV是一个开源的计算机视觉库,它提供了许多用于图像处理和计算机视觉任务的函数和工具。它可以与多种编程语言一起使用,包括C语言。在C语言中,你可以使用OpenCV的函数来处理图像、检测对象等。例如,你可以使用cvHaarDetectObjects函数来进行对象检测,该函数可以在给定图像使用Haar级联分类器来检测对象。 在C语言中使用OpenCV,你需要包含OpenCV的头文件,并链接OpenCV的库文件。在你的代码中,你可以使用OpenCV的函数来加载图像、进行图像处理和显示。例如,你可以使用cv::imread函数加载图像,并使用cv::pyrDown函数创建一个新的图像,具有输入图像一半的宽度和高度。然后,你可以使用cv::imshow函数来显示图像。 总之,OpenCV提供了许多用于图像处理和计算机视觉任务的函数和工具,你可以在C语言中使用OpenCV来编写图像处理和计算机视觉的应用程序。你需要包含OpenCV的头文件,并链接OpenCV的库文件,然后你可以使用OpenCV的函数来进行图像处理、对象检测等操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [图像处理基本方法-c语言调用opencv实现人脸检测功能](https://blog.csdn.net/jack8126/article/details/118947422)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [在C++上如何使用OpenCV头文件是什么_Opencv系列1.2实例介绍](https://blog.csdn.net/weixin_39949386/article/details/110157121)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值