opencv入門--opencv2

讀取圖片

import cv2 as cv
src=cv.imread("./3.jpg",cv.IMREAD_GRAYSCALE)
cv.namedWindow("input",cv.WINDOW_AUTOSIZE)
cv.imshow("input",src)

cv.waitKey(0)
cv.destroyAllWindows()

幾種創建初始圖像的方法

import cv2 as cv

import numpy as np


def read_write_image():
    src = cv.imread("./3.jpg", cv.IMREAD_ANYCOLOR)
    cv.imshow("imnput", src)
    cv.imwrite("./4.png", src)
    print(src)


def creation_image():
    src = cv.imread("./3.jpg", cv.IMREAD_ANYCOLOR)
    cv.imshow("input", src)

    dst = np.copy(src)
    dst.fill(127)
    cv.imshow("det", dst)

    #直接創建空白圖片
    blank=np.zeros([400,400],dtype=np.uint8)
    cv.imshow("blankl",blank)

    #第三種方法創建
    t3=np.zeros([4000],dtype=np.uint8)
    t3.reshape([200,20])
    cv.imshow("t3",t3)

    #希望創建的圖片大小和輸入圖片大小保持一致
    clone=np.zeros(src.shape,src.dtype)
    cv.imshow("clone",clone)
    cv.imwrite("./clone.png",clone)

    #隨機生成圖像
    t5=np.random.random_sample([400,400])*255
    cv.imshow("t5",t5)
    t6=np.uint8(t5)
    cv.imshow("t6",t6)


if __name__ == '__main__':
    creation_image()
    cv.waitKey(0)
    cv.destroyAllWindows()

簡單的圖形繪製

import cv2 as cv
import numpy as np


def draw_graphics_demo():
    src = np.zeros([400, 400, 3], dtype=np.uint8)
    # cv.imshow("原圖", src)

    # 繪製直線
    cv.line(src, (10, 10), (400,400),(255, 0, 0), 4, cv.LINE_8, 0)#哪一個圖像,開始點,結束點
    cv.line(src, (400, 10), (10, 400), (0, 255, 255), 4, cv.LINE_8, 0)

    #繪製方框
    cv.rectangle(src,(100,100),(300,300),(0,255,0),4,cv.LINE_8,0)

    #繪製圓
    cv.circle(src,(250,250),150,(255,255,0),4,cv.LINE_8,0)

    #繪製橢圓
    cv.ellipse(src,(250,250),(150,50),0,300,300,(255,0,255),4,cv.LINE_8,0)

    #繪製文本
    cv.putText(src,"hello cv",(50,50),cv.FONT_HERSHEY_PLAIN,1.2,(0,0,155),0)
    cv.imshow("opencv", src)


if __name__ == '__main__':
    draw_graphics_demo()
    cv.waitKey(0)
    cv.destroyAllWindows()

#運行結果
在這裏插入圖片描述

多邊形圖像繪製,隨機繪製彩色直線

import cv2 as cv
import numpy as np


def draw_graphics_demo():
    src = np.zeros([400, 400, 3], dtype=np.uint8)
    # cv.imshow("原圖", src)

    # 繪製直線
    cv.line(src, (10, 10), (400, 400), (255, 0, 0), 4, cv.LINE_8, 0)  # 哪一個圖像,開始點,結束點
    cv.line(src, (400, 10), (10, 400), (0, 255, 255), 4, cv.LINE_8, 0)

    # 繪製方框
    cv.rectangle(src, (100, 100), (300, 300), (0, 255, 0), 4, cv.LINE_8, 0)

    # 繪製圓
    cv.circle(src, (250, 250), 150, (255, 255, 0), 4, cv.LINE_8, 0)

    # 繪製橢圓
    cv.ellipse(src, (250, 250), (150, 50), 0, 300, 300, (255, 0, 255), 4, cv.LINE_8, 0)

    # 繪製文本
    cv.putText(src, "hello cv", (50, 50), cv.FONT_HERSHEY_PLAIN, 1.2, (0, 0, 155), 0)
    cv.imshow("opencv", src)

    # 繪製多邊形
    points = []
    src2 = np.zeros([400, 400, 3], dtype=np.uint8)
    points.append((100, 100))
    points.append((100, 50))
    points.append((200, 100))
    points.append((200, 300))
    points.append((100, 300))
    index = 0  # 循環裏面要用到的索引
    for point in points:
        cv.line(src2, point, points[(index + 1) % 5], (0, 0, 255), 4, cv.LINE_8, 0)
        index += 1
    # 填充
    cv.rectangle(src2, (100, 100), (400, 400), (0, 255, 0), -1, cv.LINE_8, 0)
    cv.imshow("src2", src2)

    src3 = np.zeros([600, 600, 3], dtype=np.uint8)
    for i in range(1000):  # 生成隨機顏色隨機位置的直線
        x1 = np.int(np.random.rand() * 600)
        y1 = np.int(np.random.rand() * 600)
        x2 = np.int(np.random.rand() * 600)
        y2 = np.int(np.random.rand() * 600)
        b = np.random.randint(0, 255)
        g = np.random.randint(0, 255)
        r = np.random.randint(0, 255)
        cv.line(src3, (x1, y1), (x2, y2), (b, g, r), 4, cv.LINE_8, 0)
        cv.imshow("random",src3)
        c = cv.waitKey(20)
        if c == 27:  # 退出
            break


