dither是一种图像增强方法,主要利用了错误消散技术。
import cv2
import numpy as np
img = cv2.imread('Portal_Companion_Cube.jpg')
cv2.imshow('src', img)
print(type(img))
r_channel = img[:,:,0]
g_channel = img[:,:,1]
b_channel = img[:,:,2]
r,g,b = cv2.split(img)
print(img.shape)
print(img.size)
[img_height, img_width, img_channel] = img.shape
print(img_height)
print(img_width)
#binary image
for h in range(img_height):
for w in range(img_width):
if g_channel[h,w] > 96:
g_channel[h,w] = 255
else:
g_channel[h,w] = 0
#dither process
for h in range(img_height - 1):
for w in range(img_width - 1):
if g[h,w] < 96:
error = g[h,w] - 0
g[h, w] = 0
g[h, w + 1] = g[h, w + 1] + error
if g[h, w + 1] > 255:
g[h, w + 1] = 255
if g[h, w + 1] < 0:
g[h, w + 1] = 0
else:
error = g[h, w] - 255
g[h,w] = 255
g[h, w + 1] = g[h, w + 1] + error
if g[h, w + 1] > 255:
g[h, w + 1] = 255
if g[h, w + 1] < 0:
g[h, w + 1] = 0
cv2.imshow('gray', g_channel)
cv2.imshow('dither', g)
cv2.waitKey(0)
cv2.destroyAllWindows()