2.圖像方面Numpy數組相關操作 In [1]: import cv2 as cv import numpy as np #圖片顏色反轉 def access_pixels(img): print(img.shape) height=img.shape[0] width=img.shape[1] ...
2.圖像方面Numpy數組相關操作
In [1]:import cv2 as cv import numpy as np #圖片顏色反轉 def access_pixels(img): print(img.shape) height=img.shape[0] width=img.shape[1] channels=img.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=img[row,col,c] img[row,col,c]=255-pv cv.imshow("pixels_demo",img) #前面的access_pixels()方法可以用cv.bitwise_not()代替 def inverse(image): dst=cv.bitwise_not(image) cv.imshow("inverse",dst) #畫出藍色的圖 def create_image(): #img=np.zeros([400,400,3],np.uint8)#多通道 img=np.ones([400,400,1],np.uint8)#單通道 #畫灰度圖 img=img*127 #畫藍色圖 #img[:,:,0]=np.ones([400,400])*255 cv.imshow("new image",img) cv.imwrite("./gray.png",img) print("---hello-----") src=cv.imread("aaa.png",cv.WINDOW_AUTOSIZE) #視窗名字 cv.namedWindow("picture1",0) cv.imshow("picture1",src) t1=cv.getTickCount() create_image() access_pixels(src) inverse(src) t2=cv.getTickCount() #getTickCount():用於返回從操作系統啟動到當前所經的計時周期數,看名字也很好理解,get Tick Count(s)。 #getTickFrequency():用於返回CPU的頻率。get Tick Frequency。這裡的單位是秒,也就是一秒內重覆的次數。 #總次數/一秒內重覆的次數 = 時間(s) print("time:%s ms"%((t2-t1)/cv.getTickFrequency()+1000)) cv.waitKey(0) cv.destroyAllwindows()
---hello----- (96, 89, 3) width:89,height:96,channels=3 time:1000.2815058 ms
原圖:
灰色圖片:
pixel_demo輸出的圖片:
inverse輸出圖片: