Skip to content

Latest commit

 

History

History
165 lines (126 loc) · 4.47 KB

617-1817165-图形的绘制_配合图像_文字_滚动条_多边形.sy.md

File metadata and controls

165 lines (126 loc) · 4.47 KB
show version enable_checker
step
1.0
true

opencv

回忆

  • 上次我们学习了基本绘制
    • 线段
    • 圆形
    • 矩形
  • 但是能够将图形绘制和之前的概念关联起来吗?
    • 图像
    • 滚动条
    • 文字
  • 如何关联呢?🤔

控制圆的大小

import cv2
import numpy as np
     
def callback(value):
    print(value)
     
cv2.namedWindow('canvas', cv2.WINDOW_NORMAL)
cv2.resizeWindow('canvas', 640, 480)
cv2.createTrackbar('T', 'canvas', 0, 100, callback)
     
while True:
    t = cv2.getTrackbarPos('T', 'canvas')
    canvas = np.zeros((480,640,3), np.uint8)
    canvas = cv2.circle(canvas,(150,150), t,(255,255,0),-1)
    cv2.imshow('canvas', canvas)                                               
     
    key = cv2.waitKey(1)
    if key & 0xFF == ord('q'):
        break
     
cv2.destroyAllWindows()
  • 效果

图片描述

灵梦与太极图

图片描述

  • 期待效果

图片描述

具体代码

import cv2                           
import numpy as np                   
import math                          
canvas = cv2.imread("/home/shiyanlou/girl.png")
cv2.putText(canvas, "You're DEAD~", (15,237),cv2. FONT_HERSHEY_TRIPLEX,1, (0,0
canvas = cv2.circle(canvas, (120,110),80, (95,82,200), -1) 
canvas = cv2. rectangle(canvas, (40,190), (200,110), (255,255,255), -1) 
canvas = cv2.circle(canvas, (120,110),82, (95,82,200), 3)
canvas = cv2.circle(canvas, (80,110) ,40, (255,255,255) , -1) 
canvas = cv2.circle(canvas, (160,110),40, (95,82,200), -1) 
canvas = cv2.circle(canvas, (79,110),13, (95,82,200), -1) 
canvas = cv2.circle(canvas, (161,110),13, (255,255,255) , -1) 
cv2. imshow("death sentence", canvas)
cv2.waitKey()                        
cv2. destroyAllWindows()             
cv2.imwrite('/home/shiyanlou/shikei.png',canvas)
  1. 先在这里来个和灵梦(图中女性角色)蝴蝶结一样颜色的红色实心圆作为阴阳玉(即太极图案)太阴部分的底色;
  2. 再创建一个和背景色相同的白色实心矩形,遮挡住红色实心圆的下半部分,作为太阳部分的底色;
  3. 白色矩形把下面圆都挡住了,我们只需要里面白,所以创建一个红色不填充的圆环。因为描边粗细为3,比1大2,所以直径也扩大2,为82,和之后内部的实心圆相切;
  4. 创建阳明部分的白色实心圆;
  5. 创建厥阴部分的有色实心圆;
  6. 创建阳明部分中间少阴的小实心圆;
  7. 创建厥阴部分中间少阳的小实心圆;

五角星

import cv2
import numpy as np

#创建黑色图像
img = np.zeros((512,512,3), np.uint8)

#绘制多边形
pts = np.array([[50, 190], [380, 420], [255, 50], [120, 420], [450, 190]])
cv2.polylines(img, [pts], True, (0, 255, 255), 10)

#显示图像
cv2.imshow("ellipse", img)

#等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()
  • 效果

图片描述

五角星

import cv2
import random
import numpy as np

frame = np.zeros((500,500,3),np.uint8)
pts = np.array([[70, 190], [222, 190], [280, 61], [330, 190], [467, 190],
                            [358, 260], [392, 380], [280, 308], [138, 380], [195, 260]])
cv2.polylines(frame, [pts], True, (255, 255, 0), 5)
cv2.imshow('Frame', frame)
cv2.waitKey()
cv2.destroyAllWindows()
  • 五角星效果

图片描述

文字框

import cv2
import numpy as np
import math
canvas = cv2.imread("/home/shiyanlou/kun1.png")
points = np.array([[50, 100], [50, 200], [100, 200], [100,150],[200, 90],[100,14]])
cv2.fillConvexPoly(canvas, points, (200,200,200),1)
cv2.rectangle(canvas,(50,100),(100,150), (200,200,200),-1)
cv2.putText(canvas,"k",(70,125),cv2.FONT_HERSHEY_DUPLEX,1,(0,0,255),1)
cv2.putText(canvas,"u",(70,150),cv2.FONT_HERSHEY_DUPLEX,1,(0,0,255),1)
cv2.putText(canvas,"n",(70,175),cv2.FONT_HERSHEY_DUPLEX,1,(0,0,255),1)
cv2.imshow("kun",canvas)
cv2.waitKey()
cv2.destroyAllWindows()
cv2.imwrite('/home/shiyanlou/kunkun.png',canvas)
  • 多边形文字背景框

图片描述

总结

  • 这次深入了图形绘制
    • 图像
    • 滑动条
    • 文字
    • 多边形
  • 可以制作渐变的效果吗?🤔
  • 下次再说👋