opencv-python 中对PPT图象进行图象文字增强(重制含原版)

2023-11-16

话不多说直接上代码:

这是原来的代码:

import cv2
import numpy as np

img = cv2.imread('D:\\picture\\dabo\\goal.png')
img = cv2.resize(img, None, fx=0.5, fy=0.5)

b, g, r = cv2.split(img)
bH = cv2.equalizeHist(b)
gH = cv2.equalizeHist(g)
rH = cv2.equalizeHist(r)
result = cv2.merge([bH, gH, rH])
res = np.hstack((img, result))

cv2.imshow('dbao', res)
cv2.waitKey(0)


threshold = 20
h, w = img.shape[:2]
for i in range(0, h):
    for j in range(0, w):
        B = result[i, j, 0]
        G = result[i, j, 1]
        R = result[i, j, 2]
        if B > threshold and G > threshold and R > threshold:
            result[i, j, 0] = 255
            result[i, j, 1] = 255
            result[i, j, 2] = 255
cv2.imshow('dabo', result)
cv2.waitKey(0)

这是改良后的代码:

import cv2
import numpy as np

def xian(img):
    cv2.imshow('dabo', img)
    cv2.waitKey(0)


image = cv2.imread('D:\\picture\\dabo\\goal.png')
image = cv2.resize(image, None, fx=0.5, fy=0.5)
xian(image)

# 方法1
reflect_img = cv2.copyMakeBorder(image, 1, 1, 1, 1, cv2.BORDER_REPLICATE)  # 复制相邻像素来填充边界
x, y = reflect_img.shape[:2]
for depth in range(0, 3):
    for row in range(1, x - 1):
        for col in range(1, y - 1):
            HighPass = (reflect_img.item(row, col, depth) << 2) - reflect_img.item(row - 1, col, depth) \
                       - reflect_img.item(row + 1, col, depth) - reflect_img.item(row, col - 1, depth) \
                       - reflect_img.item(row, col + 1, depth)
            Value = image.item(row - 1, col - 1, depth) + 100 * HighPass // 100
            if Value > 255:
                Value = 255
            elif Value < 0:
                Value = 0
            image.itemset((row - 1, col - 1, depth), Value)

# 方法2
# gaussian = cv2.GaussianBlur(image, (5, 5), 6)
# image = cv2.addWeighted(image, 2, gaussian, -1, 0)

xian(image)

b, g, r = cv2.split(image)  # 拆
bH = cv2.equalizeHist(b)  # 对三个通道图进行直方图均衡化
gH = cv2.equalizeHist(g)
rH = cv2.equalizeHist(r)
result = cv2.merge([bH, gH, rH])  # 合
res = np.hstack((image, result))
xian(res)

threshold = 20  # 设立一个阈值
h, w = image.shape[:2]
for i in range(0, h):
    for j in range(0, w):
        B = result[i, j, 0]
        G = result[i, j, 1]
        R = result[i, j, 2]
        if B > threshold and G > threshold and R > threshold:  # 如果大于阈值就赋予白色,保证黑色留下
            result[i, j, 0] = 255
            result[i, j, 1] = 255
            result[i, j, 2] = 255
xian(result)

 改良后与原先的区别还是很大的,原理是加入USM锐化算法,对模糊的图片进行锐化处理,更好的凸显出文字边缘轮廓。

其中,USM锐化算法有两种方法:

# 方法1
reflect_img = cv2.copyMakeBorder(image, 1, 1, 1, 1, cv2.BORDER_REPLICATE)  # 复制相邻像素来填充边界
x, y = reflect_img.shape[:2]
for depth in range(0, 3):
    for row in range(1, x - 1):
        for col in range(1, y - 1):
            HighPass = (reflect_img.item(row, col, depth) << 2) - reflect_img.item(row - 1, col, depth) \
                       - reflect_img.item(row + 1, col, depth) - reflect_img.item(row, col - 1, depth) \
                       - reflect_img.item(row, col + 1, depth)
            Value = image.item(row - 1, col - 1, depth) + 100 * HighPass // 100
            if Value > 255:
                Value = 255
            elif Value < 0:
                Value = 0
            image.itemset((row - 1, col - 1, depth), Value)
# 方法2
# gaussian = cv2.GaussianBlur(image, (5, 5), 6)
# image = cv2.addWeighted(image, 2, gaussian, -1, 0)

方法二是先进行高斯滤波,再将它与原图片融合,这与方法一有异曲同工之处。这种锐化的方法就是对原图像先做一个高斯模糊,然后用原来的图像减去一个系数乘以高斯模糊之后的图像,然后再把值Scale(图象增强)到0~255的RGB像素值范围之内。基于USM锐化的方法可以去除一些细小的干扰细节和噪声,比一般直接使用卷积锐化算子得到的图像锐化结果更加真实可信。

可参考:

(1条消息) 图像处理之USM锐化_weixin_33911824的博客-CSDN博客

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

opencv-python 中对PPT图象进行图象文字增强(重制含原版) 的相关文章

随机推荐