Interface編集部
8-4RectangleIN.py
000: import cv2
001: import numpy as np
002: import random
003:
004: def __main():
005: img = createBaseImage()
006: org = img.copy()
007:
008: gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
009: # ここで処理を実行
010: rect = cv2.boundingRect(array=gray)
011: cv2.rectangle(org, rect, (0, 255, 255))
012:
013: cv2.imshow(‘Final result’, org)
014: cv2.waitKey(0)
015:
016: cv2.destroyAllWindows()
017:
018: # ノイズの描画
019: def __setNoise(img, color):
020: height, width, channels = img.shape[:3]
021:
022: for num in range(50):
023: x = int(random.uniform((width / 2) – (width / 4), (width / 2) + (width / 4)))
024: y = int(random.uniform((height / 2) – (height / 4), (height / 2) + (height / 4)))
025: angle = (x, y, 5, 5)
026: cv2.rectangle(img, angle, color, -1)
027:
028: return img
029:
030: def createBaseImage():
031: size = (480, 640, 3) # 縦480ピクセル 横640ピクセル 3チャンネル
032: color = (0, 0, 0)
033: # 黒で塗りつぶしたMat画像を生成
034: img = np.full(size, color, dtype=np.uint8)
035:
036: color = (255, 255, 255)
037: img = __setNoise(img, color)
038:
039: return img
040:
041: if __name__ == ‘__main__’:
042: print(cv2.__version__)
043:
044: __main()