Skip to content

Latest commit

 

History

History
243 lines (207 loc) · 8.16 KB

620-1189952-三角函数的引用.sy.md

File metadata and controls

243 lines (207 loc) · 8.16 KB
show version enable_checker
step
1.0
true

opencv

回忆

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

正弦曲线

import numpy as np
import cv2 
import math
canvas = np.zeros((300, 300,3), np.uint8)
x = 0 
for num in range(0,300):
    radius = 1 
    x = x + 1         
    y = int(150 + math.sin(x/10) * 100)
    print(x,y)
    canvas = cv2.circle(canvas,(x, y),radius,(255,255,0),1)
cv2.imshow("Line",canvas)
cv2.waitKey()
cv2.destroyAllWindows()
  • 生成

图片描述

  • 能否更加连续鲜明呢?

连续正弦曲线

import numpy as np
import cv2  
import math 
canvas = np.zeros((300, 300,3), np.uint8)
for x in range(0,300):
    x1 = x  
    x2 = x + 1               
    y1 = int(150 + math.sin(x1/10) * 100)
    y2 = int(150 + math.sin(x2/10) * 100)
    canvas = cv2.line(canvas,(x1, y1),(x2, y2), (255,255,0),2)
cv2.imshow("Line",canvas)                                                     
cv2.waitKey()
cv2.destroyAllWindows()
  • 这次画的是线段

图片描述

  • 可以绘制余弦曲线吗?

连续余弦曲线

import numpy as np
import cv2  
import math 
canvas = np.zeros((300, 300,3), np.uint8)
for x in range(0,300):
    x1 = x  
    x2 = x + 1               
    y1 = int(150 + math.cos(x1/20) * 100)
    y2 = int(150 + math.cos(x2/20) * 100)
    canvas = cv2.line(canvas,(x1, y1),(x2, y2), (255,255,0),2)
cv2.imshow("Line",canvas)                                                     
cv2.waitKey()
cv2.destroyAllWindows()
  • 余弦

图片描述

圆形组成圆

import numpy as np
import cv2 
import math
canvas = np.zeros((300, 300,3), np.uint8)
for num in range(0,360,15):
    radius = math.radians(num)
    x = int(150 + math.cos(radius) * 100)
    y = int(150 + math.sin(radius) * 100)
    print(x,y)
    canvas = cv2.circle(canvas,(x, y),20,(255,255,0),1)
cv2.imshow("Line",canvas)
cv2.waitKey()
cv2.destroyAllWindows()
  • 圆形组成的圆

图片描述

  • 可以加些随机进去吗?

随机圆形组成的圆

import numpy as np                                           
import random                                                
import cv2                                                   
import math                                                  
canvas = np.zeros((300, 300,3), np.uint8)                    
for num in range(0,360,15):                                  
    radius = math.radians(num)                               
    x = int(150 + math.cos(radius) * 100)                    
    y = int(150 + math.sin(radius) * 100)                    
    r = random.randint(20,30)                                
    color = np.random.randint(0,256,size=(3,)).tolist()   
    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 cv2                                       
import numpy as np                               
import math                                      
canvas = cv2.imread("/home/shiyanlou/kun1.png")        
cv2.putText(canvas,"kun",(30,80),cv2.FONT_HERSHEY_DUPLEX,1,(0,0,255),1)
for num in range(0,360,15):                      
    radius = math.radians(num)                   
    x = int(305 + math.cos(radius) * 100)        
    y = int(178 + math.sin(radius) * 100)        
    print(x,y)                                   
    canvas = cv2.circle(canvas,(x, y),20,(0,0,255),1)
cv2.imshow("kun",canvas)                         
cv2.waitKey()                                    
cv2.destroyAllWindows()                          
cv2.imwrite('/home/shiyanlou/kunkun.png',canvas)
  • 效果

图片描述

使用三角函数画圆

import numpy as np                                           
import cv2                                                   
import time                                                  
import math                                                  
width = height = 300                                         
center_x = center_y = 150                                    
x = y = 0                                                    
r = 0                                                        
img = np.zeros((width, height, 3),np.uint8)                  
                                                             
for num in range(360):                                       
    if num == 0:                                             
        x = 250                                              
        y = 150                                              
        continue                                             
    radius = num / 360 * 2 * math.pi                         
    new_x = int(center_x + math.cos(radius) * 100)           
    new_y = int(center_y + math.sin(radius) * 100)           
    print("x,y",x,y,"====new_x,new_y",new_x, new_y)          
    img = cv2.line(img, (x,y),(new_x, new_y), (255,255,255),3)
    x = new_x                                                
    y = new_y                                                
cv2.imshow("img",img)                                        
cv2.waitKey()                                                
cv2.destroyAllWindows()
  • 画圆

图片描述

  • 可以加点随机吗?

画的没有那么圆

import numpy as np                                           
import cv2                                                   
import time                                                  
import math                                                  
import random                                                
width = height = 300                                         
center_x = center_y = 150                                    
x = y = 0                                                    
r = 0                                                        
img = np.zeros((width, height, 3),np.uint8)                  
                                                             
for num in range(360):                                       
    if num == 0:                                             
        x = 250                                              
        y = 150                                              
        continue                                             
    r = r - 1 + random.randint(0,2)                          
    if r > 103:                                              
        r = 103                                              
    if r < 97:                                               
        r = 97                                               
    radius = num / 360 * 2 * math.pi                         
    new_x = int(center_x + math.cos(radius) * r)             
    new_y = int(center_y + math.sin(radius) * r)             
    print("x,y",x,y,"====new_x,new_y",new_x, new_y)          
    img = cv2.line(img, (x,y),(new_x, new_y), (255,255,255),3)
    x = new_x                                                
    y = new_y                                                
cv2.imshow("img",img)                                        
cv2.waitKey()                                                
cv2.destroyAllWindows()
  • 使用三角函数画圆
    • 并加入随机因素

图片描述

总结 🤔

  • 这次研究了三角函数
    • 画正余弦曲线
    • 画圆
  • 还可以画点什么吗?🤔
  • 下次再说👋