Denoise.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import cv2
  2. import Tool
  3. from scipy import ndimage
  4. class Denoise(Tool.Tool):
  5. def on_init(self):
  6. self.id = "denoise"
  7. self.name = "Denoise"
  8. self.icon_path = "ui/PF2_Icons/Denoise.png"
  9. self.properties = [
  10. # Detailer
  11. Tool.Property("enabled", "Denoise", "Header", False, has_toggle=True, has_button=False),
  12. Tool.Property("strength", "Strength", "Slider", 0, max=100, min=0),
  13. Tool.Property("w_strength", "White Strength", "Slider", 20, max=100, min=0),
  14. Tool.Property("b_strength", "Black Strength", "Slider", 70, max=100, min=0),
  15. Tool.Property("method", "Method", "Combo", 0, options=[
  16. "Mean",
  17. "Gaussian",
  18. ]),
  19. ]
  20. def on_update(self, image):
  21. im = image
  22. if(self.props["enabled"].get_value()):
  23. bpp = int(str(im.dtype).replace("uint", "").replace("float", ""))
  24. np = float(2 ** bpp - 1)
  25. method = self.props["method"].get_value()
  26. strength = self.props["strength"].get_value()
  27. b_strength = self.props["b_strength"].get_value()
  28. w_strength = self.props["w_strength"].get_value()
  29. filtered = None
  30. if(method == 0):
  31. filtered = ndimage.median_filter(im, 3)
  32. elif(method == 1):
  33. filtered = ndimage.gaussian_filter(im, 2)
  34. w_filter = im
  35. w_filter[im > (float(np) / 10) * 9] = filtered[im > (float(np) / 10) * 9]
  36. b_filter = im
  37. b_filter[im < (float(np) / 10)] = filtered[im < (float(np) / 10)]
  38. # Blend
  39. im = cv2.addWeighted(filtered, (strength / 100), im, 1 - (strength / 100), 0)
  40. im = cv2.addWeighted(w_filter, (w_strength / 100), im, 1 - (w_strength / 100), 0)
  41. im = cv2.addWeighted(b_filter, (b_strength / 100), im, 1 - (b_strength / 100), 0)
  42. return im