if __name__ == '__main__':
    draw_graphics_demo()
    cv.waitKey(0)
    cv.destroyAllWindows()

運行

在這裏插入圖片描述

相應鼠標事件

import cv2 as cv
import numpy as np


def my_mouse_callback(event, x, y, flag, params):
    """
    事件處理函數
    :param event: 事件
    :param x: x軸
    :param y: y軸
    :param flag:
    :param params:用戶數據
    :return:
    """
    if event == cv.EVENT_LBUTTONDBLCLK:  # 如果用戶雙擊就繪製一個圓
        b = np.random.randint(0, 255)
        g = np.random.randint(0, 255)
        r = np.random.randint(0, 255)
        cv.circle(params, (x, y), 50, (r, b, g), 2, cv.LINE_8, 0)
    if event==cv.EVENT_LBUTTONDOWN:#鼠標是一直按着的畫 x1 = np.int(np.random.rand() * 600)
        y1 = np.int(np.random.rand() * 600)
        x2 = np.int(np.random.rand() * 600)
        y2 = np.int(np.random.rand() * 600)
        x1 = np.int(np.random.rand() * 600)
        b = np.random.randint(0, 255)
        g = np.random.randint(0, 255)
        r = np.random.randint(0, 255)
        cv.line(params, (x1, y1), (x2, y2), (b, g, r), 4, cv.LINE_8, 0)

def mouse_demo():
    src = np.zeros((512, 512, 3), dtype=np.uint8)
    cv.namedWindow("mouse_demo", cv.WINDOW_AUTOSIZE)  # 給窗口添加一個名稱
    cv.setMouseCallback("mouse_demo", my_mouse_callback, src)
    while True:
        cv.imshow("mouse_demo", src)
        c = cv.waitKey(20)
        if c == 27:
            break


if __name__ == '__main__':
    mouse_demo()
    cv.waitKey(0)
    cv.destroyAllWindows()

這裏的用戶數據其實也就是初始化的圖像

滑塊

createTrackbar 滑塊創建
getTrackbarPos 響應函數

import numpy as np
import cv2 as cv


def do_nothing():
    pass


def track_bar_demo():
    src = np.zeros((512, 512, 3), dtype=np.uint8)
    cv.namedWindow("tb_demo", cv.WINDOW_AUTOSIZE)
    cv.createTrackbar("B", "tb_demo", 0, 255, do_nothing)
    cv.createTrackbar("G", "tb_demo", 0, 255, do_nothing)
    cv.createTrackbar("R", "tb_demo", 0, 255, do_nothing)
    while True:
        b = cv.getTrackbarPos("B", "tb_demo")
        g = cv.getTrackbarPos("G", "tb_demo")
        r = cv.getTrackbarPos("R", "tb_demo")
        src[:] = [b, g, r]
        cv.imshow("tb_demo", src)
        c = cv.waitKey(15)
        if c == 17:
            break


if __name__ == '__main__':
    track_bar_demo()

這個還是蠻重要的,以後調試參數的時候就可用這個滑塊
在這裏插入圖片描述
調節RGB數值改變顏色

像素操作

import cv2 as cv
import numpy as np

def pixel_demo():
    src=cv.imread("./3.jpg")
    print(src.shape)
    h,w,ch=src.shape
    cv.imshow("input",src)

    for row in range(h):
        for cow in range(w):
            b,g,r=src[row,cow]
            b=255-b
            g=255-g
            r=255-r
            src[row,cow]=[b,b,b]
    cv.imshow("output",src)

if __name__ == '__main__':
    pixel_demo()
    cv.waitKey(0)
    cv.destroyAllWindows()

對像素值取反
在這裏插入圖片描述

單通道與多通道

待更

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