opencv基礎-影象操作
阿新 • • 發佈:2020-12-13
matplotlib 中是 RGB 順序
opencv 中是 BGR 順序
【例2.1】 使用Numpy庫生成一個元素值都是0的二維陣列,用來模擬一幅黑色影象,並對其進行訪問、修改
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')
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.png', 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')
【例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)
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)
【例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)
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.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)