• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

OpenCV基础操作_图片读取和保存

武飞扬头像
孤独的小丑
帮助1

目录

1 图片读取

2 图片保存


1 图片读取

在OpenCV中,加载图片采用imread()函数。

函数详细说明在:Reading and Writing Images and Video — OpenCV 2.4.13.7 documentation

Python:cv2.imread(filename[, flags]) 

函数功能:

imread 功能是加载图像文件成为一个 Mat 对象。

imread支持的文件类型有:

  • Windows bitmaps - *.bmp, *.dib (always supported)
  • JPEG files - *.jpeg, *.jpg, *.jpe (see the Notes section)
  • JPEG 2000 files - *.jp2 (see the Notes section)
  • Portable Network Graphics - *.png (see the Notes section)
  • Portable image format - *.pbm, *.pgm, *.ppm (always supported)
  • Sun rasters - *.sr, *.ras (always supported)
  • TIFF files - *.tiff, *.tif (see the Notes section)

参数说明:

        第一个参数表示图像文件名称;

        第二个参数表示加载的图像是什么类型,支持常见的三个参数值。可以直接使用数字来代替(1,0,-1),默认值为IMREAD_UNCHANDED.

  •  IMREAD_UNCHANDED(<0) 表示加载原图,不做任何改变 , 包括alpha通道,可以直接写-1
  •  IMREAD_GRAYSCALE(0) 表示把原图作为灰度图像加载进来。
  •  IMREAD_COLOR(>0) 表示默认参数,读入一副彩色图片,忽略alpha通道

接下来,我们分别用三个Flag加载图片:

  1.  
    import cv2
  2.  
     
  3.  
    img = cv2.imread("./1.jpeg")
  4.  
     
  5.  
    # 打印图像类型
  6.  
    print(type(img))
  7.  
    print(img)
  8.  
     
  9.  
    cv2.imshow("image",img)
  10.  
     
  11.  
    cv2.waitKey(0)
  12.  
     
  13.  
    cv2.destroyAllWindows()

按任意键退出弹出的画面。

原图:

学新通

 IMREAD_UNCHANDED图如下:

学新通

  1.  
    <class 'numpy.ndarray'>
  2.  
    [[[248 201 140]
  3.  
    [248 201 140]
  4.  
    [248 201 140]
  5.  
    ...
  6.  
    [244 222 204]
  7.  
    [244 222 204]
  8.  
    [244 222 204]]
  9.  
     
  10.  
    [[248 201 140]
  11.  
    [248 201 140]
  12.  
    [248 201 140]
  13.  
    ......

 IMREAD_GRAYSCALE图和打印如下:

学新通

  1.  
    <class 'numpy.ndarray'>
  2.  
    [[188 188 188 ... 219 219 219]
  3.  
    [188 188 188 ... 219 219 219]
  4.  
    [188 188 188 ... 218 218 218]
  5.  
    ...
  6.  
    [ 41 34 25 ... 121 121 123]
  7.  
    [ 55 49 41 ... 136 134 135]
  8.  
    [ 77 70 59 ... 125 126 129]]

IMREAD_COLOR图和打印如下:

学新通

  1.  
    <class 'numpy.ndarray'>
  2.  
    [[[248 201 140]
  3.  
    [248 201 140]
  4.  
    [248 201 140]
  5.  
    ...
  6.  
    [244 222 204]
  7.  
    [244 222 204]
  8.  
    [244 222 204]]
  9.  
     
  10.  
    [[248 201 140]
  11.  
    [248 201 140]
  12.  
    [248 201 140]
  13.  
    ...

2 图片保存

图片保存使用的是imwrite()或是SaveImage()

Python:cv2.imwrite(filename, img[, params]) 

Python:cv.SaveImage(filename, image)

原函数定义如下:Reading and Writing Images and Video — OpenCV 2.4.13.7 documentation

函数功能:

      将mat格式转换为image文件保存,支持的格式和imread一致。

函数参数:

      filename:保存的文件名。后缀名将会被自动用来表示标示文件类型。

      image:图片数据

      parems:表示为特定格式保存的参数编码,它有默认值。参数是以参数对的方式进行设置的。

  • 对于JPEG格式的图片,这个参数表示从0-100的图片质量(CV_IMWRITE_JPEG_QUALITY),默认值是95.

  • 对于PNG格式的图片,这个参数表示压缩级别(CV_IMWRITE_PNG_COMPRESSION)从0-9.较高的值意味着更小的尺寸和更长的压缩时间而默认值是3.

  • 对于PPM,PGM或PBM格式的图片,这个参数表示一个二进制格式标志(CV_IMWRITE_PXM_BINARY),取值为0或1,而默认值为1.

下面保存成jpg的图片看看:

  1.  
    import cv2
  2.  
     
  3.  
    img = cv2.imread("./1.jpeg")
  4.  
     
  5.  
    # 打印图像类型
  6.  
    print(type(img))
  7.  
    print(img)
  8.  
     
  9.  
    cv2.imshow("image",img)
  10.  
     
  11.  
    cv2.waitKey(0)
  12.  
     
  13.  
    cv2.destroyAllWindows()
  14.  
     
  15.  
    cv2.imwrite("demo_output_qd.jpg",img)
  16.  
    cv2.imwrite("demo_output_Q10.jpg",img,[int(cv2.IMWRITE_JPEG_QUALITY),10])
  17.  
    cv2.imwrite("demo_output_Q100.jpg",img,[int(cv2.IMWRITE_JPEG_QUALITY),100])
学新通

在同级目录下面新增加了3个文件:

学新通

比如:CV_IMWRITE_JPEG_QUALITY为10:

学新通

而当这个值为100时候:

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfjjibe
系列文章
更多 icon
同类精品
更多 icon
继续加载