[OpenCV笔记]简介和入门
Posted 外面下着雪的小木屋
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[OpenCV笔记]简介和入门相关的知识,希望对你有一定的参考价值。
本篇笔记会简单介绍一下OpenCV和几个它的几个入门的指令。
- OpenCV简介
OpenCV(Open Source Computer Vision Library)是一个开源的计算机视觉库,用C++/C编写,留有Python、Java接口。
- 安装
这里介绍Python环境下的OpenCV的安装:
首先你的电脑需要有pip(不知道可以百度一下,很多Python的库或者包都可以通过这个来下载和安装),(在管理员权限下)CMD执行:
pip install opencv-python
界面如下:
不出意外的话就等它装好了,当然,我在装的时候遇到了不少问题,如果遇到问题可以留言问~
那么在使用之前,你的电脑还需要装NumPy,这个是一个科学计算的库,也是用C++/C编写的,目的是计算更快。也可以用pip安装。
OpenCV目前有两个大的版本2和3,对应的是Python2和Python3,执行上面的指令的话,安装的是适配你的系统的最新的OpenCV3,嘛,毕竟现在用Python3的人会多一些。
安装好了之后就可以使用了,官方的tutorial给了最简单的例子,下面是我的笔记:
1.Opencv-python is a python wrapper of the original opencv c++ implimentation
Use numpy to make the program run faster( as fast as c++, almost)
Before learning opencv, learn the Numpy first
So you of need to import numpy and you must import cv2, your program usually begins as:
import numpy as np
import cv2
2.Opencv GUI
Getting started with images
Read, display and save an image
cv2.imread() cv2.imshow() cv2.imwrite()
Matplotlib
① cv2.imread()
img= cv2.imread('imgpath and imgname',num)
The image should be in the working directory of you give the function a full path of the image
Second argument is a flag specifying how should the image being read:
-- cv2.IMREAD_COLOR: loads a colored image, default mode, any transparency of the image will be neglected.
-- cv2.IMREAD_GRAYSCALE: loads image with a grayscale mode
-- cv2.IMREAD_UNCHANGED: loads image with such including alpha channel
You can use the argument above or more simply, use 1, 0 and -1 instead.
② cv2.imshow()
This command displays an image in a window.
cv2.imshow('image', img)
The first argument is the name of the window and the second one is the image we have
cv2.waitKey(0)
If we have this after the cv2.imshow() function, the program will wait for 0 milliseconds for any keyboard event, if you press any keys in that time, the program continue, and if not, after 0 milliseconds, if wait indefinitely for any key stroke.
cv2.destroyAllWindows() and cv2.destroyWindow()
The first one destroys all the windows that we've created, and the second one destroy a specitic one using the name of the window as the argument.
For example if we have the codes below:
importcv2
Importnumpyasnp
img=cv2.imread('c:\\users\joker\desktop\}05IL$11_]HDMWLPVNZKDMN.png')
cv2.imshow("image",img)
cv2.waitKey(100000)
cv2.destoryAllWindows()
If we do noting in the next ten seconds, the image will be shown in the window for ten seconds, and if we press any key during that period of time, the window will be shut.
③ cv2.imwrite()
cv2.imwrite('xxx.png', img)
This command will save img in format PNG in the working directory
Sum
Below is a example of the tutorial to sum the commands:
importcv2
importnumpyasnp
img=cv2.imread('c:\\users\joker\desktop\}05IL$11_]HDMWLPVNZKDMN.png')
cv2.imshow("image",img)
k=cv2.waitKey(0)
ifk==27:
cv2.destroyAllWindows()
elifk==ord('s'):
cv2.imwrite('test.png',img)
cv2.destroyAllWindows()
If you press s, the program will save the image and shut the image window, if you press esc instead, the program will shut the window without saving the image.
④ Matplotlib
Matplotlib is a plotting lib for python which gives you plotting methods, you can zooming the image, save it and etc using it.
Use pip install to install the Matplotlib: pip install Matplotlib
Codes are displayed below:
importcv2
importnumpyasnp
frommatplotlibimportpyplotasplt
img=cv2.imread('c:\\users\joker\desktop\}05IL$11_]HDMWLPVNZKDMN.png',0)
plt.imshow(img,cmap='gray',interpolation='bicubic')
plt.xticks([]),plt.yticks([])
plt.show()
This program loads the image in grayscale method, and display it in a window, which is slightly different from cv2.imshow(), you can try it and you will see the difference.
嘛,虽然是英文的但是应该还挺容易懂的,如果写的太难懂可以点原文链接,官方的tutorial会写的更清楚一些。
p.s.最近好像微信给这里增加了留言功能,嘛,感兴趣的可以在这里留言了~
有一个小小的请求,就是看完了这篇在留言的地方喵一声呗~
喵~
以上是关于[OpenCV笔记]简介和入门的主要内容,如果未能解决你的问题,请参考以下文章