OpenCV---Numpy數組的使用以及創建圖片


一:對頭像的所有像素進行訪問,並UI圖像進行像素取反

(一)for循環取反

import cv2 as cv
import numpy as np

def access_pixels(image):   #對圖像的所有像素進行訪問
    print(image.size)
    height,width,channel = image.shape  #每個像素3個通道,通道順序b,g,r
    print("height:%s\r\nwidth:%s\r\nchannel:%s\r\n"%(height,width,channel))
    '''
    height:608
    width:343
    channel:3
    '''
    for row in range(height):
        for col in range(width):
            for c in range(channel):    #循環會變慢,經過625632循環
                pv = image[row,col,c]
                image[row,col,c] = 255 - pv  #像素取反
    cv.imshow("pixels_demo",image)



src = cv.imread("./1.png")  #讀取圖片
cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)    #創建GUI窗口,形式為自適應
cv.imshow("input image",src)    #通過名字將圖像和窗口聯系
t1 = cv.getTickCount() #獲取時間,用於精度計時,操作系統啟動所經過(elapsed)的毫秒數
access_pixels(src)
t2 = cv.getTickCount()
print((t2-t1)/cv.getTickFrequency()) #getTickFrequency()是獲取一秒鍾結果的點數,獲取秒數
cv.waitKey(0)   #等待用戶操作,里面等待參數是毫秒,我們填寫0,代表是永遠,等待用戶操作
cv.destroyAllWindows()  #銷毀所有窗口
625632
height:608
width:343
channel:3

15.740029368334588  #經歷了15秒,是十分耗時的循環,我們可以使用Numpy數組訪問,更加方便快捷

(二)使用內置方法取反(直接使用c代碼執行,效率更高)

def inverse(image):
    img = cv.bitwise_not(image)
    cv.imshow("inverse image",img)
t1 = cv.getTickCount()  #獲取時間,用於精度計時,操作系統啟動所經過(elapsed)的毫秒數
inverse(src)
t2 = cv.getTickCount()
0.09940230583146789

二:使用Numpy數組,創建圖片

(一)使用多個信道創建圖片

def create_img():
    img = np.zeros([400,400,3],np.uint8) #創建一個三維數組高400,寬400,信號通道3個,初始都為0,每通道占8位個  img[:,:,0] = np.ones([400,400])*255 #將0號通道下[400,400]面積使用ones設置為1,之后乘以255,將其設置為255,注意:3個信道分別是b,g,r所以這里顯示為藍色

    cv.imshow("new image",img)

create_img()
cv.waitKey(0)   #等待用戶操作,里面等待參數是毫秒,我們填寫0,代表是永遠,等待用戶操作
cv.destroyAllWindows()  #銷毀所有窗口

(二)使用單個信道創建圖像(灰度圖像)

def create_img():
    img = np.zeros([400,400,1],np.uint8)  #創建一個只有一個信道的三維數組,初始為0
    img[:,:,0] = np.ones([400,400])*127  #修改這個圖像的信道為127,灰色
    cv.imshow("new image",img)

或者(所以初始時候使用ones會更加靈活

def create_img():
    img = np.ones([400,400,1],np.uint8)  #創建一個只有一個信道的三維數組,初始為1
    img = img * 127  #可以直接進行運算

    cv.imshow("new image",img)
cv.imwrite(".3.png",img)  #可以進行保存

三:補充Numpy的使用

 

(一)二維數組的使用(選擇正確的類型)

1.float類型

def create_arr():
    ml = np.ones([3,3],np.float32)  #float類型,允許小數存在
    ml.fill(122.388)
    print(ml)

create_arr()
[[122.388 122.388 122.388]
 [122.388 122.388 122.388]
 [122.388 122.388 122.388]]

2.int類型

def create_arr():
    ml = np.ones([3,3],np.uint8)  #不允許小數的存在,且有最大是255
    ml.fill(122.388)
    print(ml)

create_arr()
[[122 122 122]
 [122 122 122]
 [122 122 122]]
def create_arr():
    ml = np.ones([3,3],np.uint8)  #有位數限制,高位被截斷了,低位留下了
    ml.fill(256.388)
    print(ml)

create_arr()
[[0 0 0]
 [0 0 0]
 [0 0 0]]

(二)維數轉換reshape

def create_arr():
    ml = np.ones([3,3],np.uint8)
    ml.fill(122.388)
    m2 = ml.reshape([1,9])  #注意:轉換維度,數組大小還是要一致的,不然報錯
    print(m2)
[[122 122 122 122 122 122 122 122 122]]

 (三)使用array自定義數組

def create_arr():
    m3 = np.array([[2,2,3],[4,5,6],[7,8,9]],np.uint8)
    print(m3)
[[2 2 3]
 [4 5 6]
 [7 8 9]]

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM