gpt4 book ai didi

image - OpenCV - 从图像中删除水平点或线导致图像质量较低

转载 作者:太空宇宙 更新时间:2023-11-03 22:46:22 26 4
gpt4 key购买 nike

我关注 Extract horizontal and vertical lines by using morphological operations删除图像中的水平点。

该解决方案的主要思想是腐 eclipse 图像(去除水平/垂直方向上的细线或点)和扩张。但这会降低图像中字符的质量。

所以我想问一下是否有更好的解决方案来去除点并且不损失图像中字符的质量。

原图:

original image

结果:

result images

代码:

import util
import cv2

grayImage = cv2.imread("00.jpg", 0)

h = float(grayImage.shape[0])

maxVal = 255
blockSize = 15
C = 12.0*(90.0/h)

print("C:" + str(C))

showImages = []

bw = cv2.adaptiveThreshold(grayImage, maxVal, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, blockSize, C)
bw = ~bw

showImages.append(grayImage.copy())
showImages.append(bw.copy())

vertical = bw.copy()

# Specify size on vertical axis
# verticalsize = vertical.shape[0] / 20
verticalsize = 4

# Create structure element for extracting vertical lines through morphology operations
verticalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (1, verticalsize))

# Apply morphology operations
vertical = cv2.erode(vertical, verticalStructure, None, (-1,-1))
showImages.append(vertical.copy())

vertical = cv2.dilate(vertical, verticalStructure, None, (-1,-1))
showImages.append(vertical.copy())

util.showOpenCVImagesGrid(showImages, 2, 2, titles=["grayImage", "adaptiveThreshold", "after erode", "after dilate"])

工具:

import cv2
import matplotlib.pyplot as plt

def showOpenCVImagesGrid(images, x, y, titles=None, axis="on"):
fig = plt.figure()
i = 1

for item in images:
image = None
title = None
if type(images) is list:
image = item
if titles is not None:
title = titles[i - 1]
elif type(images) is dict:
image = images[item]
title = item

if image is None:
i += 1
continue
copy = image.copy()
channel = len(copy.shape)

cmap = None
if channel == 2:
cmap = "gray"
elif channel == 3:
copy = cv2.cvtColor(copy, cv2.COLOR_BGR2RGB)
elif channel == 4:
copy = cv2.cvtColor(copy, cv2.COLOR_BGRA2RGBA)

fig.add_subplot(x, y, i)

plt.title(title)
plt.axis(axis)
plt.imshow(copy, cmap=cmap)
i += 1
plt.show()

最佳答案

如果线条穿过感兴趣的图形,没有神奇的解决方案。

但在这种情况下,它们不需要,使用合适的感兴趣区域就足以处理它们。

例如,您可以只检测点(顶帽过滤器)并拟合一条直线。通过添加合适的边距,您可以将图像分成两个区域,您可以对其应用任何处理。

例如,在下图中,下部已使用 1x5 垂直膨胀进行了清理。你可以猜出 split 边缘在哪里设置。

enter image description here


无论如何,有一个严肃的问题:您是否需要完全摆脱虚线?

关于image - OpenCV - 从图像中删除水平点或线导致图像质量较低,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46645420/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com