teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

プログラムを追記

2021/07/05 12:31

投稿

Bull
Bull

スコア986

answer CHANGED
@@ -12,4 +12,33 @@
12
12
  ```Python
13
13
  img = cv2.imread(r"C:\2020-03\IMG_2052.JPG")
14
14
  ```
15
- としてみてはどうでしょうか?
15
+ としてみてはどうでしょうか?
16
+
17
+ ---
18
+ 以下追記
19
+
20
+ 当方の環境 (Windows10/Python 3.8.10) で数カ所の修正で正常動作を確認しました。
21
+ ```Python
22
+ import cv2
23
+ import numpy as np
24
+ from matplotlib import pyplot as plt
25
+
26
+ def main():
27
+ img = cv2.imread(r"C:\2020-03\IMG_2052.JPG")
28
+
29
+ gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
30
+
31
+ hist, bins = np.histogram(gray.ravel(),256,[0,256])
32
+
33
+ print(hist)
34
+
35
+ plt.xlim(0,255)
36
+ plt.plot(hist)
37
+ plt.xlabel("pixel value", fontsize=20)
38
+ plt.ylabel("Number of pixels", fontsize=20)
39
+ plt.grid()
40
+ plt.show()
41
+
42
+ if __name__ == "__main__":
43
+ main()
44
+ ```