日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

利用Floyd-Steinberg方法(dithering),将灰度图转换为二值图

發(fā)布時間:2023/12/31 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 利用Floyd-Steinberg方法(dithering),将灰度图转换为二值图 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

灰度圖有256級灰度,而二值圖只有黑白兩色。顏色數目大大降低,直觀感覺轉換效果不會好。其實人眼類似于一個低通濾波器,你看到的并不是一個一個像素點,而是接受的顏色信息是一個區(qū)域內的顏色信息的綜合效果。

Floyd-Steinberg方法實際是一種dithering的方法,將本像素的顏色信息,通過某種方式抖動到其他像素點上,就可以更好利用顏色的區(qū)域效果。

Floyd-Steinberg算法:

*表示當前像素。周圍的4個分數為誤差分配比例。
誤差=理論值-實際值
理論值是真正的顏色值,而實際值為二值化的值

上代碼

import cv2 import numpy as np import matplotlib.pyplot as pltplt.rcParams['font.sans-serif'] = ['KaiTi'] # 指定默認字體 plt.rcParams['axes.unicode_minus'] = False # 解決保存圖像是負號'-'顯示為方塊的問題img_gray0 = cv2.imread("img/david_head.jpg", cv2.IMREAD_GRAYSCALE) img_gray0 = 255 - img_gray0 h, w= img_gray0.shapeimg_gray0 = cv2.resize(img_gray0, (w//2, h//2))h, w= img_gray0.shapeplt.figure() plt.imshow(img_gray0, vmin=0, vmax=255, cmap=plt.get_cmap("Greys")) plt.title("原圖")img_gray_eq = img_gray0img_dither = np.zeros((h+1, w+1), dtype=np.float) img_undither = np.zeros((h, w), dtype=np.uint8)threshold = 128for i in range(h):for j in range(w):img_dither[i, j] = img_gray_eq[i, j]if img_gray_eq[i, j] > threshold:img_undither[i, j] = 255for i in range(h):for j in range(w):old_pix = img_dither[i, j]if (img_dither[i, j] > threshold):new_pix = 255else:new_pix = 0img_dither[i, j] = new_pixquant_err = old_pix - new_pixif j > 0:img_dither[i+1, j-1] = img_dither[i+1, j-1] + quant_err * 3 / 16img_dither[i+1, j] = img_dither[i+1, j] + quant_err * 5 / 16img_dither[i, j+1] = img_dither[i, j+1] + quant_err * 7 / 16img_dither[i+1, j+1] = img_dither[i+1, j+1] + quant_err * 1 / 16img_dither = img_dither.astype(np.uint8) img_dither = img_dither[0:h, 0:w]plt.figure() plt.imshow(img_dither, vmin=0, vmax=255, cmap=plt.get_cmap("Greys")) plt.title("dither")plt.figure() plt.imshow(img_undither, vmin=0, vmax=255, cmap=plt.get_cmap("Greys")) plt.title("undither")plt.show()



總結

以上是生活随笔為你收集整理的利用Floyd-Steinberg方法(dithering),将灰度图转换为二值图的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。