前提・実現したいこと
一枚の画像をRGBからHSVに変換し、HS成分とSV成分だけを取り出した二枚の別々の画像を作成中ですが、
HS成分だけを取り出した画像はできましたが、SV成分だけの画像作成ができなくて困ってます。
発生している問題・エラーメッセージ
TypeError Traceback (most recent call last)
<ipython-input-14-7f26c00cf06d> in <module>
12 plt.subplot(1,2,1), plt.imshow(img)
13 plt.xticks([]),plt.yticks([]),plt.title('Image')
---> 14 plt.subplot(1,2,2), plt.imshow(hist,interpolation = 'nearest')
15 plt.xticks([]),plt.yticks([]),plt.title('Histogram')
16
~/miniconda3/lib/python3.7/site-packages/matplotlib/pyplot.py in imshow(X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, data, **kwargs)
2699 filternorm=filternorm, filterrad=filterrad, imlim=imlim,
2700 resample=resample, url=url, **({"data": data} if data is not
-> 2701 None else {}), **kwargs)
2702 sci(__ret)
2703 return __ret
~/miniconda3/lib/python3.7/site-packages/matplotlib/init.py in inner(ax, data, *args, **kwargs)
1808 "the Matplotlib list!)" % (label_namer, func.name),
1809 RuntimeWarning, stacklevel=2)
-> 1810 return func(ax, *args, **kwargs)
1811
1812 inner.doc = _add_data_doc(inner.doc,
~/miniconda3/lib/python3.7/site-packages/matplotlib/axes/_axes.py in imshow(self, X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, **kwargs)
5492 resample=resample, **kwargs)
5493
-> 5494 im.set_data(X)
5495 im.set_alpha(alpha)
5496 if im.get_clip_path() is None:
~/miniconda3/lib/python3.7/site-packages/matplotlib/image.py in set_data(self, A)
640 if (self._A.dtype != np.uint8 and
641 not np.can_cast(self._A.dtype, float, "same_kind")):
--> 642 raise TypeError("Image data cannot be converted to float")
643
644 if not (self._A.ndim == 2
TypeError: Image data cannot be converted to float
エラーメッセージ
### 該当のソースコード opencv-python import cv2 import numpy as np from matplotlib import pyplot as plt %matplotlib inline img = cv2.imread('hsv.jpg') hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV) img = cv2.cvtColor(img,cv2.COLOR_RGB2BGR) hist = cv2.calcHist([hsv], [1,2], None, [0,256], [0,256,0, 256]) plt.subplot(1,2,1), plt.imshow(img) plt.xticks([]),plt.yticks([]),plt.title('Image') plt.subplot(1,2,2), plt.imshow(hist,interpolation = 'nearest') plt.xticks([]),plt.yticks([]),plt.title('Histogram') cv2.imwrite("homehsv-sv.jpg",img) ### 試したこと hist = cv2.calcHist([hsv], [0,1], None, [180,256], [0,180,0,256])で HS成分だけの画像が作成できたので、以下のようにベクトルを[1,2] 色の範囲を[0,256],[1,2]のベクトルの色の範囲を[0,256,0,256]にしました ここで0はH成分、1はs成分、2はv成分を表す。 変更したらエラーが発生しました。 hist = cv2.calcHist([hsv], [1,2], None, [0,256], [0,256,0, 256]) ### 補足情報(FW/ツールのバージョンなど) ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー