やりたいこと
pythonでの画像処理で読み込んだ画像の色情報を変更して描画する。
発生している問題
matplotlibで画像をプロットしてクリックした地点の色を別の色に変更しようとした際、中途半端に変更される。
また、クリックした地点から数ピクセル離れたところの色情報が取得される。
ソースコード
Python
1from PIL import Image 2import numpy as np 3import matplotlib.pyplot as plt 4import sys 5 6class im: 7 def __init__(self,image): 8 self.image=image 9 def onclick(self,event): 10 plist=self.image[int(event.ydata)][int(event.xdata)] 11 print(plist) 12 h, w, c = self.image.shape 13 for i in range(h): 14 for j in range(w): 15 if (self.image[i][j] == plist).all(): 16 self.image[i][j] = [255, 255, 255] 17 fig = plt.figure() 18 ax = fig.add_subplot(111) 19 ax.imshow(self.image) 20 plt.show() 21 22# 画像を読み込む 23img = Image.open( "./test.png") 24img=np.array(img) 25c_event=im(img) 26fig=plt.figure() 27ax=fig.add_subplot(111) 28ax.imshow(img) 29cid = fig.canvas.mpl_connect('button_press_event', c_event.onclick) 30plt.show() 31
試したこと
plistの色情報を手動指定
変更したいところの色が[255,0,0]の場合に、クリックしたときに取得した色情報が[255,0,0]であること、画像の変更がおかしいことを確認し、plist=[255,0,0]の固定情報になるよう書き換えて再実行したところ、うまく変更がされた。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/10 13:48
2018/11/10 23:22
2018/11/10 23:31