cv2
cv2.imread
cv2. imread( filepath, flags)
img = cv2. imread( '1.jpg' , cv2. IMREAD_GRAYSCALE)
cv2.imshow cv2.waitkey cv2.destroyAllwindows
cv2. imshow( 'image' , img)
cv2. waitKey( 0 )
cv2. destroyAllWindows( )
cv2.imwrite
cv2. imwrite( './img/1.png' , img, [ int ( cv2. IMWRITE_JPEG_QUALITY) , 95 ] )
cv2. imwrite( '1.png' , img, [ int ( cv2. IMWRITE_PNG_COMPRESSION) , 9 ] )
cv2.flip
imgflip = cv2. flip( img, 1 )
img.copy
imgcopy = img. copy( )
cv2.cvtColor
img2 = cv2. cvtColor( img, cv2. COLOR_RGB2GRAY)
img3 = cv2. cvtColor( img, cv2. COLOR_GRAY2RGB)
cv2.rectangle
cv. rectangle( img, pt1, pt2, color, thickness= 1 , lineType= 8 , shift= 0 ) → None
cv2. rectangle( img, ( bbox. left, bbox. top) , ( bbox. right, bbox. bottom) , ( 0 , 0 , 255 ) , 2 )
demo1 ord(‘s’) to exit
import cv2
img = cv2. imread( '1.jpg' , cv2. IMREAD_UNCHANGED)
cv2. imshow( 'image' , img)
k = cv2. waitKey( 0 )
if k == ord ( 's' ) :
cv2. imwrite( '1.png' , img)
cv2. destroyAllWindows( )
else :
cv2. destroyAllWindows( )
demo2 putText to img
import cv2
img= cv2. imread( '1.png' , cv2. IMREAD_COLOR)
font = cv2. FONT_HERSHEY_DUPLEX
imgzi = cv2. putText( img, "zhengwen" , ( 1100 , 1164 ) , font, 5.5 , ( 0 , 0 , 0 ) , 2 )
cv2. imwrite( '5.png' , img)
cv2. destroyAllWindows( )
cv2. destroyWindow( wname)
demo3 yolov2 video draw_rectangle
def draw ( self, image, result) :
image_h, image_w, _ = image. shape
colors = self. random_colors( len ( result) )
for i in range ( len ( result) ) :
xmin = max ( int ( result[ i] [ 1 ] - 0.5 * result[ i] [ 3 ] ) , 0 )
ymin = max ( int ( result[ i] [ 2 ] - 0.5 * result[ i] [ 4 ] ) , 0 )
xmax = min ( int ( result[ i] [ 1 ] + 0.5 * result[ i] [ 3 ] ) , image_w)
ymax = min ( int ( result[ i] [ 2 ] + 0.5 * result[ i] [ 4 ] ) , image_h)
color = tuple ( [ rgb * 255 for rgb in colors[ i] ] )
cv2. rectangle( image, ( xmin, ymin) , ( xmax, ymax) , color, 1 )
cv2. putText( image, result[ i] [ 0 ] + ':%.2f' % result[ i] [ 5 ] , ( xmin + 1 , ymin + 8 ) , cv2. FONT_HERSHEY_COMPLEX_SMALL, 0.5 , color, 1 )
print ( result[ i] [ 0 ] , ':%.2f%%' % ( result[ i] [ 5 ] * 100 ) )
def image_detect ( self, imagename) :
image = cv2. imread( imagename)
result = self. detect( image)
self. draw( image, result)
cv2. imshow( 'Image' , image)
cv2. waitKey( 0 )
def video_detect ( self, cap) :
while ( 1 ) :
ret, image = cap. read( )
if not ret:
print ( 'Cannot capture images from device' )
break
result = self. detect( image)
self. draw( image, result)
cv2. imshow( 'Image' , image)
if cv2. waitKey( 10 ) & 0xFF == ord ( 'q' ) :
break
cap. release( )
cv2. destroyAllWindows( )
draw( image, result)
cv2. imshow( 'Image' , image)
if cv2. waitKey( 10 ) & 0xFF == ord ( 'q' ) :
break
cap. release( )
cv2. destroyAllWindows( )
how to save mp4
参考1 参考2 cv2.VideoCapture.get(propId)
访问视频的某些功能,其中propId是一个从0到18的数字,每个数字表示视频的属性(Property Identifier)。retval = cv2.VideoCapture.set(propId,value)
其中一些值可以使用 cap.set(propId,value) 进行修改,value是修改后的值。 例如:通过cap.get(3)和cap.get(4)来检查帧的宽度和高度,默认的值是640x480。现修改为320x240,使用ret = cap.set(3, 320)
和ret = cap.set(4, 240)
。retval,image= cv2.VideoCapture.read([,image])
抓取,解码并返回下一个视频帧。返回值为true表明抓取成功。该函数是组合了grab()和retrieve(),这是最方便的方法。如果没有帧,该函数返回false,并输出空图像。retval, image = cv2.VideoCapture.retrieve([, image[, flag]])
解码并返回抓取的视频帧retval = cv2.VideoCapture.grab()
从视频文件或相机中抓取下一帧。true为抓取成功。该函数主要用于多摄像头时。cv2.VideoCapture.release()
关闭视频文件或相机设备。
import cv2
capture = cv2. VideoCapture( 0 )
capture = cv2. VideoCapture( “1. mp4”)
fourcc = cv2. VideoWriter_fourcc( * 'mp4v' )
width = int ( capture. get( cv2. CAP_PROP_FRAME_WIDTH) )
height = int ( capture. get( cv2. CAP_PROP_FRAME_HEIGHT) )
out = cv2. VideoWriter( 'output/detect2.mp4' , fourcc, 20.0 , ( width, height) )
while ( True ) :
t1 = time. time( )
ref, frame= capture. read( )
frame = cv2. cvtColor( frame, cv2. COLOR_BGR2RGB)
frame = Image. fromarray( np. uint8( frame) )
frame = np. array( yolo. detect_image( frame) )
frame = cv2. cvtColor( frame, cv2. COLOR_RGB2BGR)
fps = ( fps + ( 1 . / ( time. time( ) - t1) ) ) / 2
print ( "fps= %.2f" % ( fps) )
frame = cv2. putText( frame, "fps= %.2f" % ( fps) , ( 0 , 40 ) ,
cv2. FONT_HERSHEY_SIMPLEX, 1 , ( 0 , 255 , 0 ) , 2 )
cv2. imshow( "video" , frame)
out. write( frame)
c= cv2. waitKey( 30 ) & 0xff
if c== 27 :
capture. release( )
break
capture. release( )
out. release( )
cv2. destroyAllWindows( )
out = cv.VideoWriter( filename, fourcc, fps, frameSize[, isColor] )
filename:给要保存的视频起个名字 fourcc:指定视频编解码器的4字节代码
【(‘P’,‘I’,‘M’,‘1’)是MPEG-1编解码器】 【(‘M’,‘J’,‘P’,'G ')是一个运动jpeg编解码器】 fps:帧率 frameSize:帧大小 retval = cv2.VideoWriter_fourcc( c1, c2, c3, c4 )
将4字符串接为fourcc代码。
cv.VideoWriter.write( image )
将帧图像保存为视频文件。 isColor:如果为true,则视频为彩色,否则为灰度视频,默认为true
import cv2
cap = cv2. VideoCapture( 0 )
fourcc = cv2. VideoWriter_fourcc( * 'XVID' )
out = cv2. VideoWriter( 'output.avi' , fourcc, 20.0 , ( 640 , 480 ) )
while ( cap. isOpened( ) ) :
ret, frame = cap. read( )
if ret== True :
frame = cv2. flip( frame, 0 )
out. write( frame)
cv2. imshow( 'frame' , frame)
if cv2. waitKey( 1 ) & 0xFF == ord ( 'q' ) :
break
else :
break
cap. release( )
out. release( )
cv2. destroyAllWindows( )