본문으로 건너뛰기
yceffort
PostsSeriesTagsAbout🧪 Research
EN

Tweaks

theme
accent palette
film grain
minimal mode
BACK TO INDEX
◆ ESSAY
--min
--year
KOoriginal

mailMail icongithubtwitter
yceffort
•
© 2026
•
https://yceffort.kr
BACK TO INDEX
◆ ESSAY

Computer Vision 01) - Image Representation

avatar
yceffort
2019-04-01 · 2분
2min
2019year
KOoriginal
computer-visionpython

Image Representation & Classification

Images as Grids of Pixels

import numpy as np
from skimage import io
import matplotlib.image as mpimg

import matplotlib.pyplot as plt
import cv2

import urllib

%matplotlib inline

먼저 이미지를 불러온다.

waymo_car_url = 'https://zdnet2.cbsistatic.com/hub/i/r/2018/01/22/e270d68c-c028-421a-bc5b-5d2a9a9458d1/resize/770xauto/50e9d2f0fc86841ba455489d50651291/google-waymo-self-driving-atlanta.png'
f = urllib.request.urlopen(waymo_car_url)
image = mpimg.imread(f)

print('Image dimensions:', image.shape)
Image dimensions: (410, 770, 4)
# 이미지를 회색으로 바꾼다.
gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
plt.imshow(gray_image, cmap='gray')

# 특정 좌표의 grayscale
x = 400
y = 300

print(gray_image[y,x])
0.543404
# 맥시멈 / 미니멈 grayscale

max_val = np.amax(gray_image)
min_val = np.amin(gray_image)

print('Max: ', max_val)
print('Min: ', min_val)
Max:  0.9646824
Min:  0.040556863

RGB colorspace

plt.imshow(image)

## 각각의 RGB영역 확보
r = image[:,:,0]
g = image[:,:,1]
b = image[:,:,2]
fx, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(20,10))
ax1.set_title('R channel')
ax1.imshow(r, cmap='gray')
ax2.set_title('G channel')
ax2.imshow(g, cmap='gray')
ax3.set_title('B channel')
ax3.imshow(b, cmap='gray')

Color Threshold, Bluescreen

파란색 크로마키에 있는 사진을 합성하는 과정이다.

cromakey_blue = 'https://ak5.picdn.net/shutterstock/videos/548875/thumb/1.jpg'
f = urllib.request.urlopen(cromakey_blue)
image = mpimg.imread(f, 0)
print('This image is:', type(image), 'with dimensions:', image.shape)
This image is: <class 'numpy.ndarray'> with dimensions: (480, 852, 3)
# 이미지 복사
image_copy = np.copy(image)

# BGR이미지를 RGB로 변환
image_copy = cv2.cvtColor(image_copy, cv2.COLOR_BGR2RGB)

# 복사된 이미지 보여주기
plt.imshow(image_copy)

lower_blue = np.array([0,0,200])
upper_blue = np.array([250,250,255])

# 마스킹될 영역을 선택한다.
mask = cv2.inRange(image, lower_blue, upper_blue)

# 마스킹 이미지 표시
plt.imshow(mask, cmap='gray')

# 이미지를 카피한다음
masked_image = np.copy(image_copy)
# 마스킹 영역을 제외하고 모두 검정색으로 바꾼다면
masked_image[mask != 0] = [0, 0, 0]
plt.imshow(masked_image)

효과적으로 파란색 영역을 제거한 것을 알 수 있다. 여기에 배경을 합성해보자.

space_url = 'https://images.unsplash.com/photo-1496715976403-7e36dc43f17b?ixlib=rb-1.2.1&w=1000&q=80'
sf = urllib.request.urlopen(space_url)
image = np.asarray(bytearray(sf.read()), dtype="uint8")
background_image = cv2.imdecode(image, cv2.IMREAD_COLOR)
background_image = cv2.cvtColor(background_image, cv2.COLOR_BGR2RGB)

crop_background = background_image[0:480, 0:852]
# 이번엔 반대로 마스킹 해야할 영역을 검정색으로 처리한다.
crop_background[mask == 0] = [0, 0, 0]
plt.imshow(crop_background)

# 두 이미지를 합치면
complete_image = masked_image + crop_background
plt.imshow(complete_image)

관련 글

  • #python#backend

    [Python] Send ncloud sms message

    네이버 클라우드 플랫폼의 서비스 중 하나인 https://www.ncloud.com/product/applicationService/sens 로 SMS를 발송하는 예제. ncloud서비스를 다 써본건 아니지만, `make_signature`는 전 서비스에 다 똑같이 쓸 수 있을 것 같은 기분이다. ```python import time import req...

    2020-03-17·1분
  • #python#automation

    업무 자동화 (1) - 구글 스프레드 시트 API 활용하기

    구글 스프레드 시트를 파이썬에서 조작해보자. 내가 할일은 1. 스프레드시트를 읽고 2. 스프레드시트에 쓰는 두가지 작업이다. ```python import pickle import os.path from googleapiclient.discovery import build from google_auth_oauthlib.flow import Installe...

    2019-02-14·3분
  • #web-scraping#python

    초보를 위한 웹크롤링: 네이버 영화 댓글 크롤링하기

    e 파이썬과 파이썬 라이브러리 (beatifulSoup)를 활용하여 네이버 영화 댓글 크롤링 해보기 ## 1. 크롤링하려는 웹페이지의 구조를 살펴보기 인크레더블 평점 댓글 페이지를 먼저 살펴보겠습니다. [여기](https://movie.naver.com/movie/bi/mi/point.nhn?code=136990&onlyActualPointYn=Y#po...

    2018-11-06·14분

새 글을 놓치고 싶지 않으시다면 RSS로 구독해 주세요.

RSS 구독 →

yceffort — 프론트엔드 엔지니어입니다. 발표·기술 자문·기고 문의는 이곳에서 받고 있습니다.

← Back to the blogIssue on GitHub →