728x90
Open CV에 필요한 라이브러리
elipse2Poly: 타원의 자취를 직선으로 그
복합적으로 실행
rotated rectangle, 각도 0~90도로 다양하게 실행 후 결과 확인
fillpoly: 채워진 다각형을 그리는 함수
fillConvexPoly, fillPoly 모두 연
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에서 발생한 마우스 이벤트 처리 핸들러 함수 설정
- cv2.setMouseCallback(windowName, callback, param=None)
트랙바 만들기
이미지 알파 블렌딩
numpy와 opencv 자료형
numpy.ndarray로 영상 표현
- 영상의 숙성도 numpy.ndarray 관련 함수 사용
화소 접근 방법
레나 흑백 이미지 데이터 1차원 벡터로 만든 후 다시 2차원 벡터로 변경 후 이미지 생성
728x90
'부트캠프 > 컴퓨터 비전' 카테고리의 다른 글
[OpenCV] 코너 검출 (0) | 2022.11.15 |
---|---|
[OpenCv] Border Type, Blur Filter (0) | 2022.11.11 |
[Open CV] 이미지 정규화(Normalize), 임계값 분석 (0) | 2022.11.08 |
[OpenCV] 영상 접근법/영상처리 알고리즘 및 응용 (0) | 2022.11.07 |
영상 처리 개요 및 OpenCV 소개 (0) | 2022.11.04 |