如何使用python在图像中绘制文本

问题描述

我有一张图片

它有一些时间。

但我需要使用 python 将其转换为这个:

这个:

enter image description here

对此

enter image description here

简而言之,我需要使用 python 将该图像中的文本绘制为黑色。我认为 opencv 可能对此有用,但不确定。我需要在任何时候都在同一类型的图像中这样做(就像编辑前的图像,只是不同的数字)。 我认为我们可以使用 canny 来检测数字和点的边缘,然后将它们之间的区域涂成黑色。我不知道这样做的完美方法,但我认为它可能有效。希望得到解决。提前致谢。

解决方法

如前所述,由于相邻的 22 它正在产生问题,所以程序将是这样的

import numpy as np
import matplotlib.pyplot as plt
from skimage.segmentation import flood_fill

img = io.imread('number.png',as_gray=True)

plt.imshow(img,cmap='gray')

enter image description here

您申请 flood fill

ff = flood_fill(img,(0,0),125)
ff[ff != 125] = 0
ff[ff == 125] = 255

plt.imshow(ff,cmap='gray')

enter image description here

最后保存图片

io.imsave('out.png',ff)
,

打开边框像素以区分字符和背景。

import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('j3iIp.jpg',0)
ret,thresh2 = cv2.threshold(img,120,255,cv2.THRESH_BINARY_INV)
cv2.imshow("result",thresh2)
cv2.waitKey(0)
cv2.destroyAllWindows()
img1 = cv2.imwrite("thresh4.jpg",thresh2)
,

我就是这样做的,我建议您进行形态学操作,并寻找去除特定大小的斑点以解决“22”问题的方法。您还可以调整容差值。

import cv2

imgray = cv2.imread('j3iIp.png',0)

point = (0,0)   

src = imgray.copy()

tolerance = 25
connectivity = 4
flags = connectivity
flags |= cv2.FLOODFILL_FIXED_RANGE

cv2.floodFill(src,None,point,255),(tolerance,) * 3,flags)

src = cv2.subtract(255,src) 

cv2.imshow('filled',src)

cv2.waitKey(0)

cv2.imwrite("result.jpg",src)

cv2.destroyAllWindows()

结果是:

enter image description here

,

我第一次尝试使用 cv2.findContours() 但是每个字符内部的空白与锁定在 22 之间的空白之间没有层次差异。 Epsi95 的解决方案中显示的“22 问题”无法使用轮廓解决层级信息。

可以使用 cv2.floodFill() 和水平边缘检测来确定每个对象的种子点来解决该问题。

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

# Fixed colon locations (assumed to be known constants)
Y1,Y2 = (24,48)
X1,X2 = (45,93)

# Plot grayscale image
img = np.uint8(cv2.imread("digits.png",cv2.IMREAD_GRAYSCALE) / 255)
fig,axs = plt.subplots(2)
axs[0].imshow(img,cmap="gray")

# Horizontal edge detection at Y1 to determine the seed points (for flood filling)
kernel = np.array([-1,-1,1,1],dtype=np.int8)  # bbww detector
edge_det = np.correlate(img[Y1,:],kernel,mode="same")  # 2 @ bbWw
edge_xloc = np.flatnonzero(edge_det == 2)
seeds_yx = np.array(
    [
        [Y1,edge_xloc[1]],[Y1,edge_xloc[edge_xloc < X1][-2]],X1],edge_xloc[edge_xloc > X1][1]],edge_xloc[edge_xloc < X2][-2]],X2],edge_xloc[edge_xloc > X2][1]],edge_xloc[-2]],[Y2,]
)
axs[0].plot(seeds_yx[:,seeds_yx[:,0],"ro")

# Flood fill at seed points
for y,x in seeds_yx:
    cv2.floodFill(img,mask=None,seedPoint=(x,y),newVal=0)
axs[1].imshow(img,cmap="gray")
fig.show()

代码结果在这个图中filled digits