Python Opencv学习笔记
GUI Features in OpenCV
Image 初步
cv2.imread(const String & filename, int flags)
filename 图片名,字符串
flags 三种类型imread_color/grayscale/unchanged
如果图片不可以读入,则返回一个空矩阵。
cv2.imshow(const String & winname, InputArray mat)
winname 窗口名称
mat 要展示的图片的矩阵
Show with the function cv::Window_autosize
Always followed by the function cv::WaitKey
cv2.imwrite(const String & filename,InputArray img)
将要展示的图片输出到文件名为filename的文件
key = cv2.waitKey(int delayTime) & 0xFF
用于在键盘上读取输入
delayTime是反应时间
将返回值转换成ASCII码需要使用 & 0xFF
cv2.destoryAllWindows()
cv2.destoryWindow()
cv2.namedWindow(const String & winname, int flags=WINDOW_AUTOSIZE)
create a window with Window_autosize
Video初步
cv2.VideoCapture()
import cv2 as cv
cap = cv.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
# Display the resulting frame
cv.imshow('frame',gray)
if cv.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()
从摄像头获取视频信息
#用输出文件流对录制的文件信息进行保存
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc,20.0,(640,480))
import numpy as np
import cv2 as cv
cap = cv.VideoCapture(0)
# Define the codec and create VideoWriter object
fourcc = cv.VideoWriter_fourcc(*'XVID')
out = cv.VideoWriter('output.avi',fourcc, 20.0, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
#frame = cv.flip(frame,0)
# write the flipped frame
out.write(frame)
cv.imshow('frame',frame)
if cv.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv.destroyAllWindows()
其中被我搞掉的那个是上下翻转的代码。
现在让我试验一下左右反转是否可行。
现在我发现了左右翻转只需要把其中的参数0改成1就可以了。是真的不知道这个作者为什么要在这里搞一个上下翻转,可能是笔者电脑自带的摄像头的问题。
Drawing初步
cv2.line(InputOutputArray img,Point pt1,Point pt2,const Scalar & color,int thickness,int lineType,int shift)
lineType:
- filled
- cv2.line_4 4-connected line
- cv2.line_8 8-connected line
- cv2.line_AA antialiased line 反锯齿
cv2.circle()
cv2.rectangle(InputOutputArray img,Point pt1,Point pt2,const Scalar & color,int thickness,int lineType,int shift)
thickness can be made FILLED to fill the rect.
cv2.ellipse(InputOutputArray img,Point center,int radius,const Scalar & color,int thickness,int lineType,int shift)
thickness can be made FILLED to fill the circle.
cv.putText(InputOutputArray img, const String &
text, Point org, int fontFace, double fontScale,
Scalar color, int thickness = 1,
int lineType = LINE_8,
bool bottomLeftOrigin = false )
The parameter org means the left-bottom cornor of the text string.