OpenCV—Python Numpy陣列(畫素點)操作
阿新 • • 發佈:2018-12-12
一、遍歷訪問圖片每個畫素點,並修改相應的RGB
def access_pixels(image):
print(image.shape)
height = image.shape[0]
width = image.shape[1]
channels = image.shape[2]
print("width: %s height: %s channels: %s"%(width, height, channels))
for row in range(height):
for col in range(width):
for c in range(channels):
pv = image[row , col, c] #獲取每個畫素點的每個通道的數值
image[row, col, c]=255 - pv #灰度值是0-255 這裡是修改每個畫素點每個通道灰度值
cv2.imshow("Reverse_phase_image",image)
if __name__ =="__main__":
src=cv2.imread('555.png') #預設彩色讀入
cv2.imshow('original_image', src) #顯示影象
t1 = cv2.getTickCount() #GetTickcount函式返回從作業系統啟動到當前所經過的毫秒數
access_pixels(src)
t2 = cv2.getTickCount()
time = (t2-t1)/cv2.getTickFrequency() #getTickFrequency函式返回CPU的頻率,就是每秒的計時週期數
print ("time : %s ms"%(time*1000) ) #輸出執行時間
cv2.waitKey(0)
cv2.destroyAllWindows()