와챠의 우당탕탕 코딩 일기장
[Open CV-Python] 5장 연습문제 7 , 8, 9번 본문
반응형
7번
다음의 컬러 영상파일을 입력받아서 RGB의 3개 채널을 분리하고, 각 채널을 컬러영상을 윈도우에 표시해보자. 즉, Red 채널은 빨간 색으로, Green 채널은 초록 색으로, Blue 채널은 파란 색으로 표현되도록 다음의 프로그램을 작성하시오.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np, cv2 | |
logo = cv2.imread("images/logo.jpg", cv2.IMREAD_COLOR) # 로고 영상 읽기 | |
if logo is None: raise Exception("영상 읽기 오류") | |
blue, green, red = cv2.split(logo) # 채널 분리 | |
img_zero = np.zeros_like(blue) # 빈 채널 생성(검정 영상) | |
blue_img = cv2.merge([blue, img_zero, img_zero]) # 파랑만 색 있고 나머지는 검정, | |
green_img = cv2.merge([img_zero, green, img_zero]) # 초록만 색 있고 나머지는 검정, | |
red_img = cv2.merge([img_zero, img_zero, red]) # 빨강만 색 있고 나머지는 검정으로 채널 합성 | |
cv2.imshow('logo', logo) | |
cv2.imshow('blue_img', blue_img) | |
cv2.imshow('green_img', green_img) | |
cv2.imshow('red_img', red_img) | |
cv2.waitKey(0) |
8번
다음 영상에서 특정 영역의 타원만을 복사하여 새 창에 표시하는 프로그램을 완성하시오.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np, cv2 | |
image = cv2.imread("images/color.jpg", cv2.IMREAD_COLOR) # 영상 읽기 | |
if image is None: raise Exception("영상 파일 읽기 오류") | |
mask = np.zeros(image.shape[:2], np.uint8) # 마스크(image와 크기가 같은 검정 영상) | |
center = (190, 170) # 타원의 중심 | |
# 타원 그리기 | |
cv2.ellipse(mask, center, (50, 90), 0, 0, 360, (255, 255, 255), -1) | |
# image에 mask 씌우기 | |
dst = cv2.add(image, image, mask=mask) | |
cv2.imshow('image', image) | |
cv2.imshow('dst', dst) | |
cv2.waitKey(0) |
9번
3행, 6열의 행렬을 생성하고, 행렬의 원소를 초기화한 후에 cv2.reduce() 함수를 이용해서 가로 방향과 세로 방향으로 감축하여 평균을 구한 결과를 출력하시오.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np, cv2 | |
# 3행 6열의 행렬을 생성하고 | |
ch = [ [1, 2, 3, 4, 5, 6], | |
[1, 2, 3, 4, 5, 6], | |
[1, 2, 3, 4, 5, 6] ] | |
# 행렬의 원소를 초기화한 후에(10으로 초기화) | |
ch = np.full((3, 6), 10, np.uint8) | |
# cv2.reduce() 함수를 이용해서 가로 방향으로 감축하여 평균 구한 결과를 출력 | |
ch1 = cv2.reduce(ch, dim=1, rtype=cv2.REDUCE_AVG) | |
print("[%s] = \n%s \n" % ('ch1', ch1)) | |
# cv2.reduce() 함수를 이용해서 세로 방향으로 감축하여 평균 구한 결과를 출력 | |
ch2 = cv2.reduce(ch, dim=0, rtype=cv2.REDUCE_AVG) | |
print("[%s] = \n%s \n" % ('ch2', ch2)) |
얼레벌레 코드
반응형
'이런 저런 공부' 카테고리의 다른 글
[openCV-Python] 7장 연습문제 14번(Canny) (0) | 2021.04.09 |
---|---|
[opevCV-Python] 6장 연습문제 9번(영상 합성) (1) | 2021.04.09 |
[OpenCV-Python] 4장 연습문제 8 ~ 18번 (0) | 2021.03.28 |
<삼성 오픈소스 컨퍼런스(SOSCON 2020)> 참여 후기 (0) | 2020.10.21 |
<Do it - 안드로이드 앱 프로그래밍> 중간 후기 (0) | 2020.08.11 |