Skip to content

Latest commit

 

History

History
159 lines (134 loc) · 4.39 KB

619-1189951-随机的引入.sy.md

File metadata and controls

159 lines (134 loc) · 4.39 KB
show version enable_checker
step
1.0
true

opencv

回忆

  • 上次设置了各种渐变的效果
    • 可以控制灰度的渐变
    • 也可以控制彩色的渐变
    • 还可以配合滚动条制作渐变
  • 还能玩点什么呢?🤔

随机像素效果

from random import randint
import cv2 
import numpy as np
image = np.zeros((30,30,3),np.uint8)
print(image)
for x in range(30):
    for y in range(30):
        r = randint(0,256)
        g = randint(0,256)
        b = randint(0,256)
        t = (b,g,r)
        image[y,x] = t 
print(image)
cv2.imshow("image",image)
cv2.waitKey()
cv2.destroyAllWindows()
  • 随机效果

图片描述

随机颜色同心圆

import numpy as np
import cv2
canvas = np.zeros((300, 300,3), np.uint8)
for r in range(0,150,30):
    print(r)
    color = np.random.randint(0,256,size=(3,)).tolist()
    print(color)
    canvas = cv2.circle(canvas,(150,150),r,color,5)
cv2.imshow("Line",canvas)
cv2.waitKey()
cv2.destroyAllWindows()
  • 随机的颜色

图片描述

随机颜色不同心圆

import numpy as np                                       
import cv2                                               
canvas = np.zeros((300,300,3), np.uint8)                 
for num in range(0,30):                                  
    color = np.random.randint(0,256,size=(3,)).tolist()  
    x = np.random.randint(0,300)                         
    y = np.random.randint(0,300)                         
    r = np.random.randint(0,50)                          
    print("num:",num,":color",color,":(x,y)",(x,y),":r",r)                                         
    canvas = cv2.circle(canvas,(x, y), r,color,-1)       
cv2.imshow("Line",canvas)                                
cv2.waitKey()                                            
cv2.destroyAllWindows()
  • 不同圆心的实心圆

图片描述

随机的射线

import numpy as np
import cv2
canvas = np.zeros((300,300,3), np.uint8)
for num in range(0,30):   
    color = np.random.randint(0,256,size=(3,)).tolist()
    x = np.random.randint(0,300)
    y = np.random.randint(0,300)
    r = np.random.randint(1,5)
    print("num:",num,":color",color,":(x,y)",(x,y),":r",r)                                         
    canvas = cv2.line(canvas,(150,150),(x, y),color,r)
cv2.imshow("Lines",canvas) 
cv2.waitKey()
cv2.destroyAllWindows()
  • 效果

图片描述

二维码生成

from random import randint                                      
import numpy as np                                              
import cv2                                                      
canvas = np.zeros((300, 300), np.uint8)                         
for x in range(0,300,10):                                       
    for y in range(0,300,10):                                   
        r = randint(0,1)                                        
        if r == 1:                                              
            canvas = cv2.rectangle(canvas,(x, y),(x+10,y+10),255,-1)
cv2.imshow("Line",canvas)                                       
cv2.waitKey()                                                   
cv2.destroyAllWindows()
  • 随机二维码

图片描述

抖音的抖动

图片描述

import cv2                                                                      
import numpy as np
from random import randint
before = cv2.imread("/home/shiyanlou/tiktok.jpg")
cv2.imshow("before",before)
after = np.zeros((300,300,3),np.uint8)
x0 = randint(0,30)
y0 = randint(0,30)
x1 = randint(0,30)
y1 = randint(30)
after[y0:300,x0:300,0] = before[0:300-y0,0:300-x0,0]
after[y0:300,x0:300,1] = before[0:300-y0,0:300-x0,1]
after[y1:300,x1:300,2] = before[0:300-y1,0:300-x1,2]
cv2.imshow("after",after)
cv2.waitKey()
cv2.destroyAllWindows()
  • 每次运行效果都不一样

图片描述

总结 🤔

  • 我们这次使用了随机效果
    • 随机点
    • 随机颜色
    • 随机圆心
    • 随机端点
    • 随机位移
  • 还可以做点什么好玩的吗?🤔
  • 比如三角函数?🤔
  • 下次再说👋