Skip to content

Digital image processing doctests #1111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
Closed
45 changes: 45 additions & 0 deletions Maths/ZellersCongruence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import argparse
"""Zellers Congruence Birthday Algorithm
Find out what day of the week you were born on, or any other date.
"""

def zeller(date_input):

weekdays = {
'0': 'Sunday',
'1': 'Monday',
'2': 'Tuesday',
'3': 'Wednesday',
'4': 'Thursday',
'5': 'Friday',
'6': 'Saturday'
}

m = int(date_input[0] + date_input[1])
d = int(date_input[3] + date_input[4])
y = int(date_input[6] + date_input[7] + date_input[8] + date_input[9])

if m <= 2:
y = y - 1
m = m + 12
c = int(str(y)[:2])
k = int(str(y)[2:])

t = int(2.6*m - 5.39)
u = int(c / 4)
v = int(k / 4)
x = d + k
z = t + u + v + x
w = z - (2 * c)

f = round(w%7)

for day in weekdays:
if f == int(day):
print("The date " + date_input + ", is a " + weekdays[day] + ".")

if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Find out what day of the week you were born on, or any other date. Accepts birthday as a string in mm-dd-yyyy or mm/dd/yyyy format.')
parser.add_argument('date_input', type=str, help='Date as a string (mm-dd-yyyy or mm/dd/yyyy)')
args = parser.parse_args()
zeller(args.date_input)
2 changes: 2 additions & 0 deletions digital_image_processing/change_contrast.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def contrast(c: int) -> float:


if __name__ == "__main__":
import doctest
doctest.testmod()
# Load image
with Image.open("image_data/lena.jpg") as img:
# Change contrast to 170
Expand Down
1 change: 0 additions & 1 deletion digital_image_processing/edge_detection/canny.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

PI = 180


def gen_gaussian_kernel(k_size, sigma):
center = k_size // 2
x, y = np.mgrid[0 - center:k_size - center, 0 - center:k_size - center]
Expand Down
2 changes: 1 addition & 1 deletion digital_image_processing/filters/median_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def median_filter(gray_img, mask=3):
# set image borders
bd = int(mask / 2)
# copy image size
median_img = zeros_like(gray)
median_img = zeros_like(gray_img)
for i in range(bd, gray_img.shape[0] - bd):
for j in range(bd, gray_img.shape[1] - bd):
# get mask according with mask
Expand Down
67 changes: 67 additions & 0 deletions digital_image_processing/test_digital_image_processing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""
PyTest's for Digital Image Processing
"""

# add change_contrast
from PIL import Image
import digital_image_processing.change_contrast as cc
# add canny
import digital_image_processing.edge_detection.canny as canny
import cv2
# add gen_gaussian
from cv2 import imread, cvtColor, COLOR_BGR2GRAY, imshow, waitKey
from numpy import array, pi, mgrid, exp, square, zeros, ravel, dot, uint8
import digital_image_processing.filters.gaussian_filter as gg
# add convolve
import digital_image_processing.filters.convolve as conv
# add median
import digital_image_processing.filters.median_filter as med
# add sobel_filter
import digital_image_processing.filters.sobel_filter as sob
# add img
img = imread(r'digital_image_processing/image_data/lena.jpg')
# add gray
gray = cvtColor(img, COLOR_BGR2GRAY)

# Test: change_contrast()
def test_change_contrast():
# Image object
with Image.open("digital_image_processing/image_data/lena.jpg") as img:
# Work around assertion for response
assert str(cc.change_contrast(img, 110)).startswith('<PIL.Image.Image image mode=RGB size=512x512 at')

# Test: canny.gen_gaussian_kernel()
def test_gen_gaussian_kernel():
# get ambiguous array
resp = canny.gen_gaussian_kernel(9, sigma=1.4)
# Assert ambiguous array
assert resp.all()

# Test: canny()
def test_canny():
# read image in gray
canny_img = imread('digital_image_processing/image_data/lena.jpg', 0)
# assert ambiguos array for all == True
assert canny_img.all()
# Get canny array
canny_array = canny.canny(canny_img)
# assert canny array for at least one True
assert canny_array.any()

# Test: filters/gaussian_filter.py
def test_gen_gaussian_kernel_filter():
# Filter 5x5
assert gg.gaussian_filter(gray, 5, sigma=0.9).all()

def test_convolve_filter():
# laplace diagonals
Laplace = array([[0.25, 0.5, 0.25], [0.5, -3, 0.5], [0.25, 0.5, 0.25]])
res = conv.img_convolve(gray, Laplace).astype(uint8)
assert res.any()

def test_median_filter():
assert med.median_filter(gray, 3).any()

def test_sobel_filter():
grad, theta = sob.sobel_filter(gray)
assert grad.any() and theta.any()