PIL과 opencv에서의 image 변환.

필요성
tensorflow 나 pytorch등에서의 이미지 로딩의 경우,
일반적으로, PIL.Image.Image를 기본적으로 이미지를 위한 class 타입으로 사용함.
from tensorflow.keras.preprocessing import image
image_tf = image.load_img('test.gif')
print(f'type : {type(image_tf)}')
결과는 다음과 같음
type : <class 'PIL.Image.Image'>
opencv or scikit-image를 이용한 전처리 수행하고 싶은 경우에는 이 두 라이브러리의 데이터 간 변환이 필요함.
위의 두 패키지 모두 NumPy의 ndarray를 사용하여 이미지를 다룸: channel 순서만 차이가 있음 (BGR or RGB)
방법
방법 자체는 매우 간단한데,opencv의 python라이브러리는 실제로 numpy의 ndarray를 기본 이미지 class로 사용하기 때문에PIL.Image.Image와 numpy의 array간의 변환을 이용하면 된다.
- 단, color image를 처리할 때 opencv의 경우 BGR Color-space를 기본으로 사용한다는 점을 주의할 것.
변환은 다음 코드를 참조하자.
import numpy as np
from PIL import Image
import cv2
def PIL2opencv(pilimage):
return cv2.cvtColor(np.array(pilimage),cv2.COLOR_RGB2BGR)
def opencv2PIL(cv2image):
return Image.fromarray(cv2image)
이들을 테스트하기 위해, 특정 url에서 image를 로드하여 opencv의 이미지 타입인 numpy.ndarray로 읽고 이들간의 변환을 해보면 다음과 같다.
import urllib
import cv2
import numpy as np
import urllib.request
from PIL import Image
url_str = 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTmYnfxeVfCSqG514OtmU_Lw4VKmqDuf-UU9A&usqp=CAU'
# url에서 byte단위로 읽어들인 경우, opencv에서 사용가능한 image 로딩하는 방법.
resp = urllib.request.urlopen(url_str)
img1D = np.asarray(bytearray(resp.read()), dtype=np.uint8)
print(f'type : {type(img1D)}, shape:{img1D.shape}')
cv2img = cv2.imdecode(img1D,cv2.IMREAD_COLOR)
print(f'type : {type(cv2img)}, shape:{cv2img.shape}')
pilimage = opencv2PIL(cv2img)
print(f'type : {type(pilimage)}')
cv2img_new = PIL2opencv(pilimage)
print(f'type : {type(cv2img_new)}')
pilimage.save('test.gif')
결과는 다음과 같다.
type : <class 'numpy.ndarray'>, shape:(9527,)
type : <class 'numpy.ndarray'>, shape:(254, 199, 3)
type : <class 'PIL.Image.Image'>
type : <class 'numpy.ndarray'>
이를 matplotlib.pyplot을 이용하여 살펴보면 다음과 같음.
import matplotlib.pyplot as plt
matimg = plt.imread('test.gif')
plt.subplot(1,3,1)
plt.imshow(matimg)
plt.axis('off')
plt.subplot(1,3,2)
plt.imshow(np.array(pilimage))
plt.axis('off')
plt.subplot(1,3,3)
plt.imshow(cv2img_new[:,:,::-1])
plt.axis('off')
plt.show()
Torch의 Tensor 와의 변환
Torch의 Tensor와의 변환
NumPy의 ndarray로 변환한 뒤 이를 기반으로 Tensor를 생성하는 방법도 많이 사용되지만,
실제 데이터 처리 파이프라인에서는 주로 torchvision.transforms 모듈에서 제공하는 conversion 관련 클래스들이 활용된다.
[PyTorch] torchvision.transforms 사용법 - transforms란?
PyTorch의 torchvision.transforms:이미지 전처리와 데이터 증강을 위한 도구torchvision.transforms는PyTorch에서 제공하는 이미지 전처리 및 data augmentation을 위한 module.이 모듈은 이미지 데이터를 이용한 딥러
ds31x.tistory.com
[PyTorch] Conversion-Torchvision.transfroms.v2
Conversion transforms은데이터 타입과 형식을 변환하며,일부는 값 범위 스케일링(예: uint8 [0,255] ↔ float32 [0,1])을 포함함.https://docs.pytorch.org/vision/main/transforms.html#conversion Transforming and augmenting images — T
ds31x.tistory.com
기타
tensorflow.keras.preprocessing.image
내부적으로 PIL을 이용하여 이미지를 로딩한다.
from tensorflow.keras.preprocessing import image
image_tf = image.load_img('test.gif')
print(f'type : {type(image_tf)}')
결과는 다음과 같음.
type : <class 'PIL.Image.Image'>
Tensorflow 라이브러리를 이용한 URL통한 이미지 로드.
tensorflow.keras.utils.get_file을 이용한다.
import tensorflow as tf
import os
import matplotlib.pyplot as plt
url_str = 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTmYnfxeVfCSqG514OtmU_Lw4VKmqDuf-UU9A&usqp=CAU'
bname = os.path.basename(url_str)
bname = bname.split('?')[0]
img_url = tf.keras.utils.get_file(bname, origin=url_str)
pilimg = tf.keras.preprocessing.image.load_img(img_url, target_size=(224,224))
print(img_url) #/root/.keras/datasets/img
os.remove(img_url) # Remove the cached file
#plt.imshow(np.array(pilimg))
#plt.axis('off')
#plt.show()
결과는 다음과 같음.
Downloading data from https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTmYnfxeVfCSqG514OtmU_Lw4VKmqDuf-UU9A&usqp=CAU
16384/9527 [===================================================] - 0s 0us/step
24576/9527 [=============================================================================] - 0s 0us/step
/root/.keras/datasets/images
같이 보면 좋은 자료들
[Python] PIL, Pillow, OpenCV, and Scikit-image
PIL, Pillow, OpenCV, and Scikit-imagePython에서 이미지를 다룰 때 이용되는 주요 패키지들은 다음과 같음.1.PIL (Python Imaging Library)PIL은 1995년에 처음 개발된 Python의 최초 이미지 처리 라이브러리 중 하나임.
ds31x.tistory.com
[DL] Dataset: CIFAR-10
CIFAR-10Machine Learning 과 Computer Vision 의 학습에서 널리 사용되는 image dataset.캐나다 토론토 대학교의 Alex Krizhevsky, Vinod Nair, Geoffrey Hinton에 의해 만들어짐.구성 및 classes1.Data 구성:이미지 크기: 32x32 Pix
ds31x.tistory.com
[PyTorch] Conversion-Torchvision.transfroms.v2
Conversion transforms은데이터 타입과 형식을 변환하며,일부는 값 범위 스케일링(예: uint8 [0,255] ↔ float32 [0,1])을 포함함.https://docs.pytorch.org/vision/main/transforms.html#conversion Transforming and augmenting images — T
ds31x.tistory.com
'Programming > DIP' 카테고리의 다른 글
| [OpenCV] cv2.cvtColor (0) | 2022.07.14 |
|---|---|
| [Math] Hessian: Summary (0) | 2022.06.05 |
| Conda backup for DIP 2021 (0) | 2021.09.18 |
| NumPy : sum, mean, std, and so on (0) | 2021.09.17 |
| NumPy 검색 (0) | 2021.09.11 |