質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
NumPy

NumPyはPythonのプログラミング言語の科学的と数学的なコンピューティングに関する拡張モジュールです。

コードレビュー

コードレビューは、ソフトウェア開発の一工程で、 ソースコードの検査を行い、開発工程で見過ごされた誤りを検出する事で、 ソフトウェア品質を高めるためのものです。

Q&A

解決済

1回答

4066閲覧

エラーコード:too many indices for array: array is 2-dimensional, but 3 were indexed

tthr

総合スコア2

NumPy

NumPyはPythonのプログラミング言語の科学的と数学的なコンピューティングに関する拡張モジュールです。

コードレビュー

コードレビューは、ソフトウェア開発の一工程で、 ソースコードの検査を行い、開発工程で見過ごされた誤りを検出する事で、 ソフトウェア品質を高めるためのものです。

0グッド

1クリップ

投稿2021/12/26 07:28

前提・実現したいこと

下記のエラーが発生しました。
インデックスの数は同じだと思うんですが、何が原因なのかわかりません。
解決策はありますでしょうか。
よろしくお願いします。

発生している問題・エラーメッセージ

runfile('C:/Users/kanam/segmentation1/super resolution.py', wdir='C:/Users/kanam/segmentation1') Traceback (most recent call last): File "C:\Users\kanam\segmentation1\super resolution.py", line 74, in <module> save_images("./train_low/", image_train_filenames, image_train) File "C:\Users\kanam\segmentation1\super resolution.py", line 62, in save_images testimage = testimage[:,:,::-1]#色チャンネルをRGBからBGRへ変換 IndexError: too many indices for array: array is 2-dimensional, but 3 were indexed

該当のソースコード

import os import cv2 import numpy as np def load_images(inputpath, imagesize, type_color): imglist = [] exclude_prefixes = ('__', '.') for root, dirs, files in os.walk(inputpath): dirs[:] = [dir for dir in dirs if not dir.startswith(exclude_prefixes)] files[:] = [file for file in files if not file.startswith(exclude_prefixes)] for fn in sorted(files): bn, ext = os.path.splitext(fn) if ext not in [".bmp", ".BMP", ".jpg", ".JPG", ".jpeg", ".JPEG", ".png", ".PNG"]: continue filename = os.path.join(root, fn) if type_color == 'Color': #カラー画像の場合 testimage = cv2.imread(filename, cv2.IMREAD_COLOR) height, width = testimage.shape[:2] #サイズ変更 testimage = cv2.resize(testimage, (imagesize, imagesize), interpolation = cv2.INTER_AREA) testimage = np.asarray(testimage, dtype=np.float64) testimage = testimage[:,:,::-1] #チャンネルをBGRからRGBに変更 elif type_color == 'Gray': #グレースケール画像の場合 testimage = cv2.imread(filename, cv2.IMREAD_GRAYSCALE) height, width = testimage.shape[:2] #サイズ変更 testimage = cv2.resize(testimage, (imagesize, imagesize), interpolation = cv2.INTER_AREA) testimage = np.asarray([testimage], dtype=np.float64) testimage = np.asarray(testimage, dtype=np.float64).reshape((imagesize, imagesize, 1)) imglist.append(testimage) imgsdata = np.asarray(imglist, dtype=np.float32) return imgsdata, sorted(files) #画像リストとファイル名のリストを返す def generate_lowresolution(imagelist, scale): imagelist_out = [] for i in range(0, len(imagelist)): height, width = imagelist[i].shape[:2] image_temp = cv2.resize(imagelist[i], (round(height/scale), round(width/scale)), interpolation = cv2.INTER_AREA) image_temp = cv2.resize(image_temp, (height, width), interpolation = cv2.INTER_CUBIC) imagelist_out.append(image_temp) imgsdata = np.asarray(imagelist_out, dtype=np.float32) return imgsdata def save_images(savepath, filenamelist, imagelist): for i, fn in enumerate(filenamelist): filename = os.path.join(savepath, fn) testimage = imagelist[i] testimage = testimage[:,:,::-1]#色チャンネルをRGBからBGRへ変換 cv2.imwrite(filename, testimage) #画像読み込み image_train, image_train_filenames = load_images("./org_train_s/", 256, 'Gray') image_test, image_test_filenames = load_images("./org_test/", 256, 'Gray') #画像の低解像度化 image_train = generate_lowresolution(image_train, 3.0) image_test = generate_lowresolution(image_test, 3.0) #画像保存 save_images("./train_low/", image_train_filenames, image_train) save_images("./test_low/", image_test_filenames, image_test) IMAGE_SIZE = 256

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

python

1image_train, image_train_filenames = load_images("./org_train_s/", 256, 'Gray') 2image_test, image_test_filenames = load_images("./org_test/", 256, 'Gray')

で読み込んでいるから、カラーではないので2次元配列ですね。それの3次元目を逆向きにしようとしてエラーになっています。

投稿2021/12/26 08:38

ppaul

総合スコア24666

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tthr

2021/12/26 14:14

ありがとうございます。 助かりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問