test_digital_image_processing.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. """
  2. PyTest's for Digital Image Processing
  3. """
  4. import digital_image_processing.edge_detection.canny as canny
  5. import digital_image_processing.filters.gaussian_filter as gg
  6. import digital_image_processing.filters.median_filter as med
  7. import digital_image_processing.filters.sobel_filter as sob
  8. import digital_image_processing.filters.convolve as conv
  9. import digital_image_processing.change_contrast as cc
  10. from cv2 import imread, cvtColor, COLOR_BGR2GRAY
  11. from numpy import array, uint8
  12. from PIL import Image
  13. img = imread(r"digital_image_processing/image_data/lena_small.jpg")
  14. gray = cvtColor(img, COLOR_BGR2GRAY)
  15. # Test: change_contrast()
  16. def test_change_contrast():
  17. with Image.open("digital_image_processing/image_data/lena_small.jpg") as img:
  18. # Work around assertion for response
  19. assert str(cc.change_contrast(img, 110)).startswith(
  20. "<PIL.Image.Image image mode=RGB size=100x100 at"
  21. )
  22. # canny.gen_gaussian_kernel()
  23. def test_gen_gaussian_kernel():
  24. resp = canny.gen_gaussian_kernel(9, sigma=1.4)
  25. # Assert ambiguous array
  26. assert resp.all()
  27. # canny.py
  28. def test_canny():
  29. canny_img = imread("digital_image_processing/image_data/lena_small.jpg", 0)
  30. # assert ambiguos array for all == True
  31. assert canny_img.all()
  32. canny_array = canny.canny(canny_img)
  33. # assert canny array for at least one True
  34. assert canny_array.any()
  35. # filters/gaussian_filter.py
  36. def test_gen_gaussian_kernel_filter():
  37. assert gg.gaussian_filter(gray, 5, sigma=0.9).all()
  38. def test_convolve_filter():
  39. # laplace diagonals
  40. Laplace = array([[0.25, 0.5, 0.25], [0.5, -3, 0.5], [0.25, 0.5, 0.25]])
  41. res = conv.img_convolve(gray, Laplace).astype(uint8)
  42. assert res.any()
  43. def test_median_filter():
  44. assert med.median_filter(gray, 3).any()
  45. def test_sobel_filter():
  46. grad, theta = sob.sobel_filter(gray)
  47. assert grad.any() and theta.any()