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

灰度图有256级灰度,而二值图只有黑白两色。颜色数目大大降低,直观感觉转换效果不会好。其实人眼类似于一个低通滤波器,你看到的并不是一个一个像素点,而是接受的颜色信息是一个区域内的颜色信息的综合效果。

Floyd-Steinberg方法实际是一种dithering的方法,将本像素的颜色信息,通过某种方式抖动到其他像素点上,就可以更好利用颜色的区域效果。

Floyd-Steinberg算法:
在这里插入图片描述
*表示当前像素。周围的4个分数为误差分配比例。
误差=理论值-实际值
理论值是真正的颜色值,而实际值为二值化的值

上代码

import cv2
import numpy as np
import matplotlib.pyplot as plt

plt.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.shape

img_gray0 = cv2.resize(img_gray0, (w//2, h//2))

h, w= img_gray0.shape

plt.figure()
plt.imshow(img_gray0, vmin=0, vmax=255, cmap=plt.get_cmap("Greys"))
plt.title("原图")

img_gray_eq = img_gray0

img_dither = np.zeros((h+1, w+1), dtype=np.float)
img_undither = np.zeros((h, w), dtype=np.uint8)

threshold = 128

for 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] = 255

for i in range(h):
    for j in range(w):
        old_pix = img_dither[i, j]
        if (img_dither[i, j] > threshold):
            new_pix = 255
        else:
            new_pix = 0

        img_dither[i, j] = new_pix

        quant_err = old_pix - new_pix

        if j > 0:
            img_dither[i+1, j-1] = img_dither[i+1, j-1] + quant_err * 3 / 16
        img_dither[i+1, j] = img_dither[i+1, j] + quant_err * 5 / 16
        img_dither[i, j+1] = img_dither[i, j+1] + quant_err * 7 / 16
        img_dither[i+1, j+1] = img_dither[i+1, j+1] + quant_err * 1 / 16

img_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()

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 5
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我会尽力回答您的问题。 Floyd-Steinberg抖动算法是一种常用的像处理算法,可以用来将一张高色彩深度的转换为低色彩深度的片,适用于墨水屏等设备。在QT中,可以通过QImage类来读取和处理片。 以下是使用Floyd-Steinberg抖动算法处理墨水屏片的基本步骤: 1. 打开片 使用QImage类的load()函数打开需要处理的片,例如: ``` QImage image; image.load("image.png"); ``` 2. 转换灰度图像 将彩色转换灰度图像,可以使用QImage类的convertToFormat()函数实现: ``` image = image.convertToFormat(QImage::Format_Grayscale8); ``` 3. 进行抖动处理 使用Floyd-Steinberg抖动算法对灰度图像进行处理,将每个像素点的值转换为0或1,然后将误差传递给周围的像素点。这里可以用一个二维数组来存储每个像素点的值: ``` int width = image.width(); int height = image.height(); int** pixels = new int*[width]; for(int i = 0; i < width; i++) { pixels[i] = new int[height]; } for(int y = 0; y < height; y++) { for(int x = 0; x < width; x++) { int oldPixel = qGray(image.pixel(x, y)); int newPixel = oldPixel > 127 ? 255 : 0; pixels[x][y] = newPixel; int error = oldPixel - newPixel; if(x < width - 1) { pixels[x+1][y] += (int)(error * 7 / 16.0); } if(x > 0 && y < height - 1) { pixels[x-1][y+1] += (int)(error * 3 / 16.0); } if(y < height - 1) { pixels[x][y+1] += (int)(error * 5 / 16.0); } if(x < width - 1 && y < height - 1) { pixels[x+1][y+1] += (int)(error * 1 / 16.0); } } } ``` 4. 保存处理后的片 将处理后的像素点重新转换为QImage格式,并使用save()函数保存为片文件: ``` QImage result(width, height, QImage::Format_Grayscale8); for(int y = 0; y < height; y++) { for(int x = 0; x < width; x++) { result.setPixel(x, y, qRgb(pixels[x][y], pixels[x][y], pixels[x][y])); } } result.save("result.png"); ``` 以上就是使用Floyd-Steinberg抖动算法处理墨水屏片用QT实现的基本步骤,希望对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值