回答編集履歴

3

フルパス指定について追記

2017/11/29 05:55

投稿

can110
can110

スコア38262

test CHANGED
@@ -91,3 +91,15 @@
91
91
  # 略
92
92
 
93
93
  ```
94
+
95
+
96
+
97
+ なお、フルパスで指定する場合は、`r"c:\~"`と記載してください。
98
+
99
+ (パス文字中の`\`を`\`とエスケープしてもよいですが、面倒なため)
100
+
101
+ ```
102
+
103
+ im1 = cv2.imread( r"C:\Users\wakimoto\testimg\Tulips.jpg", 0);
104
+
105
+ ```

2

パス確認コード追記

2017/11/29 05:55

投稿

can110
can110

スコア38262

test CHANGED
@@ -69,3 +69,25 @@
69
69
  cv2.error: ......\modules\highgui\src\window.cpp:281: error: (-215) size.width>0 && size.height>0 in function cv::imshow
70
70
 
71
71
  ```
72
+
73
+
74
+
75
+ まずファイルパスおよびファイルを存在するかを確認することが先決です。
76
+
77
+ ```Python
78
+
79
+ import os
80
+
81
+ path1 = 'lena.jpg'
82
+
83
+ path1 = os.path.abspath(path1)
84
+
85
+ print(path1) # c:\hoge\huga\lena.jpg など意図したパスであること
86
+
87
+ print(os.path.exists(path1)) # Trueであること
88
+
89
+ im1 = cv2.imread(path1,0)
90
+
91
+ # 略
92
+
93
+ ```

1

検証コード追記

2017/11/28 07:25

投稿

can110
can110

スコア38262

test CHANGED
@@ -3,3 +3,69 @@
3
3
  `im1 = cv2.imread("Tulips.jpg",0)`の後に`print(im1)`してみて結果が`None`表示されませんか?
4
4
 
5
5
  ファイルパスを`"/hoge/huga/Tulips.jpg"`などのようにフルパスで指定してみてください。
6
+
7
+
8
+
9
+ 検証コード
10
+
11
+ ```Python
12
+
13
+ import cv2
14
+
15
+ def main():
16
+
17
+ im1 = cv2.imread("存在しないファイル.jpg",0)
18
+
19
+ im2 = cv2.imread("存在しないファイル2.jpg",0)
20
+
21
+ print(im1)
22
+
23
+
24
+
25
+ hist1 = cv2.calcHist([im1],[0],None,[256],[0,256])
26
+
27
+ hist2 = cv2.calcHist([im2],[0],None,[256],[0,256])
28
+
29
+
30
+
31
+ d = cv2.compareHist(hist1,hist2,0)
32
+
33
+ print(d)
34
+
35
+
36
+
37
+ cv2.imshow('test',im1)
38
+
39
+
40
+
41
+ if __name__ == "__main__":
42
+
43
+ main()
44
+
45
+ ```
46
+
47
+
48
+
49
+ 実行結果例
50
+
51
+ ```
52
+
53
+ None
54
+
55
+ 1.0
56
+
57
+ OpenCV Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow, file ......\modules\highgui\src\window.cpp, line 281
58
+
59
+ Traceback (most recent call last):
60
+
61
+ File "C:\hoge.py", line 19, in <module>
62
+
63
+ main()
64
+
65
+ File "C:\hoge.py", line 16, in main
66
+
67
+ cv2.imshow('test',im1)
68
+
69
+ cv2.error: ......\modules\highgui\src\window.cpp:281: error: (-215) size.width>0 && size.height>0 in function cv::imshow
70
+
71
+ ```