numpyの二次元配列で例えば、256256のものがあるとします。
これを図に表すと正方形の図として、可視化できると思いますが、それを画像拡大・縮小のように、128128に変えたいのですが、色々調べても分からず…。画像の拡大縮小なら多くの関数があったのですが。
説明が下手でわかりにくいかも知れませんが、よろしくお願いいたします…!
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/17 02:06

回答3件
0
ベストアンサー
numpy配列を画像処理系のライブラリの型に変換し、縮小してからnumpy配列に戻せば良いでしょう。
python
1import numpy as np 2from PIL import Image 3 4a = np.arange(8*8).reshape((8,8)) 5print(a) 6i = Image.fromarray(np.uint8(a)) 7a = np.asarray(i.resize((4,4))) # フィルタ等はお好みで 8print(a) 9 10""" => 11[[ 0 1 2 3 4 5 6 7] 12 [ 8 9 10 11 12 13 14 15] 13 [16 17 18 19 20 21 22 23] 14 [24 25 26 27 28 29 30 31] 15 [32 33 34 35 36 37 38 39] 16 [40 41 42 43 44 45 46 47] 17 [48 49 50 51 52 53 54 55] 18 [56 57 58 59 60 61 62 63]] 19[[ 9 11 13 15] 20 [25 27 29 31] 21 [41 43 45 47] 22 [57 59 61 63]] 23"""
参考:
NumPyのarrayとPILの変換 - white wheelsのメモ
Python, Pillowで画像を一括リサイズ(拡大・縮小) | note.nkmk.me
投稿2018/10/17 01:20
編集2018/10/17 01:21総合スコア30939
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。

0
コメント欄で言及している論文は Unlabeled Samples Generated by GAN
Improve the Person Re-identification Baseline in vitro でしょうか?
生成した画像を (128, 128, 3) を (256, 256, 3) にリサイズすると書いてありますね
1枚の画像をリサイズするサンプル
TensorFlow / Keras であれば、tf.image.resize_bilinear() という関数があるので、これでミニバッチをまとめてリサイズできます。
python
1import cv2 2import tensorflow as tf 3import numpy as np 4 5# 入力画像作成 6img = np.random.randint(0, 10, (128, 128, 3), dtype=np.uint8) 7 8# OpenCV によるリサイズ 9cv_resized = cv2.resize(img, (256, 256), interpolation=cv2.INTER_LINEAR) 10print(cv_resized.shape) # (256, 256, 3) 11 12# TensorFlow によるリサイズ 13x = tf.placeholder(tf.float32) 14resize = tf.image.resize_bilinear(x, (256, 256)) 15with tf.Session() as sess: 16 tf_resized = sess.run(resize, feed_dict={x: img[np.newaxis, ...]}) 17 tf_resized = tf_resized[0] 18print(tf_resized.shape) # (256, 256, 3) 19 20# OpenCV と TensorFlow のリサイズ結果が一致するかどうか 21print(np.any(np.isclose(cv_resized, tf_resized))) # True
速度
- N枚の画像を1枚ずつ cv.resize() でリサイズする。
- Tensorflow の tf.image.resize_bilinear() でN枚の画像をまとめてリサイズする。
python
1import time 2import cv2 3import numpy as np 4import tensorflow as tf 5 6# 入力画像作成 7num_imgs = 100 8img_batch = np.random.randint(0, 10, (num_imgs, 128, 128, 3), dtype=np.uint8) 9 10# OpenCV によるリサイズ 11#------------------------------------ 12start = time.time() 13for img in img_batch: 14 cv2.resize(img, (256, 256), interpolation=cv2.INTER_LINEAR) 15end = time.time() - start 16print('resize {} images by using cv2.resize(): {:.4f} s'.format(num_imgs, end)) 17 18# TensorFlow によるリサイズ 19#------------------------------------ 20x = tf.placeholder(tf.float32) 21resize = tf.image.resize_bilinear(x, (256, 256)) 22 23start = time.time() 24with tf.Session() as sess: 25 sess.run(resize, feed_dict={x: img_batch}) 26end = time.time() - start 27print('resize {} images by using tf.image.resize_bilinear(): {:.4f} s'.format(num_imgs, end))
resize 100 images by using cv2.resize(): 0.0144 s resize 100 images by using tf.image.resize_bilinear(): 0.0330 s
OpenCV のほうが早い結果になりました。
TensorFlow でもリサイズは GPU ではなく、CPU で行っているのではないでしょうか?
OpenCV はこうした処理はかなり最適化されてるので、OpenCV のほうが早いのでしょう。
投稿2018/10/17 03:48
編集2018/10/17 03:49総合スコア21960
0
こうですか?
A = np.empty((256,256),float) #Aに値を代入(省略) #128×128の配列BにAを圧縮して代入する B = np.empty((128,128),float) for i in range(128): for j in range(128): B[i][j]=0 for k in range(2): for l in range(2): B[i][j]=B[i][j]+A[2*i+k][2*j+l]/4
投稿2018/10/16 16:58
編集2018/10/16 16:59総合スコア284
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。