회원 로그인
|
OpenCV 9-2. 색 추적
https://blog.naver.com/samsjang OpenCV의 강좌를 공부 차원에서 재정리한 것입니다. |
필요환경 : python 3.6.x, openCV 3.2.0+contrib-cp36
웹캠으로 전송되는 영상을 아래와 같은 기능을 수행하는 코드
- 웹캠으로 전송되는 비디오 영상을 분석하여 Blue, Green, Red 생상을 가진 물제만 따로 보이도록 한다.
- 프로그램은 총 4개의 화면으로 구성
- 오리지널 비디오
- Blue 색상을 가진 물체만 보이는 화면
- Green 색상을 가진 물체만 보이는 화면
- Red 색상을 가진 물체만 보이는 화면
색상의 다향성으로 그 범위가 있다. 적절하게 범위를 설정하게 된다.
HSV 색공간 Blue(120, 255, 255), Green(60, 255, 255), Red(0, 255, 255) 기준으로 아래의 범위를 적용한다.
- (110, 100, 100) ~ (130, 255, 255) 범위는 Blue로 판단
- (50, 100, 100) ~ (70, 255, 255) 범위는 Green으로 판단
- (-10, 100, 100) ~ (10, 255, 255) 범위는 Red로 판단
위 범위를 좁히면 B, G, R을 보다 까다롭게 추출할 것이고, 범위는 넓히면 그 반대이다.
# -*- coding: utf-8 -*- # opencv_013-2.py # https://blog.naver.com/samsjang/220504633218 # 영상 속도 빠를 떄 https://codeday.me/ko/qa/20190531/676666.html # 영상 처리 최적화 https://pgmaru.tistory.com/51 # (https://worldawesome.tistory.com/1 # https://museguy.tistory.com/16 # https://darkpgmr.tistory.com/50 # https://darkpgmr.tistory.com/102) import numpy as np import cv2 import time def tracking(): try: cap = cv2.VideoCapture(0) #cap = cv2.VideoCapture('images/The Food Thief RIGG - Technical breakdown on Vimeo.mp4') start = time.time() counter = 0 print('Drived the camera.') except: print('Do not find the camera.') return while True: ret, frame = cap.read() if not ret: print('Error read video.') break # BGR을 HSV로 변환 # 비디오 프레임을 hsv 색공간으로 변경 hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # HSV에서 BGR로 가정할 범위 설정 # blue의 HSV는 [120, 255, 255] lower_blue = np.array([110, 100, 100]) upper_blue = np.array([130, 255, 255]) # green의 HSV는 [60, 255, 255] lower_green = np.array([50, 100, 100]) upper_green = np.array([70, 255, 255]) # red의 HSV는 [0, 255, 255] lower_red = np.array([-10, 100, 100]) upper_red = np.array([10, 255, 255]) """ lower_blue = np.array([90, 50, 50]) upper_blue = np.array([150, 255, 255]) lower_green = np.array([30, 50, 50]) upper_green = np.array([90, 255, 255]) lower_red = np.array([0, 50, 50]) upper_red = np.array([0, 255, 255]) """ # HSV 이미지에서 파랑색, 초록색, 빨강색만 추출하기 위한 범위값 # 범위 값 이외에 색은 0(검정색)으로 채우게 된다. mask_blue = cv2.inRange(hsv, lower_blue, upper_blue) mask_green = cv2.inRange(hsv, lower_green, upper_green) mask_red = cv2.inRange(hsv, lower_red, upper_red) # mask와 원본 이미지를 비트 연산 res1 = cv2.bitwise_and(frame, frame, mask=mask_blue) res2 = cv2.bitwise_and(frame, frame, mask=mask_green) res3 = cv2.bitwise_and(frame, frame, mask=mask_red) # 출력 cv2.imshow('Original', frame) cv2.imshow('Blue', res1) cv2.imshow('Green', res2) cv2.imshow('Red', res3) # 영상 속도가 빨라 프레임 확인을 위한 # 25프레임에 한번씩 time 출력 counter += 1 if counter %25 == 0: print('time', time.time() - start) k = cv2.waitKey(1) & 0xFF if k == 27: break cv2.destroyAllWindows() # 조금 기다려야 카메라가 뜬다. tracking()
댓글 0개
| 엮인글 0개
285개(1/15페이지)
번호 | 제목 | 글쓴이 | 조회 | 날짜 |
---|---|---|---|---|
285 | [Python] 동적 import - 모듈을 변수로 받아오기 | 푸딩뱃살 | 405 | 2022.10.27 10:45 |
284 | [Python] 파이썬 3.7.7과 3.9.7의 os.path.expanduser() 차이 | 푸딩뱃살 | 440 | 2022.08.18 12:22 |
283 | [Python] error: Microsoft Visual C++ 9.0 is required. | 푸딩뱃살 | 659 | 2022.08.03 13:35 |
282 | [Python] pyscript | 푸딩뱃살 | 434 | 2022.06.09 11:21 |
281 | [Python] float is / float not is | 푸딩뱃살 | 583 | 2022.03.02 15:03 |
280 | [Python] 이터널 문자열 f | 푸딩뱃살 | 811 | 2022.01.27 16:35 |
279 | [Python] is와 ==의 차이 | 푸딩뱃살 | 475 | 2021.11.25 15:54 |
278 |
[Python] Error: ImportError: file |
푸딩뱃살 | 891 | 2021.11.16 11:24 |
277 | [Python] 파이썬 디컴파일 - uncompyle6 | 푸딩뱃살 | 750 | 2021.11.10 14:46 |
276 | [Python] 파이썬 확장자 설명 | 푸딩뱃살 | 538 | 2021.11.03 14:38 |
275 | [참고] 웹 fbx 뷰어 | 푸딩뱃살 | 456 | 2021.10.19 15:46 |
274 | [Python] enumerate() | 푸딩뱃살 | 487 | 2021.10.13 14:44 |
273 | [Python] 아나콘다에서 가상 환경 | 푸딩뱃살 | 696 | 2020.11.21 00:26 |
272 | [Python] pip로 설치 때 퍼미션 에러 | 푸딩뱃살 | 1247 | 2020.06.06 17:13 |
271 | [Python] OpenCV 10-3. 이미지 Thresholding - Otsu's Binarizatio | 푸딩뱃살 | 657 | 2020.06.05 14:01 |
270 | [Python] OpenCV 10-2. 이미지 Thresholding - Adaptive Threshold | 푸딩뱃살 | 678 | 2020.06.05 13:58 |
269 | [Python] OpenCV 10-1. 이미지 Thresholding | 푸딩뱃살 | 569 | 2020.06.05 13:56 |
>> | [Python] OpenCV 9-2. 색 추적 | 푸딩뱃살 | 745 | 2020.06.02 23:29 |
267 | [Python] OpenCV 9-1. 색공간 바꾸기 | 푸딩뱃살 | 630 | 2020.06.02 23:27 |
266 | [Python] OpenCV 8-3. 이미지 비트 연산 | 푸딩뱃살 | 512 | 2020.06.02 23:21 |