2020-04-26 09:59:11 +00:00
|
|
|
"""
|
2024-03-13 06:52:41 +00:00
|
|
|
Implemented an algorithm using opencv to tone an image with sepia technique
|
2020-04-26 09:59:11 +00:00
|
|
|
"""
|
2024-03-13 06:52:41 +00:00
|
|
|
|
2020-07-06 07:44:19 +00:00
|
|
|
from cv2 import destroyAllWindows, imread, imshow, waitKey
|
2020-04-26 09:59:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def make_sepia(img, factor: int):
|
2020-06-16 08:09:19 +00:00
|
|
|
"""
|
|
|
|
Function create sepia tone.
|
|
|
|
Source: https://en.wikipedia.org/wiki/Sepia_(color)
|
|
|
|
"""
|
2020-04-26 09:59:11 +00:00
|
|
|
pixel_h, pixel_v = img.shape[0], img.shape[1]
|
|
|
|
|
|
|
|
def to_grayscale(blue, green, red):
|
|
|
|
"""
|
|
|
|
Helper function to create pixel's greyscale representation
|
|
|
|
Src: https://pl.wikipedia.org/wiki/YUV
|
|
|
|
"""
|
|
|
|
return 0.2126 * red + 0.587 * green + 0.114 * blue
|
|
|
|
|
|
|
|
def normalize(value):
|
2021-04-26 05:46:50 +00:00
|
|
|
"""Helper function to normalize R/G/B value -> return 255 if value > 255"""
|
2020-04-26 09:59:11 +00:00
|
|
|
return min(value, 255)
|
|
|
|
|
|
|
|
for i in range(pixel_h):
|
|
|
|
for j in range(pixel_v):
|
|
|
|
greyscale = int(to_grayscale(*img[i][j]))
|
|
|
|
img[i][j] = [
|
|
|
|
normalize(greyscale),
|
|
|
|
normalize(greyscale + factor),
|
|
|
|
normalize(greyscale + 2 * factor),
|
|
|
|
]
|
|
|
|
|
|
|
|
return img
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
# read original image
|
|
|
|
images = {
|
|
|
|
percentage: imread("image_data/lena.jpg", 1) for percentage in (10, 20, 30, 40)
|
|
|
|
}
|
|
|
|
|
|
|
|
for percentage, img in images.items():
|
|
|
|
make_sepia(img, percentage)
|
|
|
|
|
|
|
|
for percentage, img in images.items():
|
|
|
|
imshow(f"Original image with sepia (factor: {percentage})", img)
|
|
|
|
|
|
|
|
waitKey(0)
|
|
|
|
destroyAllWindows()
|