부트캠프/컴퓨터 비전

[Open CV] 도형 그리기

례지 2022. 11. 4. 17:43
728x90
Open CV에 필요한 라이브러리 

elipse2Poly: 타원의 자취를 직선으로 그

polylines를 따로 써도 가능
polylines를 붙여써도 가능하다.

복합적으로 실행

rotated rectangle, 각도 0~90도로 다양하게 실행 후 결과 확인

0도
색상은 랜덤
각도별, 색상별 사각형 그리기

fillpoly: 채워진 다각형을 그리는 함수
fillConvexPoly, fillPoly 모두 연

b,g,r을 지정해서 색상을 지정할 때 숫자를 따로 안넣어도 된다.

putText: 문자열을 출력하는 함수
putText, getTextSize 연습

이벤트 처리
  • 키보드 이벤트 처리
    • cv2.waitKey(): 키보드로부터 입력된 키 값을 받음 - 1바이트
    • cv2.waitKeyEx(): 키보드로부터 입력된 키 값을 받음 - 2바이트

위 아래 이동이 이상함.
방향키를 사용해 원 움직이기

import numpy as np

class move_circle:
    def __init__(self, width, height, x, y, R):
        self.width = width
        self.height = height
        self.x = x
        self.y = y
        self.R = R
        self.left, self.right, self.down, self.up = 2, 0, 1, 3
        self.direction = self.down
        self.key = 0

    def key_input(self):
        key = cv2.waitKeyEx(1000)
        if key == 0x1B:
            return 0
        elif key == 0x270000: #right
            mc.direction = 0
            return 1
        elif key == 0x280000: #up
            mc.direction = 3
            return 1
        elif key == 0x250000: #left
            mc.direction = 2
            return 1
        elif key == 0x260000: #down
            mc.direction = 1
            return 1
        elif key == 0x2e0000: #del
            cv2.destroyAllWindows()
            return 0
        
        
    def direction_change(self):
        if self.direction == self.up:
            self.x, self.y = (self.x, self.y + 20)
        elif self.direction == self.down:
            self.x, self.y = (self.x, self.y -20)
        elif self.direction == self.right:
            self.x, self.y = (self.x + 20, self.y)
        elif self.direction == self.left:
            self.x, self.y = (self.x - 20, self.y)
            
    def detect_wall(self):
        #벽에 부딪혔을 때
        if 0 > (self.x - self.R) or self.width < (self.x + self.R):
            self.direction = abs(self.direction - 2)
            self.direction_change()
        elif 0 > (self.y - self.R) or self.height < (self.y + self.R):
            self.direction = abs(self.direction - 3) + 1
            self.direction_change()
            
    def on_mouse(self, event, x, y, flags, param):
        if event == cv2.EVENT_LBUTTONDOWN:
            if flags & cv2.EVENT_FLAG_SHIFTKEY:
                cv2.circle(param[0], (x, y), 10, (0, 0, 255), 2)
            else:
                cv2.circle(param[0], (x, y), 10, (255, 0, 0), 2)
        elif event == cv2.EVENT_RBUTTONDOWN:
            cv2.rectangle(param[0], (x, y), (x + 10, y + 10), (0, 0, 0))
        cv2.imshow('img', param[0])
        
mc = move_circle(512, 512, 256, 256, 50)


while(True):
    if mc.key_input() == 0:
        break
    
    mc.direction_change()
    
    mc.detect_wall()
        
    img = np.zeros(shape = (mc.width, mc.height, 3), dtype = np.uint8) + 255
    cv2.circle(img, center = (mc.x, mc.y), radius = mc.R, color = (0, 0, 255), thickness = 4)
    cv2.startWindowThread()
    cv2.imshow('Moving', img)
  • 마우스 이벤트 처리
    • cv2.setMouseCallback(windowName, callback, param=None)
      • windowName에서 발생한 마우스 이벤트 처리 핸들러 함수 설정

트랙바 만들기

이미지 알파 블렌딩

numpy와 opencv 자료형

numpy.ndarray로 영상 표현

  • 영상의 숙성도 numpy.ndarray 관련 함수 사용
화소 접근 방법

레나 흑백 이미지 데이터 1차원 벡터로 만든 후 다시 2차원 벡터로 변경 후 이미지 생성

원본 이미지
1차원으로 바꾼 후 다시 2차원으로 만든 이미지

 

728x90