개발 공부

2강 선 원 박스 그리기 본문

AI/AI_Python_OPENCV 기초

2강 선 원 박스 그리기

아이셩짱셩 2018. 8. 7. 21:28

# USAGE

# python drawing.py


# Import the necessary packages

#numpy는 파이션을 위한 수학 라이브러리라 생각하면 된다. numpy라고 길게 쓰면 힘드니 np라고 줄여서 쓰자,.

import numpy as np

import cv2


# Initialize our canvas as a 300x300 with 3 channels,

# Red, Green, and Blue, with a black background

#np.zeros는 모두 공백으로 채운다는 의미다. 빈 종이로 만든다는 거다. 처음의 (300,300,3)은 300 * 300 픽셀 크기와 3채널(RGB 3가지 색을 사용한다는 의미)을 사용한다는 것이며 dtype는 데이터 타입을 말한다.

#데이터 타입은 종류가 많고 우리는 unit8 즉, 8bit를 정수값을 사용한다. unsigned integer를 줄여 uint라고 한 것이다.

#이 크기의 도화지를 생성한다.

canvas = np.zeros((300, 300, 3), dtype = "uint8")


# Draw a green line from the top-left corner of our canvas

# to the bottom-right

#앞서 배운대로 녹색으로 모두 채운다.

#엔터키를 치면 다음으로 넘어간다. cv2.waitKey는 키를 기다린다는 의미이며 0는 바로 처리한다는 의미다.

green = (0, 255, 0)

cv2.line(canvas, (0, 0), (300, 300), green)

cv2.imshow("Canvas", canvas)

cv2.waitKey(0)


# Now, draw a 3 pixel thick red line from the top-right

# corner to the bottom-left

red = (0, 0, 255)

cv2.line(canvas, (300, 0), (0, 300), red, 3)

cv2.imshow("Canvas", canvas)

cv2.waitKey(0)


# Draw a green 50x50 pixel square, starting at 10x10 and

# ending at 60x60

cv2.rectangle(canvas, (10, 10), (60, 60), green)

cv2.imshow("Canvas", canvas)

cv2.waitKey(0)


# Draw another rectangle, this time we'll make it red and

# 5 pixels thick

cv2.rectangle(canvas, (50, 200), (200, 225), red, 5)

cv2.imshow("Canvas", canvas)

cv2.waitKey(0)


# Let's draw one last rectangle: blue and filled in

blue = (255, 0, 0)

cv2.rectangle(canvas, (200, 50), (225, 125), blue, -1)

cv2.imshow("Canvas", canvas)

cv2.waitKey(0)


# Reset our canvas and draw a white circle at the center

# of the canvas with increasing radii - from 25 pixels to

# 150 pixels

#프로그램은 수학과 밀접한 관려이 있다. 25픽셀씩 커지는 원을 보여준다.

canvas = np.zeros((300, 300, 3), dtype = "uint8")

(centerX, centerY) = (canvas.shape[1] / 2, canvas.shape[0] / 2)

white = (255, 255, 255)


#2 버젼에서는 xrange로 사용한다.

#range(0,175)는 0 에서  175 범위를 말하며 for문을 통해 1씩 증가하여 원을 그리게 된다.

for r in range(0, 175):

   cv2.circle(canvas, (150, 150), r, white)

#for문 끝. 아래 첫줄에서 시작하므로

cv2.imshow("Canvas", canvas)

cv2.waitKey(0)


# Let's go crazy and draw 25 random circles

#난수를 발생시켜 25개의 원을 무작위로 그린다.

for i in range(0, 25):

   # randomly generate a radius size between 5 and 200,

   # generate a random color, and then pick a random

   # point on our canvas where the circle will be drawn

   #np.random.randint는 numpy의 난수 발생 방법이다. 


#    rand: 0부터 1사이의 균일 분포

 #   randn: 정규 분포

  #  randint: 균일 분포의 정수 난수

  #(시작점, 끝나는 점)으로 보통 쓰이나 randint는 numpy.random.randint(low, high=None, size=None)으로 쓰인다.

  #만약 high를 입력하지 않으면 0과 low사이의 숫자를, high를 입력하면 low와 high는 사이의 숫자를 출력한다. size는 난수의 숫자이다.

  #.tolist()는 이미지와 같은 2D와 같은 형태를 중첩 배열로 만들어준다.

  #size는 튜풀 리스트로 정의한다. 일반 배열로 정의해도 된다. 튜플은 리스트 내용을 수정할 수 없도록 한다. 리스트는 원소가 하나인 경우에라도 (3,)으로 표현한다. 일반 리스트는 [3,]로 표기한다.


   radius = np.random.randint(5, high = 200)

   color = np.random.randint(0, high = 256, size = (3,)).tolist()

   pt = np.random.randint(0, high = 300, size = (2,))


   # draw our random circle

   cv2.circle(canvas, tuple(pt), radius, color, -1)


# Show our masterpiece

cv2.imshow("Canvas", canvas)

cv2.waitKey(0)

'AI > AI_Python_OPENCV 기초' 카테고리의 다른 글

6강  (0) 2018.08.07
5강  (0) 2018.08.07
4강 이미지 회전  (0) 2018.08.07
3강  (0) 2018.08.07
1강  (0) 2018.08.07
Comments