OpenCV+Python图像读写

2018-07-04  本文已影响157人  音符纸飞机

写图像

cv2.imwrite(filename, img)

色彩空间转换

cv2.cvtColor(src_img, color_convertion_code)

在cv2中列出了所有支持的code,以“COLOR_”开头

读图像

import cv2

img = cv2.imread("laugh.jpg")
cv2.imshow("laugh", img)
cv2.waitKey()
laugh

样例

import cv2
import numpy as np

img_write = np.zeros((100, 100), np.uint8)
print(img_write.shape)
cv2.imwrite("black_square_gray.jpg", img_write)
img_write = cv2.cvtColor(img_write, cv2.COLOR_GRAY2BGR)
print(img_write.shape)
cv2.imwrite("black_square_color.jpg", img_write)

img_read = cv2.imread("black_square_gray.jpg")
print(img_read.shape)

img_read = cv2.imread("black_square_gray.jpg", cv2.IMREAD_GRAYSCALE)
print(img_read.shape)

img_read = cv2.imread("black_square_color.jpg")
print(img_read.shape)

img_read = cv2.imread("black_square_color.jpg", cv2.IMREAD_GRAYSCALE)
print(img_read.shape)

'''
(100, 100)
(100, 100, 3)
(100, 100, 3)
(100, 100)
(100, 100, 3)
(100, 100)
'''

默认情况下,imread()返回的图像是BGR格式。也可以指定格式,在cv2中列出了所有支持的格式,以"IMREAD_"开头。

上一篇 下一篇

猜你喜欢

热点阅读