dericheSample.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import sys
  2. import numpy as np
  3. import cv2 as cv
  4. def AddSlider(sliderName,windowName,minSlider,maxSlider,valDefault, update=[]):
  5. if update is None:
  6. cv.createTrackbar(sliderName, windowName, valDefault,maxSlider-minSlider+1)
  7. else:
  8. cv.createTrackbar(sliderName, windowName, valDefault,maxSlider-minSlider+1, update)
  9. cv.setTrackbarMin(sliderName, windowName, minSlider)
  10. cv.setTrackbarMax(sliderName, windowName, maxSlider)
  11. cv.setTrackbarPos(sliderName, windowName, valDefault)
  12. class Filtrage:
  13. def __init__(self):
  14. self.s =0
  15. self.alpha = 100
  16. self.omega = 100
  17. self.updateFiltre=True
  18. self.img=[]
  19. self.dximg=[]
  20. self.dyimg=[]
  21. self.module=[]
  22. def DericheFilter(self):
  23. self.dximg = cv.ximgproc.GradientDericheX( self.img, self.alpha/100., self.omega/1000. )
  24. self.dyimg = cv.ximgproc.GradientDericheY( self.img, self.alpha/100., self.omega/1000. )
  25. dx2=self.dximg*self.dximg
  26. dy2=self.dyimg*self.dyimg
  27. self.module = np.sqrt(dx2+dy2)
  28. cv.normalize(src=self.module,dst=self.module,norm_type=cv.NORM_MINMAX)
  29. def SlideBarDeriche(self):
  30. cv.destroyWindow(self.filename)
  31. cv.namedWindow(self.filename)
  32. AddSlider("alpha",self.filename,1,400,self.alpha,self.UpdateAlpha)
  33. AddSlider("omega",self.filename,1,1000,self.omega,self.UpdateOmega)
  34. def UpdateOmega(self,x ):
  35. self.updateFiltre=True
  36. self.omega=x
  37. def UpdateAlpha(self,x ):
  38. self.updateFiltre=True
  39. self.alpha=x
  40. def run(self,argv):
  41. # Load the source image
  42. self.filename = argv[0] if len(argv) > 0 else "../doc/pics/corridor_fld.jpg"
  43. self.img=cv.imread(self.filename,cv.IMREAD_GRAYSCALE)
  44. if self.img is None:
  45. print ('cannot read file')
  46. return
  47. self.SlideBarDeriche()
  48. while True:
  49. cv.imshow(self.filename,self.img)
  50. if self.updateFiltre:
  51. self.DericheFilter()
  52. cv.imshow("module",self.module)
  53. self.updateFiltre =False
  54. code = cv.waitKey(10)
  55. if code==27:
  56. break
  57. if __name__ == '__main__':
  58. Filtrage().run(sys.argv[1:])