Interface編集部
8-4CircleIN.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: # ここで処理を実行
009: (x, y), radius = cv2.minEnclosingCircle(points=pts)
010: org = cv2.circle(org, (int(x), int(y)), int(radius), (0, 255, 0), 2)
011:
012: cv2.imshow(‘Final result’, org)
013: cv2.waitKey(0)
014:
015: cv2.destroyAllWindows()
016:
017: # ノイズの描画
018: def __setNoise(img, color):
019: global pts
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: pts = np.append(pts, np.array([[x, y]]), axis=0)
029:
030: return img
031:
032: def createBaseImage():
033: size = (480, 640, 3) # 縦480ピクセル 横640ピクセル 3チャンネル
034: color = (0., 0., 0.)
035: # 黒で塗りつぶしたMat画像を生成
036: img = np.full(size, color, dtype=np.uint8)
037:
038: color = (255, 255, 255)
039: img = __setNoise(img, color)
040:
041: return img
042:
043: if __name__ == ‘__main__’:
044: print(cv2.__version__)
045:
046: pts = np.empty((0, 2), int)
047: __main()