こんにちは、以前より画像の色相に関する質問をしているものです。
さて、今回画像の色相、彩度、明度が0なら、その画素数をカウントして、
それぞれ色相[0]、彩度[0]、明度[0]の部分より画素数を引くコードを作っている際に起きたエラーについてお聞きしたいです。
色相の部分では正常にコードが動いているのですが、
彩度の部分になった途端にエラーが発生します。
python
1import cv2 2import numpy as np 3dst_ = cv2.imread("/Users/hogehoge/Desktop/nekoneko_test2.jpg") 4 5height, width = dst_.shape[:2] 6dst_ = cv2.cvtColor(dst_, cv2.COLOR_BGR2HSV) 7 8hue = cv2.split(dst_)[0] 9sat = cv2.split(dst_)[1] 10val = cv2.split(dst_)[2] 11 12hue0 = 0 13sat0 = 0 14val0 = 0 15black = 0 16 17for j in range(0, height): 18 for k in range(0, width): 19 20 if dst_.item(j, k, 0) == 0: 21 hue0 = hue0 +1 22 if dst_.item(j, k ,1) == 0: 23 sat0 = sat0 +1 24 if dst_.item(j, k, 2) == 0: 25 val0 = val0 +1 26 "GIMPの提示する黒は(0, 0 ,0)なのでマスク部分をカウント" 27 if dst_.item(j, k, 0) == 0 and dst_.item(j, k ,1) == 0 and dst_.item(j, k ,2) == 0: 28 black = black +1 29 30print(hue0) 31print(sat0) 32print(val0) 33print("黒色は", black) 34 35"透明部分の画素を引いた行列を作成" 36hist_m0= cv2.calcHist([hue], [0], None, [180], [0, 180]) 37hist_m0[0] =hue0 - black 38np.savetxt('/Users/hogehoge/Desktop/hue_neko_test2.csv', hist_m0, delimiter=',') 39 40hist_m1 = cv2.calcHist([saturation], [1], None, [256], [0, 256]) 41hist_m1[0] = sat0 - black 42np.savetxt('/Users/hogehioge/Desktop/sat_neko_test2.csv', hist_m1, delimiter=',') 43 44hist_m2 = cv2.calcHist([val], [2], None, [256], [0,256]) 45hist_m2[0] = val0 - black 46np.savetxt('/Users/hogehoge/Desktop/val_neko_test2.csv', hist_m2, delimiter=',') 47 48print("行列の作成が完了した。")
これに対してエラーが、
python
1105533 2105330 3105185 4黒色は 105185 5[ 348.] 6--------------------------------------------------------------------------- 7error Traceback (most recent call last) 8<ipython-input-6-36bfb175d4c1> in <module>() 9 39 np.savetxt('/Users/hogehoge/Desktop/hue_neko_test2.csv', hist_m0, delimiter=',') 10 40 11---> 41 hist_m1 = cv2.calcHist([saturation], [1], None, [256], [0, 256]) 12 42 hist_m1[0] = sat0 - black 13 43 np.savetxt('/Users/hogehoge/Desktop/sat_neko_test2.csv', hist_m1, delimiter=',') 14 15error: C:\bld\opencv_1506447021968\work\opencv-3.3.0\modules\imgproc\src\histogram.cpp:152: error: (-215) j < nimages in function cv::histPrepareImages
となっていて、どうやらこちらのリファレンスを見ると、
- 画像の位置がおかしい。
と言っているのですが、色相が正常に処理できているため、謎が解決しません。
どうか、ご教授お願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/02/06 07:20
2018/02/06 13:49
2018/02/07 00:52