본문 바로가기

딥러닝/신경망 기초

이미지 데이터 다루기

이미지 데이터 다루기 (Image Handling)


간단간단하게 코드를 통해 설명하겠습니다.


아래의 패키지를 먼저 import해줍니다.


import os from scipy.misc import imread, imresize import matplotlib.pyplot as plt %matplotlib inline


이미지의 shape을 print해보기 위해 간단한 출력함수를 만들어줍니다.


def print_typeshape(img):
    print("Type is %s" % (type(img)))
    print("Shape is %s" % (img.shape,))


먼저, 저는 코드를 실행시키는 위치와 동일선상에 img 폴더(디렉토리)를 만들어 그 안에 고양이, 강아지, 젖소 등의 사진을 넣어놓았습니다.


아래를 보면,

imread로 고양이 사진을 읽어드립니다. 이렇게 되면 고양이사진은 array로 바뀝니다.(행렬로 바꾼 겁니다!!)

말그대로 im(=image) + read(=읽다)


cat = imread("img/cat.jpg")
print_typeshape(cat)

출력 : 

Type is <class 'numpy.ndarray'>
Shape is (1600, 2400, 3)


이제 한번 그려 볼까요?


plt.figure(figsize=(10,8)) # 이미지를 표현하기 위한 판떼기?정도로 보시면 될거 같아요 plt.imshow(cat) plt.grid(False) plt.title("Original Cat") plt.show()

read the image





Load + Cast to float

: 불러와서 float로 형변환을 시켜보자

: 추후에 0~1사이의 값으로 바꿔주기 위해


# Load
cat2 = imread("img/cat.jpg").astype(np.float)
print_typeshape(cat2)

출력:

Type is <class 'numpy.ndarray'> 

Shape is (1600, 2400, 3) 


# Plot
plt.figure(figsize=(10,8))
plt.imshow(cat2)
plt.grid(False)
plt.title("Original image with imread.astype(np.float)")
plt.draw()

read the image in a wrong way



아무것도 표시가 되지 않는다....왜?

integer 타입이면 0~255사이의 값을 가질 것으로 가정하고 float 타입이면 0~1 사이의 값을 가질 것으로 가정한다. 따라서 float 타입의 경우 255로 나눠줘야 한다.


# 다시 Load
cat2 = imread("img/cat.jpg").astype(np.float) / 255.
print_typeshape(cat2)

출력:

Type is <class 'numpy.ndarray'>

Shape is (1600, 2400, 3) 


# 다시 Plot
plt.figure(figsize=(10,8))
plt.imshow(cat2)
plt.grid(False)
plt.title("Original image with imread.astype(np.float)")
plt.draw()

change the image by float




Resize
: 사이즈를 조정해보자

# resize
catsmall = imresize(cat, [100,100])
print_typeshape(catsmall)

# plot
plt.figure(1)
plt.imshow(catsmall)
plt.grid(False)
plt.title("Resized image")
plt.show()

출력:

Type is <class 'numpy.ndarray'> 

Shape is (100, 100, 3) 

resize the image




GrayScale - 흑백사진으로 바꾸기

: 간단한 함수를 만들어 바꿔보자.


각 Red, Green, Blue값에 일정 값을 곱해준다.


def rgb2gray(rgb): if len(rgb.shape) is 3: # 3이면 컬러라는 의미. return np.dot(rgb[...,:3], [0.299, 0.587, 0.114]) else: print("Current image is Gray!") return rgb



catsmall_gray = rgb2gray(catsmall)
print("Size of catsmallgray is %s" % (catsmall_gray.shape,))
print("Type of catsmallgray is ", type(catsmall_gray))

출력:

Size of catsmallgray is (100, 100) 

Type of catsmallgray is <class 'numpy.ndarray'> 


plt.imshow(catsmall_gray) # cmap없으면 이상한 그림이 됨
plt.title("[imshow] Gray image")
plt.grid(False)
plt.colorbar()
plt.show()

fail to convert to gray


plt.imshow(catsmall_gray, cmap=plt.get_cmap("gray")) # cmap없으면 이상한 그림이 됨
plt.title("[imshow] Gray image")
plt.grid(False)
plt.colorbar()
plt.show()

gray-scaled image




Reshape

# Matrix to Vector
catrowvec = np.reshape(catsmall_gray, (1, -1)) # 1 x 10,000 (row:1, column : -1 mean 나머지 알아서넣어줘)
print("Size of catsmallgray is %s" % (catrowvec.shape,))
print("Type of catsmallgray is ", type(catrowvec))

# Vector to Matrix
catmatrix = np.reshape(catrowvec, (100,-1)) # 100 x 100 (row:100, column : -1 mean 나머지 알아서넣어줘)
print("Size of catsmallgray is %s" % (catmatrix.shape,))
print("Type of catsmallgray is ", type(catmatrix))
출력:
Size of catsmallgray is (1, 10000)
Type of catsmallgray is  <class 'numpy.ndarray'>
Size of catsmallgray is (100, 100) 

Type of catsmallgray is <class 'numpy.ndarray'> 


plt.imshow(catmatrix, cmap=plt.get_cmap("gray")) # cmap없으면 이상한 그림이 됨
plt.title("Reshaped Matrix")
plt.grid(False)
plt.colorbar()
plt.show()

reshaped image




Load from folder

: 폴더 안에 있는 이미지들을 확인해보자


cwd = os.getcwd()
path = cwd + "/img/"
file_list = os.listdir(path)
print("[{}] file are in [{}]".format(len(file_list), path))

출력:

[4] file are in [C:\dss\DeepLearning_beginning/img/] 


사진리스트를 뽑아보면...


for i, f in enumerate(file_list):
    print("{}. file is [{}]".format(i,f))

출력:

0. file is [cat.jpg]
1. file is [cow.jpg]
2. file is [dog.jpg] 

3. file is [horse.jpeg] 


사진파일하고 다른 파일이 섞여있다면?


# 사진 파일이 아닌 데이터 자르기
valid_exts = [".jpg",".gif",".png",".tga",".jpeg"]
images = []
names = []
for f in file_list:
    ext = os.path.splitext(f)[1]
    print(ext)
    if ext.lower() not in valid_exts:
        continue
    fullpath = os.path.join(path,f)
    images.append(imread(fullpath))
    names.append(os.path.splitext(f)[0])

출력:

.jpg 

.jpg 

.jpg 

.jpeg