"""Implemented an algorithm using opencv to tone an image with sepia technique"""from cv2 import destroyAllWindows, imread, imshow, waitKeydef make_sepia(img, factor: int):"""Function create sepia tone.Source: https://en.wikipedia.org/wiki/Sepia_(color)"""pixel_h, pixel_v = img.shape[0], img.shape[1]def to_grayscale(blue, green, red):"""Helper function to create pixel's greyscale representationSrc: https://pl.wikipedia.org/wiki/YUV"""return 0.2126 * red + 0.587 * green + 0.114 * bluedef normalize(value):""" Helper function to normalize R/G/B value -> return 255 if value > 255"""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 imgif __name__ == "__main__":# read original imageimages = {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()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。