avatar
Published on

Computer Vision 01) - Image Representation

Author
  • avatar
    Name
    yceffort

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')

./images/01.png

# ํŠน์ • ์ขŒํ‘œ์˜ 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)

../images/02.png

## ๊ฐ๊ฐ์˜ 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')

../images/03.png

Color Threshold, Bluescreen

ํŒŒ๋ž€์ƒ‰ ํฌ๋กœ๋งˆํ‚ค์— ์žˆ๋Š” ์‚ฌ์ง„์„ ํ•ฉ์„ฑํ•˜๋Š” ๊ณผ์ •์ด๋‹ค.

https://ak5.picdn.net/shutterstock/videos/548875/thumb/1.jpg

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)

../images/04.png

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')

../images/05.png

# ์ด๋ฏธ์ง€๋ฅผ ์นดํ”ผํ•œ๋‹ค์Œ
masked_image = np.copy(image_copy)
# ๋งˆ์Šคํ‚น ์˜์—ญ์„ ์ œ์™ธํ•˜๊ณ  ๋ชจ๋‘ ๊ฒ€์ •์ƒ‰์œผ๋กœ ๋ฐ”๊พผ๋‹ค๋ฉด
masked_image[mask != 0] = [0, 0, 0]
plt.imshow(masked_image)

../images/06.png

ํšจ๊ณผ์ ์œผ๋กœ ํŒŒ๋ž€์ƒ‰ ์˜์—ญ์„ ์ œ๊ฑฐํ•œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๊ธฐ์— ๋ฐฐ๊ฒฝ์„ ํ•ฉ์„ฑํ•ด๋ณด์ž.

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)

../images/07.png

# ๋‘ ์ด๋ฏธ์ง€๋ฅผ ํ•ฉ์น˜๋ฉด
complete_image = masked_image + crop_background
plt.imshow(complete_image)

../images/08.png