opencv基础-图像操作
matplotlib 中是 RGB 顺序
opencv 中是 BGR 顺序
【例2.1】 使用Numpy库生成一个元素值都是0的二维数组,用来模拟一幅黑色图像,并对其进行访问、修改
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
img = np.zeros((5,5), dtype=np.uint8)
print('img=\n', img)
plt.subplot(1, 2, 1)
plt.imshow(img, cmap='gray')
img[:,3] = 255
print('img_modifier=\n', img)
plt.subplot(1, 2, 2)
plt.imshow(img, cmap='gray')
plt.show();
plt.show()
img=
[[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]]
img_modifier=
[[ 0 0 0 255 0]
[ 0 0 0 255 0]
[ 0 0 0 255 0]
[ 0 0 0 255 0]
[ 0 0 0 255 0]]
【例2.2】读取一个灰度图像,并对其像素进行访问、修改。
import cv2 as cv
lenna = cv.imread('../img/lenna.bmp', cv.IMREAD_GRAYSCALE)
plt.subplot(121),plt.imshow(lenna, cmap='gray')
lenna[100:200, 80:100] = 255
plt.subplot(122),plt.imshow(lenna, cmap='gray')
plt.show()
【例2.3】使用Numpy生成三维数组,用来观察三个通道值的变化情况。
import numpy as np
import matplotlib.pyplot as plt
blue = np.zeros((300, 300, 3), dtype=np.uint8)
blue[:, :, 0] = 255
print('blue:\n', blue)
green = np.zeros((300, 300, 3), dtype=np.uint8)
green[:, :, 1] = 255
print('green:\n', green)
red = np.zeros((300, 300, 3), dtype=np.uint8)
red[:, :, 2] = 255
print('red:\n', red)
plt.subplot(1, 3, 1), plt.imshow(blue)
plt.subplot(1, 3, 2), plt.imshow(green)
plt.subplot(1, 3, 3), plt.imshow(red)
plt.show()
blue:
[[[255 0 0]
[255 0 0]
[255 0 0]
...
[255 0 0]
[255 0 0]
[255 0 0]]
[[255 0 0]
[255 0 0]
[255 0 0]
...
[255 0 0]
[255 0 0]
[255 0 0]]
[[255 0 0]
[255 0 0]
[255 0 0]
...
[255 0 0]
[255 0 0]
[255 0 0]]
...
[[255 0 0]
[255 0 0]
[255 0 0]
...
[255 0 0]
[255 0 0]
[255 0 0]]
[[255 0 0]
[255 0 0]
[255 0 0]
...
[255 0 0]
[255 0 0]
[255 0 0]]
[[255 0 0]
[255 0 0]
[255 0 0]
...
[255 0 0]
[255 0 0]
[255 0 0]]]
green:
[[[ 0 255 0]
[ 0 255 0]
[ 0 255 0]
...
[ 0 255 0]
[ 0 255 0]
[ 0 255 0]]
[[ 0 255 0]
[ 0 255 0]
[ 0 255 0]
...
[ 0 255 0]
[ 0 255 0]
[ 0 255 0]]
[[ 0 255 0]
[ 0 255 0]
[ 0 255 0]
...
[ 0 255 0]
[ 0 255 0]
[ 0 255 0]]
...
[[ 0 255 0]
[ 0 255 0]
[ 0 255 0]
...
[ 0 255 0]
[ 0 255 0]
[ 0 255 0]]
[[ 0 255 0]
[ 0 255 0]
[ 0 255 0]
...
[ 0 255 0]
[ 0 255 0]
[ 0 255 0]]
[[ 0 255 0]
[ 0 255 0]
[ 0 255 0]
...
[ 0 255 0]
[ 0 255 0]
[ 0 255 0]]]
red:
[[[ 0 0 255]
[ 0 0 255]
[ 0 0 255]
...
[ 0 0 255]
[ 0 0 255]
[ 0 0 255]]
[[ 0 0 255]
[ 0 0 255]
[ 0 0 255]
...
[ 0 0 255]
[ 0 0 255]
[ 0 0 255]]
[[ 0 0 255]
[ 0 0 255]
[ 0 0 255]
...
[ 0 0 255]
[ 0 0 255]
[ 0 0 255]]
...
[[ 0 0 255]
[ 0 0 255]
[ 0 0 255]
...
[ 0 0 255]
[ 0 0 255]
[ 0 0 255]]
[[ 0 0 255]
[ 0 0 255]
[ 0 0 255]
...
[ 0 0 255]
[ 0 0 255]
[ 0 0 255]]
[[ 0 0 255]
[ 0 0 255]
[ 0 0 255]
...
[ 0 0 255]
[ 0 0 255]
[ 0 0 255]]]
【例2.4】使用Numpy生成一个三维数组,用来观察三个通道值的变化情况。
import numpy as np
import matplotlib.pyplot as plt
img = np.zeros((300, 300, 3), dtype=np.uint8)
img[:, 0:100, 0] = 255
img[:, 100:200, 1] = 255
img[:, 200:300, 2] = 255
plt.imshow(img)
plt.show()
【例2.5】使用Numpy生成一个三维数组,用来模拟一幅BGR模式的彩色图像,并对其进行访问、修改
import numpy as np
import matplotlib.pyplot as plt
import cv2 as cv
img = np.zeros((2, 4, 3), dtype=np.uint8)
print('img:\n',img)
img[0, 3] = 255
img[0, 1] = [66, 67, 88]
img[1, 1, 1] = 255
img[1, 2, 2] = 0
img[0, 2, 0] = 255
print(img)
plt.imshow(img)
plt.show()
img:
[[[0 0 0]
[0 0 0]
[0 0 0]
[0 0 0]]
[[0 0 0]
[0 0 0]
[0 0 0]
[0 0 0]]]
[[[ 0 0 0]
[ 66 67 88]
[255 0 0]
[255 255 255]]
[[ 0 0 0]
[ 0 255 0]
[ 0 0 0]
[ 0 0 0]]]
【例2.6】读取一幅彩色图像,并对其像素进行访问、修改。
import cv2 as cv
import matplotlib.pyplot as plt
lenna = cv.imread('../img/lenna_color.png')
lenna = lenna[:, :, [2, 1, 0]]
lenna[100:300, 100:200] = 255
lenna[300:400, 200:300] = 0
lenna[400:500, 300:400, 1] = 0
plt.imshow(lenna)
plt.show()
【例2.7】使用Numpy生成一个二维随机数组,用来模拟一幅灰度图像,并对其像素进行访问、修改。
import cv2 as cv
import matplotlib.pyplot as plt
img = np.random.randint(0, 100, size=[10, 10], dtype=np.uint8)
print(img)
print(img.item(3, 2))
img.itemset((3, 2), 255)
plt.imshow(img, cmap='gray')
plt.show()
[[87 88 13 62 8 78 24 39 77 92]
[77 77 65 93 95 64 69 78 15 89]
[40 27 43 79 35 46 7 73 87 54]
[69 41 70 81 19 71 52 70 82 34]
[42 82 68 24 19 83 84 72 1 29]
[ 6 89 61 40 5 47 30 17 44 25]
[42 65 43 33 56 49 21 68 57 76]
[60 89 72 22 56 68 92 16 34 45]
[ 3 44 26 20 95 22 31 31 40 34]
[42 94 5 97 77 60 18 54 7 75]]
70
【例2.8】生成一个灰度图像和彩色图像,让其中的像素值均为随机数。
import matplotlib.pyplot as plt
img = np.random.randint(0, 256, size=[256, 256], dtype=np.uint8)
print(img)
plt.subplot(121),plt.imshow(img, cmap='gray')
img2 = np.random.randint(0, 255, size=[256, 256, 3], dtype=np.uint8)
plt.subplot(122),plt.imshow(img2)
plt.show()
[[ 93 42 178 ... 132 236 35]
[246 237 132 ... 59 80 60]
[ 7 236 177 ... 102 202 134]
...
[162 217 73 ... 244 216 67]
[105 73 15 ... 151 144 206]
[ 93 68 85 ... 136 77 88]]
【例2.9】获取图像lena的脸部信息,并将其显示出来。
import cv2 as cv
import matplotlib.pyplot as plt
lenna = cv.imread('../img/lenna_color.png')
lenna = lenna[:, :, [2, 1, 0]]
face = lenna[220:400, 250:350]
mask_face = lenna.copy()
mask_face[220:400, 250:350] = np.random.randint(0, 256)
plt.subplot(131),plt.axis('off'),plt.imshow(lenna)
plt.subplot(132),plt.axis('off'),plt.imshow(face)
plt.subplot(133),plt.axis('off'),plt.imshow(mask_face)
plt.show()
【例2.10】将一幅图像内的ROI复制到另一幅图像内。
import cv2 as cv
import matplotlib.pyplot as plt
lenna = cv.imread('../img/lenna512.bmp', cv.IMREAD_GRAYSCALE)
face_lenna = lenna[220:400, 250:350]
dollar = cv.imread('../img/dollar.bmp', cv.IMREAD_GRAYSCALE)
face_mask = dollar.copy()
face_mask[160:340, 200:300] = face_lenna
plt.figure(figsize=(8,6))
plt.subplot(131),plt.axis('off'),plt.title('lenna'),plt.imshow(lenna, cmap='gray')
plt.subplot(132),plt.axis('off'),plt.title('dollar'),plt.imshow(dollar, cmap='gray')
plt.subplot(133),plt.axis('off'),plt.title('face_mask'),plt.imshow(face_mask, cmap='gray')
plt.show()
【例2.11】编写程序,使用函数cv2.split()拆分图像通道, 演示使用函数cv2.merge()合并通道。
import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
lenna = cv.imread('../img/lenna_color.png', cv.IMREAD_COLOR)
#lenna = lenna[:, :, [2, 1, 0]]
b, g, r = cv.split(lenna)
channel = np.zeros((512,512), dtype=np.uint8)
img_b = cv.merge([channel, channel, b])
img_g = cv.merge([channel, g, channel])
img_r = cv.merge([r, channel, channel])
img_merge_bgr = cv.merge([r, g, b]) #plt画图是RGB
img_add = img_b+img_g+img_r
plt.figure(figsize=(8, 6))
plt.subplot(2,3,1),plt.axis('off'),plt.title('b'),plt.imshow(b, cmap='gray')
plt.subplot(2,3,2),plt.axis('off'),plt.title('g'),plt.imshow(g, cmap='gray')
plt.subplot(2,3,3),plt.axis('off'),plt.title('r'),plt.imshow(r, cmap='gray')
plt.subplot(2,3,4),plt.axis('off'),plt.title('img_b'),plt.imshow(img_b)
plt.subplot(2,3,5),plt.axis('off'),plt.title('img_g'),plt.imshow(img_g)
plt.subplot(2,3,6),plt.axis('off'),plt.title('img_r'),plt.imshow(img_r)
plt.show()
plt.subplot(1,2,1),plt.axis('off'),plt.title('img_merge_bgr'),plt.imshow(img_merge_bgr)
plt.subplot(1,2,2),plt.axis('off'),plt.title('img_add'),plt.imshow(img_add)
plt.show()
希望能一辈子学习,希望能学习一辈子。
(^ _ ^)