python-opencv(cv2) 之一 图像的简单读取

源码:

#!/usr/bin/env python
#coding=utf-8
__author__ = 'zhangdebin'

import cv2

if __name__ == '__main__':
    #image read
    image = cv2.imread("/Users/zhangdebin/Documents/checkFace2.jpg")
    image0 = cv2.imread("/Users/zhangdebin/Documents/checkFace2.jpg",0)
    image1 = cv2.imread("/Users/zhangdebin/Documents/checkFace2.jpg",1)

    print image
    cv2.namedWindow("the window")
    cv2.imshow("the window", image)
    cv2.namedWindow("the window0")
    cv2.imshow("the window0", image0)
    cv2.namedWindow("the window1")
    cv2.imshow("the window1", image1)

    cv2.waitKey(0)

其中,cv2是我在mac系统中安装的python-opencv包,具体安装方法在另一篇博文有介绍,
cv2.imread和matlab中的imread函数很像,其中源码有一个参数imread(…)
imread(filename[, flags]) -> retval,falgs经测试,当缺省时为原图像,当falgs=0时,因为只读取第一个字节,彩色图像读取为灰度图像,当falgs逐渐增加,向7(0~7,8位)靠拢时,字节数增加,图像色彩越来越靠近原图像,当超过8位,比如8,变为(8-8=0),又是只取一个字节,视觉效果为灰度图,同理,15位为原图,有兴趣的朋友可以自己测试下,具体效果见下图:
实例效果
同时,cv2中自带了falgs =cv2.CV_LOAD_IMAGE_GRAYSCALE或
cv2.CV_LOAD_IMAGE_COLOR,其实这两个参数和上面是一样的,0为grayimage,但是color=1,而且显示时候也不是彩色的,这和matlab的imread有很大区别

    grayimage = cv2.imread('/Users/zhangdebin/Documents/checkoutFace2.jpeg',cv2.CV_LOAD_IMAGE_GRAYSCALE);
     colorimage = cv2.imread('/Users/zhangdebin/Documents/checkoutFace2.jpeg', cv2.CV_LOAD_IMAGE_COLOR)
     print 'cv2.grayscale:'
    print cv2.CV_LOAD_IMAGE_GRAYSCALE
    print 'colr:'
    print cv2.CV_LOAD_IMAGE_COLOR

cv2.grayscale:
0
colr:
1

posted on 2015-05-21 15:46  决心1119  阅读(1973)  评论(0编辑  收藏  举报

导航