갈루아의 반서재

scikit-image 는 이미지 처리를 위한 파이썬 라이브러리, 엔터프라이즈급 응용프로그램을 작성하는데 적합하다. 

우분투의 경우 다음과 같이 설치한다. 

1
2
3
4
5
6
7
8
9
10
11
12
(venv) fuchsia@fuchsia:~$ pip install -U scikit-image
Installing collected packages: toolz, dask, cloudpickle, scikit-image
  Found existing installation: scikit-image 0.13.1
    Not uninstalling scikit-image at /usr/lib/python3/dist-packages, outside environment /home/fuchsia/venv
    Cant uninstall 'scikit-image'. No files were found to uninstall.
Successfully installed cloudpickle-0.6.1 dask-1.0.0 scikit-image-0.14.1 toolz-0.9.0
 
(venv) fuchsia@fuchsia:~$ pip3 list
Package                 Version
----------------------- ---------
scikit-image            0.14.1
 
cs


scikit-image 는 이미지 처리 파이썬 패키지로 numpy 배열로 동작한다. skimage 로 임포트할 수 있다.


이미지 저장하고 읽기
io 모듈의 imsave() 함수를 이용해 이미지를 저장하고, imread() 함수를 이용해 읽을 수 있다.

from skimage import io
from matplotlib import pyplot as plt

img = io.imread("space_station.jpg")
io.imshow(img)
plt.show()
io.imsave("new_space_station.jpg", img)


new_img = io.imread("new_space_station.jpg")
io.imshow(new_img)
plt.show()


데이터 모듈 

scikit-image 에서는 실습에 사용할 수 있는 표준 테스트 이미지를 제공하고 있다.

from skimage import data, io
io.imshow(data.camera())
io.show()


from skimage import data, io
io.imshow(data.text())
io.show()


색상 모듈

색상 공간에서 다른 색상 공간으로 이미지를 변경하는 함수를 포함한다. rgb2gray() 함수는 RGB 에서 그레이스케일로 변환하는데 사용되고, rgb2hsv() 함수는 RGB 이미지를 HSV 이미지로 변환하는데 사용된다. 

from skimage import data, io, color
img = data.astronaut()
img_gray = color.rgb2gray(img)
io.imshow(img_gray)
io.show()
from skimage import data, io, color
img = data.astronaut()
img_hsv = color.rgb2hsv(img)
io.imshow(img_hsv)
io.show()


그리기 모듈 http://scikit-image.org/docs/dev/api/skimage.draw.html

원그리기 예를 통해 그리기 모듈에 대해 살펴보자. 

skimage.draw.circle(r, c, radius[, shape])Generate coordinates of pixels within circle.

아래는 반지름이 10, 중심좌표가 (50, 50)인 원을 그리는 코드다. np.zero() 함수를 통해 이미지 전체에 0을 할당한 후, circle() 함수를 통해 원 안에 있는 모든 픽셀 좌표를 가져온 후 1을 할당한다. 

import numpy as np
from skimage.draw import circle
img = np.zeros((100, 100), dtype=np.uint8)
rr, cc = circle(50, 50, 10)
img[rr, cc] = 1
img
Out[29]:
array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ...,
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]], dtype=uint8)
io.imshow(img)
io.show()

np.zeros() 

https://docs.scipy.org/doc/numpy/reference/generated/numpy.zeros.html

np.zeros((100,100), dtype=np.uint8)
Out[20]:
array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ...,
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]], dtype=uint8)


아래는 중심좌표 (4,4) 반지름 5인 원을 그리는 코드다.

from skimage.draw import circle
img = np.zeros((10, 10), dtype=np.uint8)
rr, cc = circle(4, 4, 5)
img[rr, cc] = 1
img
Out[32]:
array([[0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
       [0, 1, 1, 1, 1, 1, 1, 1, 0, 0],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
       [0, 1, 1, 1, 1, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)

io.imshow(img)
io.show()