'''
Mat cv::imread ( const String & filename,
int flags = IMREAD_COLOR
)
Python:
retval = cv.imread( filename[, flags] )
官方的文档是这么说的,
The function imread loads an image from the specified file and returns it. If the image cannot be read (because of missing file,
improper permissions, unsupported or invalid format), the function returns an empty matrix ( Mat::data==NULL )
这里要特别注意,当文件没被读取到,返回的是NULL。
首先看一下参数,文件名不能少,其它的都不是必须的。
这个函数看着简单,其实那都是表象,我们来一一看看官方的文档。
The function determines the type of an image by the content, not by the file extension.
此函数是根据图像的内容来决定其类型,而不是根据文件的扩展名。
In the case of color images, the decoded images will have the channels stored in B G R order.
当图像是彩色图时,图像解码后输出的维度是BGR的顺序。
When using IMREAD_GRAYSCALE, the codec's internal grayscale conversion will be used, if available.
Results may differ to the output of cvtColor()
当使用参数'IMREAD_GRAYSCALE',解码器的内置灰度图转换可能会被调用,这个转换可能和opencv的cvtColor()这个函数得到的结果不一样。
On Microsoft Windows* OS and MacOSX*, the codecs shipped with an OpenCV image
(libjpeg, libpng, libtiff, and libjasper) are used by default.
So, OpenCV can always read JPEGs, PNGs, and TIFFs.
On MacOSX, there is also an option to use native MacOSX image readers.
But beware that currently these native image loaders give images with different pixel values because of the color management embedded into MacOSX.
此处解释了WINDOWS, MacOSX的解码,太详细了,具体细看。 On Linux*, BSD flavors and other Unix-like open-source operating systems,
OpenCV looks for codecs supplied with an OS image. Install the relevant packages
(do not forget the development files, for example, "libjpeg-dev", in Debian* and Ubuntu*) to get the codec
support or turn on the OPENCV_BUILD_3RDPARTY_LIBS flag in CMake.
Linux包括其它别的开源操作系统,opencv解码依赖系统的image操作。因此,需要安装相关软件包,比如libjpeg-dev,同时在安装时,cmake文件中
需要打开OPENCV_BUILD_3RDPARTY_LIBS 这个选项。 In the case you set WITH_GDAL flag to true in CMake and IMREAD_LOAD_GDAL to load the image,
then the GDAL driver will be used in order to decode the image, supporting the following formats:
Raster, Vector.
当你在cmake中打开了WITH_GDAL,那么在加载图像时将寻找GDAL驱动来解码图像。 If EXIF information is embedded in the image file, the EXIF orientation will be taken into account
and thus the image will be rotated accordingly except if the flags IMREAD_IGNORE_ORIENTATION or
IMREAD_UNCHANGED are passed.Use the IMREAD_UNCHANGED flag to keep the floating point values from PFM image.
如果 EXIF 这个信息被封装在图像文件中, By default number of pixels must be less than 2^30. Limit can be set using system variable OPENCV_IO_MAX_IMAGE_PIXELS '''
'''
filename Name of file to be loaded.
flags Flag that can take values of cv::ImreadModes
下面看一看ImreadModes都可以取哪些值,常用的就是前三个,其余可以参考官方文档:
enum ImreadModes
{
IMREAD_UNCHANGED = -1, # If set, return the loaded image as is (with alpha channel包含alpha通道, otherwise it gets cropped).
IMREAD_GRAYSCALE = 0, # If set, always convert image to the single channel grayscale image.
IMREAD_COLOR = 1, # If set, always convert image to the 3 channel BGR color image.
IMREAD_ANYDEPTH = 2,
IMREAD_ANYCOLOR = 4,
IMREAD_LOAD_GDAL = 8,
IMREAD_REDUCED_GRAYSCALE_2 = 16,
IMREAD_REDUCED_COLOR_2 = 17,
IMREAD_REDUCED_GRAYSCALE_4 = 32,
IMREAD_REDUCED_COLOR_4 = 33,
IMREAD_REDUCED_GRAYSCALE_8 = 64,
IMREAD_REDUCED_COLOR_8 = 65,
IMREAD_IGNORE_ORIENTATION = 128,
};
''' import numpy as np
import cv2
img = cv2.imread(r"messi.jpg") # 默认是IMREAD_COLOR
print('default image',img.shape) # (296, 474, 3) in height, width, channel
cv2.imshow("default", img)
cv2.waitKey(0)
cv2.destroyAllWindows() img2 = cv2.imread(r"messi.jpg",cv2.IMREAD_COLOR) #
print('color image',img.shape) # (296, 474, 3) in height, width, channel
cv2.imshow("color", img2)
cv2.waitKey(0)
cv2.destroyAllWindows() '''
逐个像素比较一下,两者是完全相等的。
'''
num = 0
for i in range(img.shape[0]):
for j in range(img.shape[1]):
if img[i,j,0] != img2[i,j,0] or img[i,j,1] != img2[i,j,1] or img[i,j,2] != img2[i,j,2]:
num +=1
print (num) # 读入完整图片,含alpha通道
imgwithalpha = cv2.imread('messi.jpg', cv2.IMREAD_UNCHANGED)
cv2.imshow('IMREAD_UNCHANGED+Color', imgwithalpha)
cv2.waitKey(0)
cv2.destroyAllWindows() # 读入彩色图片,忽略alpha通道
imgcolor = cv2.imread('messi.jpg', cv2.IMREAD_COLOR)
cv2.imshow('IMREAD_COLOR+Color', imgcolor)
cv2.waitKey(0)
cv2.destroyAllWindows()
'''
上面两种格式读进来的图,可以类似之前的做法比较一下,还是有稍微差别的。
''' #彩色图片以灰度图片读入
imgGray = cv2.imread('messi.jpg', cv2.IMREAD_GRAYSCALE)
cv2.imshow('IMREAD_GRAYSCALE+Color', imgGray)
cv2.waitKey(0)
cv2.destroyAllWindows()

  

