(一)OpenCV之读写显示函数

0 环境

  • Ubuntu18.04
  • Python3.6
  • python-opencv 3.2.0

安装OpenCV

sudo apt-get install opencv-python

1 imread(param)

项目描述
功能读取图片,转为numpy.array格式.
param图片路径(路径+图片名称)

1.0 Demo

import cv2

def readImage(image_path):
	img = cv2.imread(image_path)
	print("Type of imread function processed image: {}".format(type(img)))
	print("Shape of imread funciton processed image: {}".format(img.shape))
	print("Imread function processed image: {}".format(img))

image_path = "./images/reading/Aaron_Eckhart_0001.jpg"
readImage(image_path)

1.2 Result

Type of imread function processed image: <type 'numpy.ndarray'>
Shape of imread funciton processed image: (250, 250, 3)
Imread function processed image: [[[  0   0   0]
  [  0   0   0]
  [  0   0   0]
  ...
  [  1   1   1]
  [  1   1   1]
  [  1   1   1]]

 [[  0   0   0]
  [  0   0   0]
  [  0   0   0]
  ...
  [  0   0   0]
  [  0   0   0]
  [  0   0   0]]

 [[  0   0   0]
  [  0   0   0]
  [  1   1   1]
  ...
  [  3   3   3]
  [  3   3   3]
  [  3   3   3]]

 ...

 [[149 120 105]
  [155 126 111]
  [163 134 119]
  ...
  [154 132 121]
  [166 144 133]
  [172 150 139]]

 [[148 119 104]
  [150 121 106]
  [157 128 113]
  ...
  [160 138 127]
  [173 151 140]
  [180 158 147]]

 [[144 115 100]
  [146 117 102]
  [154 125 110]
  ...
  [165 143 132]
  [177 155 144]
  [184 162 151]]]

1.3 Analysis

  • imread处理图片,将图片转换为numpy.array格式,为后续矩阵计算做准备,生成的图形矩阵为BGR格式,需要转换为RGB格式,方式有两种,一种使用cv2的函数:cv2.cvtColor(image, cv2.COLOR_BGR2RGB), 一种是直接利用图像矩阵切片:[…, ::-1];
  • 图片维度为(250, 250, 3),即图片尺寸为250x250,BGR channels=3;
  • 图片输出数组如上所示;

2 imwrite(param1, param2)

项目描述
功能按param1命名并保存图片到指定位置;
param1图片路径(图片路径+图片名)
param2imread处理的图片

2.1 Demo

import cv2
def writeImage(sourceDir, objectDir):
	img = cv2.imread(sourceDir)
	cv2.imwrite(objectDir, img)
	
sourceDir = "./images/reading/Aaron_Eckhart_0001.jpg"
objectDir = "./images/saved/processed_1.jpg"
writeImage(sourceDir, objectDir)

2.2 Result

图片以processed_1.jpg保存到路径images/saved中.

2.3 Analysis

  • imwrite读取imread处理的图片;
  • 保存路径和图片名可自定义;

3 imshow(param1, param2)

项目描述
功能显示读取的图片
param1打开图片的标题
param2imread处理的图片

3.1 Demo

import cv2
def showImage(sourceDir):
	img = cv2.imread(sourceDir)
	# 窗口名称:test
	cv2.namedWindow("test", 0)
	# 窗口大小:w:640, h:480
	cv2.resizeWindow("test", 640, 480)
	cv2.imshow("test", img)
	cv2.waitKey(5000)
sourceDir = "./images/reading/Aaron_Eckhart_0001.jpg"
showImage(sourceDir)

3.2 Result

以test为图片标题,显示图片5s.

3.3 Analysis

  • 以test为Figure名称显示图片;
  • 通过waitKey延时函数,延时5000ms即图片显示5000ms自动关闭;

[1]https://blog.csdn.net/qq_32846595/article/details/79264071

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天然玩家

坚持才能做到极致

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值