タイトルの通り、pythonでmatplotlibを使って画像のアルファチャンネルを3D描画したいのですが、エラーが出てしまいます。
python
1#!/usr/bin/env python 2#-*- coding:utf-8 -*- 3 4import os, sys 5import cv2 6import numpy as np 7from mpl_toolkits.mplot3d import Axes3D 8import matplotlib.pyplot as plt 9 10image = sys.argv[1] 11#img = cv2.imread(image, -1) 12img = cv2.imread(image, cv2.IMREAD_UNCHANGED) 13 14class PlotAlpha: 15 def __init__(self, image): 16 self.image = image 17 self.img = img 18 self.width = 0 19 self.height = 0 20 self.channel = 0 21 self.alpha_array = np.zeros((self.height, self.width)) 22 23 def w_h_c(self, img): 24 if len(img.shape) == 3: 25 self.height, self.width, self.channels = img.shape[:3] 26 else: 27 self.height, self.width = img.shape[:2] 28 self.channels = 1 29 print self.width, self.height 30 31 def mpl(self): 32 x = np.arange(0, self.width, 0.25) 33 y = np.arange(0, self.height, 0.25) 34 for i in range(0, self.height): 35 for j in range(0, self.width): 36 Z = self.alpha_array[i][j] = img[i][j][3] 37 38 X, Y = np.meshgrid(x, y) 39 #Z = self.alpha_array[X][Y] 40 41 fig = plt.figure() 42 ax = Axes3D(fig) 43 ax.plot_wireframe(X,Y,Z) 44 45 plt.show() 46 47def main(): 48 PA = PlotAlpha(image) 49 PA.w_h_c(img) 50 PA.mpl() 51 52if __name__ == '__main__': 53 main()
result:
$python plt_alpha.py apple_0000.png 72 70 Traceback (most recent call last): File "plt_alpha.py", line 54, in <module> main() File "plt_alpha.py", line 51, in main PA.mpl() File "plt_alpha.py", line 37, in mpl Z = self.alpha_array[i][j] = img[i][j][3] IndexError: index 0 is out of bounds for axis 0 with size 0
となってしまいます。
プログラミングは勉強中で非常にお見苦しいコードとなっていますが、どうかご勘弁ください。
どなたかお詳しいお方がいらっしゃいましたらお力添えをお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。