#IMAGE DITHERING BASED ON Floyd-Steinberg METHOD
#author : bharath kotari
#date :18-1-2018
import cv2
import numpy as np
def set_pixel(im,x,y,new):
im[x,y]=new
def quantize(im):
for y in range(0,height-1):
for x in range(1,width-1):
old_pixel=im[x,y]
if old_pixel<127:
new_pixel=0
else:
new_pixel=255
set_pixel(im,x,y,new_pixel)
quant_err=old_pixel-new_pixel
set_pixel(im,x+1,y,im[x+1,y]+quant_err*w1)
set_pixel(im,x-1,y+1, im[x-1,y+1] + quant_err*w2 )
set_pixel(im,x,y+1, im[x,y+1] + quant_err * w3 )
set_pixel(im,x+1,y+1, im[x+1,y+1] + quant_err * w4 )
return im
img=cv2.imread("/home/user/Downloads/blender_images/truck.jpg")
gray=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img2=img.copy()
width,height,z=img.shape
w1=7/16.0
#print w1
w2=3/16.0
w3=5/16.0
w4=1/16.0
blue=img[:,:,0]
blue=quantize(blue)
green=img[:,:,1]
green=quantize(green)
red=img[:,:,2]
red=quantize(red)
gray1= quantize(gray)
image = cv2.merge((blue, green, red))
cv2.imshow('original',img2)
cv2.imshow('merged',image)
cv2.imshow('gray',gray1)
cv2.waitKey(0)
THANKYOU。
答案 0 :(得分:1)
由于整数溢出而发生斑点。 cv2.imread()表示使用无符号8位整数(参见numpy.uint8)的图像,其最大值为255,如果传递该值,则从零开始。例如:255 + 50 = 49.添加错误时,会在代码中发生这种情况。
有几种方法可以避免这种情况。最简单的方法是确保你的值不超过255.