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

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

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

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

0回答

309閲覧

IndexError: index 2356 is out of bounds for axis 1 with size 2356のエラーを解決したい

fireworks

総合スコア10

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2022/10/18 03:44

IndexError: index 2356 is out of bounds for axis 1 with size 2356のエラーを解決したい

マスクを作る過程で上記のエラーが出てしまい、shapeをプリントしたりと確認しましたが誤りがわからなかったため投稿しました。

実現したいこと

私は今画像のimage stitchingを行っています。今実行しているプログラムでマスクを作る過程でエラーが出てしまいました。

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

(5840185,) [ 787 788 789 ... 2234 2235 2236] (3024, 2356, 3) (3024, 2356, 3) --------------------------------------------------------------------------- IndexError Traceback (most recent call last) /var/folders/fm/c943c_r93bzd8zk1ht89x4dr0000gn/T/ipykernel_2496/3081542039.py in <module> 5 BaseImage, _, _ = ProjectOntoCylinder(Images[0]) 6 for i in range(1, len(Images)): ----> 7 StitchedImage = StitchImages(BaseImage, Images[i]) 8 9 BaseImage = StitchedImage.copy() /var/folders/fm/c943c_r93bzd8zk1ht89x4dr0000gn/T/ipykernel_2496/3935820449.py in StitchImages(BaseImage, SecImage) 9 print(SecImage_Mask.shape) 10 print(SecImage_Cyl.shape) ---> 11 SecImage_Mask[mask_y, mask_x,:] = 255 12 13 # Finding matches between the 2 images and their keypoints IndexError: index 2356 is out of bounds for axis 1 with size 2356

該当のソースコード

python

1def ProjectOntoCylinder(InitialImage): 2 global w, h, center, f 3 h, w = InitialImage.shape[:2] 4 center = [w // 2, h // 2] 5 f = 1100 # 1100 field; 1000 Sun; 1500 Rainier; 1050 Helens 6 7 # Creating a blank transformed image 8 TransformedImage = np.zeros(InitialImage.shape, dtype=np.uint8) 9 10 # Storing all coordinates of the transformed image in 2 arrays (x and y coordinates) 11 AllCoordinates_of_ti = np.array([np.array([i, j]) for i in range(w) for j in range(h)]) 12 ti_x = AllCoordinates_of_ti[:, 0] 13 ti_y = AllCoordinates_of_ti[:, 1] 14 15 # Finding corresponding coordinates of the transformed image in the initial image 16 ii_x, ii_y = Convert_xy(ti_x, ti_y) 17 18 # Rounding off the coordinate values to get exact pixel values (top-left corner) 19 ii_tl_x = ii_x.astype(int) 20 ii_tl_y = ii_y.astype(int) 21 22 # Finding transformed image points whose corresponding 23 # initial image points lies inside the initial image 24 GoodIndices = (ii_tl_x >= 0) * (ii_tl_x <= (w-2)) * \ 25 (ii_tl_y >= 0) * (ii_tl_y <= (h-2)) 26 27 # Removing all the outside points from everywhere 28 ti_x = ti_x[GoodIndices] 29 ti_y = ti_y[GoodIndices] 30 31 ii_x = ii_x[GoodIndices] 32 ii_y = ii_y[GoodIndices] 33 34 ii_tl_x = ii_tl_x[GoodIndices] 35 ii_tl_y = ii_tl_y[GoodIndices] 36 37 # Bilinear interpolation 38 dx = ii_x - ii_tl_x 39 dy = ii_y - ii_tl_y 40 41 weight_tl = (1.0 - dx) * (1.0 - dy) 42 weight_tr = (dx) * (1.0 - dy) 43 weight_bl = (1.0 - dx) * (dy) 44 weight_br = (dx) * (dy) 45 46 TransformedImage[ti_y, ti_x, :] = ( weight_tl[:, None] * InitialImage[ii_tl_y, ii_tl_x, :] ) + \ 47 ( weight_tr[:, None] * InitialImage[ii_tl_y, ii_tl_x + 1, :] ) + \ 48 ( weight_bl[:, None] * InitialImage[ii_tl_y + 1, ii_tl_x, :] ) + \ 49 ( weight_br[:, None] * InitialImage[ii_tl_y + 1, ii_tl_x + 1, :] ) 50 51 52 # Getting x coorinate to remove black region from right and left in the transformed image 53 min_x = min(ti_x) 54 55 # Cropping out the black region from both sides (using symmetricity) 56 TransformedImage = TransformedImage[:, min_x : -min_x, :] 57 58 return TransformedImage, ti_x-min_x, ti_y 59 60def StitchImages(BaseImage, SecImage): 61 # Applying Cylindrical projection on SecImage 62 SecImage_Cyl, mask_x, mask_y = ProjectOntoCylinder(SecImage) 63 print(mask_x.shape) 64 print(mask_y) 65# print(SecImage_Cyl) 66 # Getting SecImage Mask 67 SecImage_Mask = np.zeros(SecImage_Cyl.shape, dtype=np.uint8) 68 print(SecImage_Mask.shape) 69 print(SecImage_Cyl.shape) 70 SecImage_Mask[mask_y, mask_x,:] = 255

試したこと

中身、shapeを確認

補足情報(FW/ツールのバージョンなど)

python3.7

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

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

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

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

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

can110

2022/10/18 04:33

提示エラーはmask_xの中の値のひとつが2356で配列の範囲外を参照しているためですが ProjectOntoCylinder関数をはじめとした提示コードについて動作、使い方など詳細を記載ください。 (ご自身で書かれたものでなければ出典も)
jbpb0

2022/10/18 06:47 編集

> print(SecImage_Mask.shape) が > (3024, 2356, 3) なら、 > SecImage_Mask[mask_y, mask_x,:] = 255 の「mask_x」の範囲は0〜2355です (2356はダメ) それなのに、そこに「2356」が入ってるため、 > IndexError: index 2356 is out of bounds for axis 1 with size 2356 となってます > SecImage_Mask[mask_y, mask_x,:] = 255 の「mask_x」は、 > SecImage_Cyl, mask_x, mask_y = ProjectOntoCylinder(SecImage) から来てますので、「def ProjectOntoCylinder(InitialImage):」の > return TransformedImage, ti_x-min_x, ti_y の「ti_x-min_x」がそうですから、「ti_x」か「min_x」が計算される過程のどこかに誤りがあります
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問