利用numpy对像素点进行操作
生活随笔
收集整理的這篇文章主要介紹了
利用numpy对像素点进行操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1,源代碼:
import cv2 as cv
import numpy as npdef 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 #修改圖像像素點的數值cv.imshow("pixels_demo", image)def inverse(image):dst = cv.bitwise_not(image) #對圖像(灰度圖像或彩色圖像均可)每個像素值進行二進制“非”操作cv.imshow("inverse demo", dst)def create_image(): #創建新的圖像img1 = np.zeros([400, 400, 3], np.uint8) #定義多通道圖像大小并初始化為0#img[: , : , 0] = np.ones([400, 400])*255 #給單通道賦值img1[:, :, 2] = np.ones([400, 400]) * 255cv.imshow("new image1", img1) img2 = np.ones([400, 400, 1], np.uint8) #定義單通道圖像大小并初始化為1img 2= img2 * 255cv.imshow("new image", img)cv.imwrite("F:/images/myImage.png", img2)m1 = np.ones([3, 3], np.int32) #選擇合適的類型,避免截斷m1.fill(12222.388) print(m1)m2 = m1.reshape([1, 9]) #維度轉換print(m2)m3 = np.array([[2,3,4], [4,5,6],[7,8,9]], np.int32)#m3.fill(9)print(m3)print("--------- Hello Python ---------")
src = cv.imread("F:/images/lena.png") # blue, green red
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
t1 = cv.getTickCount() #獲取CPU時鐘周期個數
#access_pixels(src)
create_image()
inverse(src)
t2 = cv.getTickCount()
time = (t2-t1)/cv.getTickFrequency(); #獲取時間段
print("time : %s ms"%(time*1000))
cv.waitKey(0)cv.destroyAllWindows()
2,運行結果:
總結
以上是生活随笔為你收集整理的利用numpy对像素点进行操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《秋思》第七句是什么
- 下一篇: 上海欢乐谷日场和夜场的区别