opencv和numpy数据格式的转换

opencv和numpy数据格式的转换

  1. opencv的image数据格式就是用numpy unit8 格式存储的。两者之间可以相互装换;
  2. 想用imshow格式输出,array格式一定用转换为uint8的格式。用array.astype(np.uint8)强制转换为uint8的格式。
  3. 对于彩色图像有三个通道,每一个通道都是一样的操作,最后用cv2.merge(r,g,b)函数将三个通道的值何在一起就行。
import cv2 as cv
import numpy as np

filename = 'A.jpg'
img = cv.imread(filepath)
print(img.dtype)
print('img.shape',img.shape)
print(type(img))
img = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
cv.imshow("original image", img)
# r = np.random.randint(0,255,(535, 574),dtype=np.uint8)
# g = np.random.randint(0,255,(535, 574),dtype=np.uint8)
# b = np.random.randint(0,255,(535, 574),dtype=np.uint8)
gray = np.random.randn(535, 574)
gray.astype(np.uint8)
gray = ((gray+1)/2)*255
# img = cv.merge(r, g, b)
cv.imshow('test', gray)
cv.imshow('subtract', img-gray)
cv.waitKey(0)
cv.destroyWindow('test')

你可能感兴趣的:(numpy,python,opencv,numpy)