opencv項目之畫圖

看代碼,有詳細的註釋

import cv2
import numpy as np
import random
import time
import math

#這是圖片的大小
sx = 1000
sy = 1200
#窗口函數
def show(img, name = "haigen",time = 10):
    # cv2.namedWindow(name, cv2.WINDOW_NORMAL)
    cv2.imshow(name,img)
    cv2.waitKey(time)

#畫星星
def star(fi):
    #根據fi的大小來畫星星的個數
    while fi >= 0:
        #確定星星的位置
        x = random.randint(0, 350)
        y = random.randint(0,sy)
        dot = random.randint(0,1)
        #根究dot的大小判斷星星的大小
        cv2.circle(board,(y,x),dot,(255,255,255),dot)
        fi -= 1

#畫閃爍得星星
def ran_star():
    list = []
    #每次閃爍得個數
    for i in range(35):
        x = random.randint(0, 350)
        y = random.randint(0, sy)
        dot = random.randint(0, 1)
        list.append((x,y,dot))
    #畫星星
    for x, y, dot in list:
        cv2.circle(board, (y, x), dot, (255, 255, 255), dot)
    show(board,time=10)
    #然後把星星註釋掉
    for x,y,dot in list:
        cv2.waitKey(20)
        cv2.circle(board, (y, x), dot, (0, 0, 0), dot)

#畫樹
def tree(n,l,star, end, fi):
    val = int(n / 2)
    if(val < 1):
        val = 1
    cv2.line(board,star,end,[255,255,255],val)
    if n > 0:
        # 右分支偏轉角度
        b = random.random() * 15 + 40 - fi * 3
        # 左分支偏轉角度
        c = random.random() * 20 + 80 - fi
        #這裏是計算出接下來的偏角
        b = -b
        c = -c
        c += b
        # 下一個分支的長度
        d = l * (random.random() * 0.25 + 0.75)
        x,y = end
        #計算出下一個右分支中點的位置,根據數學
        x1 = int(x + l * math.cos(math.radians(b)))
        y1 = int(y + l * math.sin(math.radians(b)))
        #畫右分支
        tree(n - 1,d,end,(x1,y1), fi + 1)
        # 計算出下一個左分支中點的位置,根據數學
        x1 = int(x + l * math.cos(math.radians(c)))
        y1 = int(y + l * math.sin(math.radians(c)))
        #畫左分支
        tree(n - 1, d, end, (x1, y1), fi + 1)
    #如果遍歷的層數大於六層就開始畫樹上的花
    if(fi > 6):
        #畫樹上的花
        cv2.circle(board,end,4,[255,255,255],2)

#畫飛起的花
def flower():
    list = []
    #剛開始離樹進的花,就少一點
    for i in range(30):
        x = random.randint(m - 400, m - 50)
        y = random.randint(n - 60, n + 200)
        ori = board[x - 1:x + 10, y - 1:y + 10].copy()
        list.append((x,y, ori))
        cv2.circle(board, (y + 4, x + 4), 4, (255, 255, 255), 2)
    #後面的花多一點啊
    for i in range(75):
        x = random.randint(m - 400, m)
        y = random.randint(n + 200, sy)
        ori = board[x - 1:x + 10, y - 1:y + 10].copy()
        list.append((x,y, ori))
        cv2.circle(board, (y + 4, x + 4), 4, (255, 255, 255), 2)
    return list

#畫動態花
def ran_flower():
    list = []
    #10多多動態花
    list = []
    #運的花不宜太多
    for i in range(20):
        x = random.randint(m - 400, m - 50)
        y = random.randint(n - 60, sy)
        #需要畫一個ori,爲了消掉飛起的花
        ori = board[x - 1:x + 10,y - 1:y + 10].copy()
        # show(ori, "ori1")
        # show(ori, "ori2")
        list.append((x,y,ori))
        #加4的原因是更好的消去運動的花
        cv2.circle(board, (y + 4, x + 4), 4, (255, 255, 255), 2)
    show(board,time=500)
    #消去運動的花
    for x,y,ori in list:
        board[x - 1: x + 10, y - 1 : y + 10] = ori[0:11,0:11]

#畫地上的花
def leaf():
    high = 10
    for i in range(8):
        #畫樹葉
        n = random.randint(0,sy)
        cv2.ellipse(board,(n - 8,sx - high), (20,4), 45, 0, 360,(255,255,255),-1)
        cv2.ellipse(board,(n + 8, sx - high),(20, 4), 130, 0, 360, (255, 255, 255), -1)
        #畫莖稈
        cv2.ellipse(board, (n + 4, sx - high - 10), (30, 6), 105, 0, 180, (255, 255, 255), 2)
        #畫花
        cv2.circle(board,(n + 7, sx - high - 35),6, (255,255,255),-1)
        cv2.circle(board, (n + 7, sx - high - 35), 8, (255, 255, 255), 2)

#畫地上的草
def grass():
    for i in range(200):
        #草的位置
        site = random.randint(0,sy)
        #草的躺的角度
        angle = random.randint(100,160)
        #草的高度
        h = random.randint(0,10)
        #畫草
        cv2.ellipse(board, (site + 4, sx - h), (30, 6), angle, 0, angle, (255, 255, 255), -1)

#畫狼
def wolf():
    #導入狼的圖片
    img = cv2.imread("C:/Test/top.jpg")
    #轉換成HSV的格式,進行分離圖像
    hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
    #cv2.inRange 進行圖像的分離
    low = np.array([0,0,0])
    up = np.array([180,255,42])
    dst = cv2.inRange(hsv,low,up)
    #找到前景
    fg = cv2.cvtColor(dst,cv2.COLOR_GRAY2BGR)
    #找到圖像的大小
    m,n = fg.shape[:2]
    #ORI 來尋找背景
    ori = board[ sx - m : sx, 700: 700 + n]
    mask = cv2.bitwise_not(dst)
    print("ori = ", ori.shape[:2])
    print("fg = ", fg.shape[:2])
    bg = cv2.add(fg,ori,mask=mask)
    #最後的圖
    res = cv2.add(fg,bg)
    #加入原圖像
    board[ sx - m : sx, 700: 700 + n] = res[0:m,0:n]
    show(res,"wolf")

#函數開始的位置
#準備一個畫板
board = np.ones((sx, sy, 3),np.uint8)
#畫固定的星星
star(200)
#畫月亮
cv2.circle(board, (100, 100), 50, (255, 255, 255), -1)
fi = 0
#樹的起始位置
m = 1000
n = 200
#畫樹
tree(9,63, (n,m), (n, m - 60), fi)#遞歸7層
#畫天上飛的花
flower()
#畫花
leaf()
#畫草地
grass()
#畫狼
wolf()
#畫閃爍的星星
while True:
    #每一次遍歷一次就更新一次星星
    ran_star()
    #月亮要不斷的刷新,否則會被星星幹掉
    cv2.circle(board, (100, 100), 50, (255, 255, 255), -1)

圖片
這是狼的圖片
在這裏插入圖片描述
結果圖

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章