Skip to content

Instantly share code, notes, and snippets.

@takatakamanbou
Last active June 16, 2016 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takatakamanbou/35a99e3e47dbb92fa975 to your computer and use it in GitHub Desktop.
Save takatakamanbou/35a99e3e47dbb92fa975 to your computer and use it in GitHub Desktop.
視覚認知計算特論
# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import division
import numpy as np
import scipy as sp
import scipy.misc as spmisc
# 普通にPNG画像を読み込むやり方.
img = spmisc.imread( 'blackuni3.png' )
# どんな大きさでどんなデータ型の配列(NumPyのndarray)に格納されるか確認
print( 'img: ', img.shape, img.dtype )
# 画素値を反転して出力
spmisc.imsave( 'hoge.png', 255 - img )
# 左右を反転した画像と元画像の平均を出力
img2 = img[:, ::-1, :]
spmisc.imsave( 'hoge2.png', (img + img2)/2 )
# ==> 画素値がオーバーフローして化け猫化
# img を浮動小数点型に変換してやり直し
imgF = np.asarray( img, dtype = float )
print( 'imgF:', imgF.shape, imgF.dtype )
img2F = imgF[:, ::-1, :]
spmisc.imsave( 'hoge3.png', (imgF + img2F)/2 )
# ==> 今度は問題ない
# catg/catXYZ.png は...
cat = spmisc.imread( 'catg/cat000.png' )
print( 'cat: ', cat.shape, cat.dtype )
# ==> グレイスケール形式のPNG画像なので,2次元配列
# データ行列を作る & データ型を変換する
X = np.empty( ( 131, 64 * 64 ) ) # デフォルトの型すなわち float64 の配列を用意
for i in range( 131 ):
cat = spmisc.imread( 'catg/cat%03d.png' % i )
X[i, :] = cat.reshape( -1 )
print( 'X: ', X.shape, X.dtype )
# (おまけ) PNG画像を読み込むと同時にグレイスケール化
imgG = spmisc.imread( 'blackuni3.png', flatten = True )
print( 'imgG:', imgG.shape, imgG.dtype )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment