有很多新上手opencv的小伙伴在图片的读取这一块常犯错误,很多小伙伴可能看到资料中的代码是这样的:
cv2.imread("lena.jpg")
然后报错是:
error: (-215) size.width>0 && size.height>0 in function cv::imshow
这里就要千万注意了,这一段千万不要照抄书上的内容。因为图片的读取是从你自己的电脑上读取图片,你的电脑上不一定有这个图片,或者路径不对,那就读取不了了。
讲到这里又不得不提到路径要注意的点了。路径分为绝对路径和相对路径了。绝对路径就是从根文件夹开始(Windows一般是以c:,d: 这些盘符作为根文件夹)一层一层地找到目标文件所在地路径,比如说以下示例:
cv2.imread(r"C:\Users\17135\Desktop\python\1.jpg")
相对路径就有点像物理里面的参照物了,简单来说,你建立了一个py文件,一般没有换路径的操作的话,你的相对路径的参照物就是这个py文件的这一级。然后相对路径的表示就是其他文件相对这个参照文件的相对的路径了。比如说,我的py文件的路径是:
那么现在读入图片时.py文件就是我们的参照文件,而1.jpg在python目录下,所以我们要用…/(…/表示上一级目录,./表示同级目录)进入上级目录,也就是进入了python目录下(也就是进入了和opencv文件夹同一级的目录下),可知python目录有两个文件或者文件夹,一个是opencv文件夹,一个是1.jpg文件。所以绝对路径就是:
cv2.imread("../1.jpg")
简单来说要读入图片就要输入图片在你自己电脑上的图片路径,路径不对就会报错。所以说我们来看第一段代码,如果你的py文件同级目录下没有lena.jpg的话,那肯定是会报错的,人家资料这么写是因为人家py文件的同级目录下又lena.jpg这个文件。
OpenCV: 开源计算机视觉库
最近提交(Master分支:3 个月前 )
2a29d87e
Fix null pointer dereference in G-API stateful kernels 7 小时前
c0373447
Introduce option to generate Java code with finalize() or Cleaners interface #28159
Closes https://github.com/opencv/opencv/issues/22260
Replaces https://github.com/opencv/opencv/pull/23467
The PR introduce configuration option to generate Java code with Cleaner interface for Java 9+ and old-fashion finalize() method for old Java and Android. Mat class and derivatives are manually written. The PR introduce 2 base classes for it depending on the generator configuration.
Pros:
1. No need to implement complex and error prone cleaner on library side.
2. No new CMake templates, easier to modify code in IDE.
Cons:
1. More generator branches and different code for modern desktop and Android.
TODO:
- [x] Add Java version check to cmake
- [x] Use Cleaners for ANDROID API 33+
### Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [ ] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
8 小时前
所有评论(0)