imread opencv的更多相关文章

  1. opencv安装及学习资料

    第一次装时win7+VS2010+opencv3.0,结果不成功,原因解压出来的没有vc10,可能新版本不在支持vc的旧版本了.所以换了VS2013+opencv3.0,比较经典的安装时VS2010+ ...

  2. vs2013中opencv的配置

    下面开始介绍如何配置,我用的系统是win8.1 64位系统,vs用的是vs3013 ultimate,先到官网下载opencv 我用的的版本是最新的版本3.0 ALPHA,下载下来直接执行即可,实际上 ...

  3. Python各类图像库的图片读写方式总结

    最近在研究深度学习视觉相关的东西,经常需要写python代码搭建深度学习模型.比如写CNN模型相关代码时,我们需要借助python图像库来读取图像并进行一系列的图像处理工作.我最常用的图像库当然是op ...

  4. matplotlib、PIL、cv2图像操作 && caffe / tensorflow 通道顺序

    用python进行图像处理中分别用到过matplotlib.pyplot.PIL.cv2三种库,这三种库图像读取和保存方法各异,并且图像读取时顺序也有差异,如plt.imread和PIL.Image. ...

  5. opencv用imread( argv[1], 1)读取图片

    显示一幅图:主要是运用功能:imread namedWindow imshowimread:从字面意思我们就可以看懂,用来读取图片的:namedWindow:显然,我们也可以看到这是用来命名窗口名称的 ...

  6. OpenCV imread读取图片,imshow展示图片,出现cv:Exception at memory location异常

    问题如上.环境:VS2013. 代码如下: #include "stdafx.h" #include "opencv2\opencv.hpp" using na ...

  7. imread() not working in OpenCV 2.4.11 Debug mode

    The OpenCV function imread() not working in OpenCV 2.4.11 Debug mode of VS2010 under Win32, the way ...

  8. OpenCV问题集锦,图片显示不出来,WaitKey(0),imread()不能读图片,未经处理的异常,等问题集合

    昨天根据uc伯克利的人工图像分割文件.seg,显示图像的时候调用了OpenCV的库函数,图片都能用imwrite写好,但是imshow死活显示不出来. 今天早上发现原来是imshow()后面应该加上: ...

  9. opencv : imread()的应用

    概述: imread()是opencv中用于读取图片的一个工具.怎么读取图片看似一个很简单的工作,但实际上也有一些细节需要我们注意,以避免在后续的操作中出现bug. 函数原型: 函数原型: Mat i ...

随机推荐

  1. SSRS筛选器的IN运算(即包含于)用法

    筛选器的IN运算,在Microsoft的官网上没像样儿的例子,不好设置,很容易错 Microsoft上的文档:https://docs.microsoft.com/zh-cn/sql/reportin ...

  2. Collection子接口:Set接口

    1.Set 存储的数据特点:无序的.不可重复的元素具体的:以HashSet为例说明: 1. 无序性:不等于随机性.存储的数据在底层数组中并非照数组索引的顺序添加,而是根据数据的哈希值决定的. 2. 不 ...

  3. 可变参数和Collections集合工具类的方法_addAll&shuffle

    可变参数 可变参数:是JDK1.5之后出现的新特性 使用前提:当方法的参数列表数据类型已经确定,但是参数的个数不确定,就可以使用可变参数 使用格式:定义方法时使用 ~修饰符 返回值类型 方法名(数据类 ...

  4. Hadoop集群搭建(完全分布式版本) VMWARE虚拟机

    Hadoop集群搭建(完全分布式版本) VMWARE虚拟机 一.准备工作 三台虚拟机:master.node1.node2 时间同步 ntpdate ntp.aliyun.com 调整时区 cp /u ...

  5. [javaweb]javaweb中HttpServletResponse实现文件下载,验证码和请求重定向功能

    HttpServletResponse web服务器接受到客户端的http请求之后,针对这个请求,分别创建一个代表请求的httpServletRequest和代表响应的HttpServletRespo ...

  6. SkyWalking分布式系统应用程序性能监控工具-上

    概述 微服务系统监控三要素 现在系统基本都是微服务架构,对于复杂微服务链路调用如下问题如何解决? 一个请求经过了这些服务后其中出现了一个调用失败的问题,如何定位问题发生的地方? 如何计算每个节点访问流 ...

  7. 闭包类型(Fn,FnMut,FnOnce)和move关键字

    move关键字是强制让环境变量的所有权转移到闭包中而不管是不是发生了所有权的转移 move关键字和匿名函数是否是FnOnce没有必然联系,之和匿名函数体有关 当匿名函数体里转移了环境变量的所有权的时候 ...

  8. 《Python编程:从入门到实践》第十八章笔记:Django最基本用法笔记

    最近在看Python编程:从入门到实践,这是这本书"项目3 Web应用程序"第18章的笔记.记录了django最基本的一些日常用法,以便自己查阅. 可能是我的这本书版本比较老,书上 ...

  9. 获取字典中values值中最大的数,返回对应的keys

    1.字典中键值对的获取 print(data.values()) # 查看字典的值 print(data.keys()) # 查看字典的key 2.对字典中的值进行排序 sorted(data.val ...

  10. [Linux] 如何在 Linux 电脑上制作专业的视频教程

    目录 前言 1.软件工具准备 a. 录音软件 b. 录屏软件 c. 摄像头软件 d. 安卓屏幕操作软件 e. 视频剪辑软件 2.视频教程制作 3.效果 参考链接 前言 博主使用 Arch Linux ...