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
提示エラーはmask_xの中の値のひとつが2356で配列の範囲外を参照しているためですが
ProjectOntoCylinder関数をはじめとした提示コードについて動作、使い方など詳細を記載ください。
(ご自身で書かれたものでなければ出典も)
> 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」が計算される過程のどこかに誤りがあります
あなたの回答
tips
プレビュー