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

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

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

MatplotlibはPythonのおよび、NumPy用のグラフ描画ライブラリです。多くの場合、IPythonと連携して使われます。

NumPy

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

OpenCV

OpenCV(オープンソースコンピュータービジョン)は、1999年にインテルが開発・公開したオープンソースのコンピュータビジョン向けのクロスプラットフォームライブラリです。

Python

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

Q&A

解決済

1回答

500閲覧

pythonでmatplotlibを使って画像のアルファチャンネルを3D描画したい

TANUKIpro

総合スコア14

Matplotlib

MatplotlibはPythonのおよび、NumPy用のグラフ描画ライブラリです。多くの場合、IPythonと連携して使われます。

NumPy

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

OpenCV

OpenCV(オープンソースコンピュータービジョン)は、1999年にインテルが開発・公開したオープンソースのコンピュータビジョン向けのクロスプラットフォームライブラリです。

Python

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

0グッド

0クリップ

投稿2018/03/30 10:24

タイトルの通り、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

となってしまいます。
プログラミングは勉強中で非常にお見苦しいコードとなっていますが、どうかご勘弁ください。

どなたかお詳しいお方がいらっしゃいましたらお力添えをお願いいたします。

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

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

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

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

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

guest

回答1

0

自己解決

以下でできました。
改善点は山ほどありますが、とりあえず形にはなったのでこの質問は解決したということで終わらせてもらいます。

ありがとうございました。

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 22 def w_h_c(self, img): 23 if len(img.shape) == 3: 24 self.height, self.width, self.channels = img.shape[:3] 25 else: 26 self.height, self.width = img.shape[:2] 27 self.channels = 1 28 print self.width, self.height 29 30 def mpl(self): 31 self.alpha_array = np.zeros((self.height, self.width), dtype = 'uint8') 32 x = np.arange(0, self.width, 1) 33 y = np.arange(0, self.height, 1) 34 for i in range(0, self.height): 35 for j in range(0, self.width): 36 self.alpha_array[i][j] = img[i][j][3] 37 38 X, Y = np.meshgrid(x, y) 39 Z = self.alpha_array 40 41 fig = plt.figure() 42 ax = Axes3D(fig) 43 ax.scatter(X, Y, Z) 44 45 plt.title(image) 46 plt.show() 47 48def main(): 49 PA = PlotAlpha(image) 50 PA.w_h_c(img) 51 PA.mpl() 52 53if __name__ == '__main__': 54 main()

投稿2018/03/30 11:45

TANUKIpro

総合スコア14

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問