OpenCV图像处理--高斯模糊


1 #!/bin/python3
  2 
  3 import cv2 as cv
  4 import numpy as np
  5 
  6 
  7 def clamp(pv):
  8     if pv > 255:
  9         return 255
 10     if pv < 0:
 11         return 0
 12     else:
 13         return pv
 14 
 15 
 16 def gaussian_noise(image):
 17     h,w,c = image.shape
 18     for row in range(h):
 19         for col in range(w):
 20             s = np.random.normal(0,20,3)
 21             b = image[row,col,0] #blue
 22             g = image[row,col,1] #green
 23             r = image[row,col,2] #red
 24             image[row,col,0] = clamp(b + s[0])
 25             image[row,col,1] = clamp(g + s[1])
 26             image[row,col,2] = clamp(r + s[2])
 27     cv.imshow("noise image",image)
 28 
 29 print("----------Hello Python-----------------")
 30 src = cv.imread("/home/laohe/downloads/image.jpeg")
 31 cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)
 32 
 33 cv.imshow("input image",src)
 34 t1 = cv.getTickCount()
 35 gaussian_noise(src)
 36 t2 = cv.getTickCount()
 37 time = (t2 - t1)/cv.getTickFrequency()
                                                       

 


print
("time cinsume: %s"%(time*1000)) 39 dst = cv.GaussianBlur(src,(5,5),0) 40 cv.imshow("Gaussian Blur",dst) 41 cv.waitKey(0) 42 43 cv.destroyAllWindows()

 

posted @ 2020-12-20 22:50  挑水工  阅读(271)  评论(0编辑  收藏  举报