1 2 3 4 5 6 7 8 9 |
import numpy as np a=np.arange(8*8).reshape((8,8)) print(a) print(a[0:4, 0:4]) print(a[4:8, 0:4]) print(a[0:4, 4:8]) print(a[4:8, 4:8]) |
実行結果
オリジナル配列
[[ 0 1 2 3 4 5 6 7]
[ 8 9 10 11 12 13 14 15]
[16 17 18 19 20 21 22 23]
[24 25 26 27 28 29 30 31]
[32 33 34 35 36 37 38 39]
[40 41 42 43 44 45 46 47]
[48 49 50 51 52 53 54 55]
[56 57 58 59 60 61 62 63]]
print(a[0:4, 0:4])
[[ 0 1 2 3]
[ 8 9 10 11]
[16 17 18 19]
[24 25 26 27]]
print(a[4:8, 0:4])
[[32 33 34 35]
[40 41 42 43]
[48 49 50 51]
[56 57 58 59]]
print(a[0:4, 4:8])
[[ 4 5 6 7]
[12 13 14 15]
[20 21 22 23]
[28 29 30 31]]
print(a[4:8, 4:8])
[[36 37 38 39]
[44 45 46 47]
[52 53 54 55]
[60 61 62 63]]
ビデオから、部分画像をスライス
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import numpy as np import cv2 cap = cv2.VideoCapture(0) w = cap.get(cv2.CAP_PROP_FRAME_WIDTH) h = cap.get(cv2.CAP_PROP_FRAME_HEIGHT) fps = cap.get(cv2.CAP_PROP_FPS) while True: ret, frame = cap.read() if frame is None: print("frame is None") break width = 200 xstart = int((w-width)/2) xend = int(xstart + width) ystart = int((h-width)/2) yend = int(ystart + width) subFrame = frame[ystart:yend, xstart:xend] cv2.imshow('Frame', frame) cv2.imshow('SubFrame', subFrame) if cv2.waitKey(30) == 27: break cap.release() cv2.destroyAllWindows() |