读入数字图像到数组,用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()的一点使用经验,注意默认参数的赋值的更多相关文章

  1. 在定义C++, C通用接口函数时让C++接口支持默认参数

    在SOUI4的开发中,所有SOUI核心对象都采用了一种类似COM接口的技术来导出接口. 这所以采用这种方案,主要目的是为了让SOUI4支持C语言调用,扩展SOUI的使用场景. 众所周知,C++函数的参 ...

  2. 图像读取函数cv::imread()的几种使用方式

    string imgpath = "C:\Users\Y\Pictures\miao.jpg"; OpenCV的imread()函数不支持单右斜线形式的路径,即不支持上述形式的路径 ...

  3. 拷贝构造函数,深拷贝,大约delete和default相关业务,explicit,给定初始类,构造函数和析构函数,成员函数和内联函数,关于记忆储存,默认参数,静态功能和正常功能,const功能,朋友

     1.拷贝构造 //拷贝构造的规则,有两种方式实现初始化. //1.一个是通过在后面:a(x),b(y)的方式实现初始化. //2.另外一种初始化的方式是直接在构造方法里面实现初始化. 案比例如以 ...

  4. 【opencv学习笔记五】一个简单程序:图像读取与显示

    今天我们来学习一个最简单的程序,即从文件读取图像并且创建窗口显示该图像. 目录 [imread]图像读取 [namedWindow]创建window窗口 [imshow]图像显示 [imwrite]图 ...

  5. C/C++ Python的函数默认参数

    发现C/C++  Python的函数可以使用默认参数,来减少传参时候的参数个数. 但是:这样的默认参数最好是不变对象! #include <stdio.h> #include <st ...

  6. Day5 - 03 函数的参数-位置参数和默认参数

    位置参数    调用函数时,传入函数的参数,按照位置顺序依次赋值给函数的参数.#计算乘方的函数                def power(x, n):            s = 1     ...

  7. 我的c++学习(6)默认参数和内联函数

    默认参数 一般情况下,函数调用时实参个数应与形参相同,但为了更方便地使用函数,C++也允许定义具有默认参数的函数,这种函数调用时实参个数可以与形参不相同.“默认参数”指在定义或声明函数时为形参指定默认 ...

  8. 3.C++内联函数,默认参数,占位参数

    本章主要内容: 1)内联函数(替代宏代码段) 2)默认参数 3)占位参数 1.C++的内联函数分析 1.1讲解内联函数之前,首先回忆下之前讲的define宏定义: 之前讲过宏定义会经过预处理器进行文本 ...

  9. 经典面试题-python函数之默认参数

    1.可变的默认参数----list  示例: def add(a, mylist=[]): # print(id(mylist)) mylist.append(a) return mylist pri ...

随机推荐

  1. [BEC][hujiang] Lesson03 Unit1:Working life ---Grammar & Listening & Vocabulary

    3 Working life p8 Grammar Gerund and infinitive(动名词和不定式) 一般而言:        1 动词后面接动名词还是不定式没有特定规则,主要取决于语言习 ...

  2. Java Code Examples for org.springframework.http.HttpStatus

    http://www.programcreek.com/java-api-examples/index.php?api=org.springframework.http.HttpStatus

  3. java jdbc dbcp连接SQL Server

    使用到的jar: commons-collections-3.1.jar commons-dbcp-1.4.jar commons-pool-1.5.6.jar sqljdbc4.jar dbcp配置 ...

  4. UINavigationController使用详解

    UINavigationController使用详解 有一阵子没有写随笔,感觉有点儿手生.一个多月以后终于又一次坐下来静下心写随笔,记录自己的学习笔记,也希望能够帮到大家. 废话少说回到正题,UINa ...

  5. 在自己的网站上实现QQ授权登录

    最近在实现QQ授权登录,现将我的实现过程以及我的理解整理如下.以下所述如有不对之处,请指正. 官方提供的SDK有:JS,PHP,Java.我的网站使用Scala+Play搭建的,所以只能用JS SDk ...

  6. 115. Distinct Subsequences

    题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...

  7. bzoj3774

    这算是最小割中比较难的吧 看到选取显然最小割 看到上下左右四个点我感觉肯定和染色相关 注意每个点的收益获得条件是[或],因此我们考虑拆点i', i,分别表示通过四周控制和控制本身的代价 连边s--&g ...

  8. POJ 3281 Dining (网络流构图)

    [题意]有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.现在有N头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢的食物和 ...

  9. 【转】Android AlertDialog 点击对话框外部区域不关闭的设置

    原文网址:http://blog.sina.com.cn/s/blog_8f1c79dd0101a63u.html 在Android开发中,常常需要调用对话框,但会遇到这样一种情况,在显示对话框的时候 ...

  10. Erlang入门(五)——补遗

    暂时搞不到<Programming Erlang>,最近就一直在看Erlang自带的例子和Reference Manual.基础语法方面有一些过去遗漏或者没有注意的,断断续续仅记于此. 1 ...