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 | 图片路径(图片路径+图片名) |
param2 | imread处理的图片 |
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 | 打开图片的标题 |
param2 | imread处理的图片 |
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