😎 κ³΅λΆ€ν•˜λŠ” μ§•μ§•μ•ŒνŒŒμΉ΄λŠ” μ²˜μŒμ΄μ§€?

[Python 으둜 μ˜μƒμ²˜λ¦¬ (4)] 디더링(Dithering): μ œν•œλœ 색을 μ΄μš©ν•˜μ—¬ μŒμ˜μ΄λ‚˜ 색을 λ‚˜νƒ€λ‚΄λŠ” 것 λ³Έλ¬Έ

πŸ‘©‍πŸ’» IoT (Embedded)/Image Processing

[Python 으둜 μ˜μƒμ²˜λ¦¬ (4)] 디더링(Dithering): μ œν•œλœ 색을 μ΄μš©ν•˜μ—¬ μŒμ˜μ΄λ‚˜ 색을 λ‚˜νƒ€λ‚΄λŠ” 것

μ§•μ§•μ•ŒνŒŒμΉ΄ 2023. 12. 28. 10:58
728x90
λ°˜μ‘ν˜•

πŸ’— 0κ³Ό 1둜 λͺ…μ•” ν‘œν˜„ν•΄λ³΄κΈ°

디더링(Dithering)은 μ œν•œλœ 색을 μ΄μš©ν•˜μ—¬ μŒμ˜μ΄λ‚˜ 색을 λ‚˜νƒ€λ‚΄λŠ” 것이며, μ—¬λŸ¬ 컬러의 색을 μ΅œλŒ€ν•œ λ§žμΆ”λŠ” 과정이닀.κ·Έλž˜ν”½ λ””μžμ΄λ„ˆμ˜ 색채곡간 μ†μ—μ„œ ν•˜λ‚˜μ˜ μ„ΈκΈ°κ°€ μ•„λ‹Œ λ‹€λ₯Έ ν”½μ…€μ„ΈκΈ°λ‘œ κ΅¬μ„±λ˜κΈ° λ•Œλ¬Έμ— λ‚Ÿμ•Œμ΄ λ§Žμ€ κ²ƒμ²˜λŸΌ λ‹€μ†Œ 거칠게 보일 수 μžˆλ‹€

μš”μ¦˜ 잘 μ•ˆμ“°μž„ ^_^

def minmax(pixel):
    if pixel > 255:
        pixel = 255
    if pixel < 0:
        pixel = 0
    return pixel
import numpy as np

def dithering(img):
    height, width = img.shape

    for y in range(0, height-1):
        for x in range(1, width-1):
            p = img[y, x]
            new_p = np.round(p/255) * 255
            img[y, x] = new_p
            error = p - new_p

            img[y  , x+1] = minmax(img[y  , x+1] + error*7/16)
            img[y+1, x-1] = minmax(img[y+1, x-1] + error*3/16)
            img[y+1, x  ] = minmax(img[y+1, x  ] + error*5/16)
            img[y+1, x+1] = minmax(img[y+1, x+1] + error*1/16)

    return img
import cv2

lena = cv2.imread('images/lena.jpg', cv2.IMREAD_GRAYSCALE)
lena_dithering = dithering(lena.copy())
# cv2.imwrite('lena_dithering.jpg', lena_gray_dithering)
cv2.imshow('Lena grayscale', lena)
cv2.imshow('Lena dithering', lena_dithering)
cv2.waitKey(0)
cv2.destroyAllWindows()

728x90
λ°˜μ‘ν˜•
Comments