import cv2
img=cv2.imread('e:/rotman.jpg')
img
array([[[ 14, 21, 16], [ 13, 20, 15], [ 12, 19, 14], ..., [ 22, 40, 41], [ 22, 40, 41], [ 22, 40, 41]], [[ 13, 20, 15], [ 13, 20, 15], [ 12, 19, 14], ..., [ 21, 39, 40], [ 21, 39, 40], [ 21, 39, 40]], [[ 12, 19, 14], [ 13, 20, 15], [ 13, 20, 15], ..., [ 22, 37, 39], [ 20, 38, 39], [ 20, 38, 39]], ..., [[ 26, 52, 69], [ 32, 58, 75], [ 51, 77, 94], ..., [ 32, 60, 77], [ 61, 89, 106], [ 68, 96, 113]], [[ 49, 75, 92], [ 44, 70, 87], [ 46, 72, 89], ..., [ 46, 74, 91], [ 64, 92, 109], [ 61, 89, 106]], [[ 56, 82, 99], [ 42, 68, 85], [ 35, 61, 78], ..., [ 60, 88, 105], [ 64, 92, 109], [ 49, 77, 94]]], dtype=uint8)
img.shape
(403, 432, 3)
img[100,100]
array([ 34, 76, 105], dtype=uint8)
img[100,100,1]
76
cv2.imread('e:/rotman.jpg',0).shape
(403, 432)
img2=cv2.imread('e:/cat.jpg')
import matplotlib.pyplot as plt
plt.imshow(img2)
<matplotlib.image.AxesImage at 0x21784a86a58>
type(img2)
numpy.ndarray
import matplotlib.pyplot as plt
plt.imshow(img2)
<matplotlib.image.AxesImage at 0x21784c86ac8>
plt.imshow(img)
<matplotlib.image.AxesImage at 0x21784d026d8>
img.item(10,10,2) # img.itemset((10,10,2),100)
17
img.size # 图像总像素数
522288
img.shape
(403, 432, 3)
403*432*3
522288
img.dtype
dtype('uint8')
b,g,r = cv2.split(img) #split的效率要比numpy索引差
img = cv2.merge((r,g,b))
plt.imshow(img)
<matplotlib.image.AxesImage at 0x21784d6c0b8>
b = img [:,:,0 ] #单通道
print(b.shape)
plt.imshow(b)
(403, 432)
<matplotlib.image.AxesImage at 0x21785d9c4e0>
img1 = img
replicate = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REPLICATE)# 形如 aaaaaa | abcdefgh | hhhhhhh
reflect = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REFLECT)#形如 fedcba | abcdefgh | hgfedcb
reflect101 = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REFLECT_101)# 形如 gfedcb | abcdefgh | gfedcba
wrap = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_WRAP)# 形如 cdefgh | abcdefgh | abcdefg
constant= cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_CONSTANT,value=[255,0,0])#绿色常量边框
plt.subplot(231),plt.imshow(img1,'gray'),plt.title('ORIGINAL')
plt.subplot(232),plt.imshow(replicate,'gray'),plt.title('REPLICATE')
plt.subplot(233),plt.imshow(reflect,'gray'),plt.title('REFLECT')
plt.subplot(234),plt.imshow(reflect101,'gray'),plt.title('REFLECT_101')
plt.subplot(235),plt.imshow(wrap,'gray'),plt.title('WRAP')
plt.subplot(236),plt.imshow(constant,'gray'),plt.title('CONSTANT')
plt.show()
img[100:300,100:300]=img2[50:250,70:270]
plt.imshow(img)
<matplotlib.image.AxesImage at 0x21785e1cb70>