绘制图形和文字
demo1
import cv2
import numpy as np
canvas = np. ones( ( 300 , 300 , 3 ) , np. uint8) * 255
canvas = cv2. line( canvas, ( 50 , 50 ) , ( 250 , 50 ) , ( 255 , 0 , 0 ) , 5 )
canvas = cv2. line( canvas, ( 50 , 150 ) , ( 250 , 150 ) , ( 0 , 255 , 0 ) , 10 )
canvas = cv2. line( canvas, ( 50 , 250 ) , ( 250 , 250 ) , ( 0 , 0 , 255 ) , 15 )
canvas = cv2. line( canvas, ( 150 , 50 ) , ( 150 , 250 ) , ( 0 , 255 , 255 ) , 5 ) cv2. imshow( "Lines" , canvas)
cv2. waitKey( )
cv2. destroyAllWindows( )
demo2
import cv2
import numpy as npcanvas = np. zeros( ( 300 , 300 , 3 ) , np. uint8)
canvas = cv2. rectangle( canvas, ( 50 , 50 ) , ( 200 , 150 ) , ( 255 , 255 , 0 ) , 5 )
cv2. imshow( "img" , canvas)
cv2. waitKey( )
cv2. destroyAllWindows( )
demo3
import numpy as np
import cv2
canvas = np. zeros( ( 300 , 300 , 3 ) , np. uint8)
canvas = cv2. rectangle( canvas, ( 50 , 50 ) , ( 250 , 250 ) , ( 0 , 0 , 255 ) , 40 )
canvas = cv2. rectangle( canvas, ( 90 , 90 ) , ( 210 , 210 ) , ( 0 , 255 , 0 ) , 30 )
canvas = cv2. rectangle( canvas, ( 120 , 120 ) , ( 180 , 180 ) , ( 255 , 0 , 0 ) , 20 )
canvas = cv2. rectangle( canvas, ( 140 , 140 ) , ( 160 , 160 ) , ( 0 , 255 , 255 ) , - 1 )
cv2. imshow( "img" , canvas)
cv2. waitKey( )
cv2. destroyAllWindows( )
demo4
import numpy as np
import cv2canvas = np. zeros( ( 100 , 300 , 3 ) , np. uint8)
canvas = cv2. circle( canvas, ( 50 , 50 ) , 40 , ( 0 , 0 , 255 ) , - 1 )
canvas = cv2. circle( canvas, ( 150 , 50 ) , 40 , ( 0 , 255 , 255 ) , - 1 )
canvas = cv2. circle( canvas, ( 250 , 50 ) , 40 , ( 0 , 255 , 0 ) , - 1 ) cv2. imshow( "circle" , canvas)
cv2. waitKey( )
cv2. destroyAllWindows( )
demo5
import numpy as np
import cv2canvas = np. zeros( ( 300 , 300 , 3 ) , np. uint8)
center_X = int ( canvas. shape[ 1 ] / 2 )
center_Y = int ( canvas. shape[ 0 ] / 2 ) for r in range ( 0 , 150 , 30 ) : cv2. circle( canvas, ( center_X, center_Y) , r, ( 0 , 255 , 0 ) , 5 )
cv2. imshow( "Circles" , canvas)
cv2. waitKey( )
cv2. destroyAllWindows( )
demo6
import numpy as np
import cv2canvas = np. zeros( ( 300 , 300 , 3 ) , np. uint8)
for numbers in range ( 0 , 28 ) : center_X = np. random. randint( 0 , high= 300 ) center_Y = np. random. randint( 0 , high= 300 ) radius = np. random. randint( 11 , high= 71 ) color = np. random. randint( 0 , high= 256 , size= ( 3 , ) ) . tolist( ) cv2. circle( canvas, ( center_X, center_Y) , radius, color, - 1 ) cv2. imshow( "Circles" , canvas)
cv2. waitKey( )
cv2. destroyAllWindows( )
demo7
import numpy as np
import cv2
canvas = np. zeros( ( 300 , 300 , 3 ) , np. uint8) pts = np. array( [ [ 100 , 50 ] , [ 200 , 50 ] , [ 50 , 250 ] , [ 250 , 250 ] ] , np. int32) canvas = cv2. polylines( canvas, [ pts] , True , ( 0 , 0 , 255 ) , 5 ) cv2. imshow( "img" , canvas)
cv2. waitKey( )
cv2. destroyAllWindows( )
demo8
import numpy as np
import cv2
canvas = np. zeros( ( 100 , 300 , 3 ) , np. uint8)
fontStyle = cv2. FONT_HERSHEY_DUPLEX + cv2. FONT_ITALIC
cv2. putText( canvas, "mrsoft" , ( 20 , 70 ) , fontStyle, 2 , ( 0 , 255 , 0 ) , 5 )
cv2. imshow( "img" , canvas)
cv2. waitKey( )
cv2. destroyAllWindows( )
demo9
import cv2
import time
import numpy as np
width, height = 200 , 200
r = 20
x = r + 20
y = r + 100
x_offer = y_offer = 4 while cv2. waitKey( - 1 ) == - 1 : if x > width - r or x < r: x_offer *= - 1 if y > height- r or y < r: y_offer *= - 1 x += x_offery += y_offerimg = np. ones( ( width, height, 3 ) , np. uint8) * 255 cv2. circle( img, ( x, y) , r, ( 255 , 0 , 0 ) , - 1 ) cv2. imshow( "img" , img) time. sleep( 1 / 60 )
cv2. destroyAllWindows( )