Saturday, January 21, 2017

Dodge and burn

Dodging has the over-exposure effect, where light pixels tend to be pushed to white.
Burning is the opposite, where dark pixels tend to be pushed to black.

import numpy as np
import cv2


def dodge(image, mask):
    return cv2.divide(image, 255 - mask, scale=256)


def burn(image, mask):
    return 255 - cv2.divide(255 - image, 255 - mask, scale=256)


class PencilSketch:
    def __init__(self, width, height, bg_gray="pencilsketch_bg.jpg"):
        self.width = width
        self.height = height
        self.canvas = cv2.imread(bg_gray, cv2.CV_8UC1)
        if self.canvas is not None:
            self.canvas = cv2.resize(self.canvas, (width, height))

    def render(self, img_rgb):
        img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY)
        img_gray_inv = 255 - img_gray
        img_blur = cv2.GaussianBlur(img_gray_inv, (21, 21), 0, 0)
        img_blend = dodge(img_gray, img_blur)
        return cv2.cvtColor(img_blend, cv2.COLOR_GRAY2RGB)


import matplotlib.pyplot as plt
# img = plt.imread("/Users/kaiyin/PycharmProjects/opencv3blueprints/chapter01/tree.jpg")
img_rgb = cv2.imread("/Users/kaiyin/PycharmProjects/opencv3blueprints/chapter01/tree.jpg", -1)
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY)
# print(img_rgb.shape)
plt.imshow(img_rgb)
plt.imshow(img_gray, cmap="gray")
img_dodge = dodge(img_gray, img_gray)
plt.imshow(img_dodge, cmap="gray")
img_burn = burn(img_gray, img_gray)
img_blur = cv2.GaussianBlur(img_gray, (21, 21), 3)
plt.clf(); plt.imshow(img_blur, cmap="gray")
# effect of dodging: pixels that are brighter than a certain threshold are pushed to 255 (except that 255 is degenerated into 0)
plt.clf(); plt.scatter(img_gray.flatten(), img_dodge.flatten())
plt.clf(); plt.scatter(img_gray.flatten(), img_burn.flatten())
img_dodge1 = dodge(img_gray, img_blur)
img_burn1 = burn(img_gray, img_blur)
plt.clf(); plt.scatter(img_gray.flatten(), img_dodge1.flatten())
plt.clf(); plt.scatter(img_gray.flatten(), img_burn1.flatten())
plt.clf(); plt.imshow(img_dodge1, cmap="gray")
plt.clf(); plt.imshow(img_burn1, cmap="gray")

Dodge

Burn

1 comments:

Oliver Maurice said...

Visit this site for some information about iphone parental control soft