얼굴의 광도 맵을 나타내는 0과 1 사이의 4096 개의 데이터 포인트를 포함하는 M 개의 벡터가 있습니다. 이것은 실제 이미지의 샘플입니다.
이제 내 목적은 그것들을 시각적으로 시각적으로 표현하는 것이지만 그렇게하려면 이미지를 나타내는 PIL 객체를 제공해야합니다. 이것은 내 MVC입니다.
import PIL.Image as pilim
import matplotlib.cm as cm
import numpy as np
greys = cm.get_cmap('Greys')
num_images = 10
num_faces = faces.shape[0]
sample_images = np.random.choice(num_faces, num_images, replace=False)
for index in sample_images:
greyscale = np.apply_along_axis(greys, 0, faces[index]).reshape((64, 64, 4))
im = pilim.fromarray(greyscale, mode='RGBA')
im.save('test{}.png'.format(index)) greys = cm.get_cmap('Greys')
Faces는 698 개의 샘플을 포함하는 ndarray입니다. 다음 샘플과 같은 것
[[0.01617647 0.01617647 0.01617647 ... 0. 0. 0. ]
[0.01617647 0.01617647 0.01617647 ... 0. 0. 0. ]
[0.01617647 0.01617647 0.01617647 ... 0. 0. 0. ]]
그리고 이것은 나의 우울한 결과입니다
PIL 은 픽셀 데이터와 함께 작동하므로 각 RGBA는 0에서 255 사이의 값입니다. 컬러 맵 기본값은 0-1 범위의 RGBA 값을 생성합니다. 이를 변환하려면 다음과 같이 255를 곱하고 8 비트 부호없는 정수 (uint8)로 변환 할 수 있습니다.
greyscale = np.uint8(cmap(faces[index].reshape(64,64)) * 255)
그러나 matplotlib의 컬러 맵은 이러한 바이트를 직접 생성하는 매개 변수도 지원합니다.
greyscale = cmap(faces[index].reshape(64,64), bytes=True)
나중에 배열의 형태를 (64,64,4)로 변경할 수 있지만 컬러 맵을 적용하기 전에 변환을 수행하는 것이 더 쉽고 읽기 쉽습니다.
이러한 유형의 이미지에 대해 여러 가지 순차적 인 컬러 맵 을 선택할 수 있습니다. _r
이름에를 추가하면 반전 컬러 맵이 제공됩니다 (너무 어둡고 밝은 반전).
시작하기위한 몇 가지 코드는 다음과 같습니다.
import PIL.Image as pilim
import matplotlib.cm as cm
import numpy as np
from matplotlib import pyplot as plt
cmap = cm.get_cmap('copper_r') # 'bone_r', 'Greys', 'copper_r', 'Purple', ...
num_images = 1
faces = np.tile(np.linspace(0,1,4096), 698).reshape(698, 4096)
num_faces = faces.shape[0]
sample_images = np.random.choice(num_faces, num_images, replace=False)
print(sample_images)
for index in sample_images:
greyscale = cmap(faces[index].reshape(64,64), bytes=True)
im = pilim.fromarray(greyscale, mode='RGBA')
im.save(f'test{index}.png')
추신 : imsave
matplotlib에는 코드를 더욱 단순화 하는 함수 도 있습니다.
for index in sample_images:
plt.imsave(f'test{index}.png', faces[index].reshape(64,64), cmap=cmap)
이미지가 거꾸로 표시되면에 추가 origin='lower'
하면 imsave
반전됩니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다