Python Opencv:如何在不增加图像大小的情况下增加字母大小?
我想增加图像中的字母大小,包含分布在多行中的字母,并保持它们的坐标不变,同时删除字母之间的行。 例如Python Opencv:如何在不增加图像大小的情况下增加字母大小?,python,opencv,tesseract,Python,Opencv,Tesseract,我想增加图像中的字母大小,包含分布在多行中的字母,并保持它们的坐标不变,同时删除字母之间的行。 例如: 我已经应用了形态变换。它有助于使用tesseract OCR进行字符识别,但还不足以解决此图像中字母4的问题 我已经应用了腐蚀和cv2.MORPH\u CLOSE kernel = np.ones((2,2),np.uint8) erosion = cv2.erode(img,kernel,iterations = 1) kernel = np.ones((3,3),np.uint8) c
: 我已经应用了形态变换。它有助于使用tesseract OCR进行字符识别,但还不足以解决此图像中字母4的问题 我已经应用了
腐蚀和cv2.MORPH\u CLOSE
kernel = np.ones((2,2),np.uint8)
erosion = cv2.erode(img,kernel,iterations = 1)
kernel = np.ones((3,3),np.uint8)
closing = cv2.morphologyEx(erosion, cv2.MORPH_CLOSE, kernel)
我得到这个输出
:
已编辑
输入图像
我使用的完整代码
import cv2
import numpy as np
img = cv2.imread('total_2.png',0)
edges = cv2.Canny(img,50,150,apertureSize = 3)
minLineLength=100
lines = cv2.HoughLinesP(image=edges,rho=1,theta=np.pi/180, threshold=100,lines=np.array([]), minLineLength=minLineLength,maxLineGap=80)
a,b,c = lines.shape
for i in range(a):
x = lines[i][0][0] - lines [i][0][2]
y = lines[i][0][1] - lines [i][0][3]
if x!= 0 and abs(y/x) <1:
cv2.line(img, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (255, 255, 255), 1, cv2.LINE_AA)
se = cv2.getStructuringElement(cv2.MORPH_ELLIPSE , (4,3))
gray = cv2.morphologyEx(img, cv2.MORPH_CLOSE, se)
img = cv2.fastNlMeansDenoising(gray, None, 65, 5, 21)
img = cv2.threshold(img, 128, 255, cv2.THRESH_BINARY_INV)[1]
img = cv2.bitwise_not(img)
k1 = np.zeros((3, 3), np.uint8)
img = cv2.erode(img, k1, iterations = 1)
ret,img = cv2.threshold(img,0,255,0)
element = cv2.getStructuringElement(cv2.cv2.MORPH_RECT,(3,3))
kernel = np.ones((1,1),np.uint8)
#dilation = cv2.dilate(img,element,iterations = 1)
erosion = cv2.erode(img,element,iterations = 1)
kernel = np.ones((3,3),np.uint8)
#opening = cv2.morphologyEx(erosion, cv2.MORPH_OPEN, element)
closing = cv2.morphologyEx(erosion, cv2.MORPH_CLOSE, element)
cv2.findContours
的问题是,我还有其他图像,我对它们应用了相同的预处理技术,但它们是不固定的,这种方法会破坏一些字母
例如,此图像
:
我的理想解决方案是增加字母大小,同时使字母更清晰您已经调低了输入图像的色调并将其转换为二进制图像,从这一点来看,准确恢复丢失的信息似乎有点困难,您能否共享原始图像和生成这些二值图像所使用的代码?通过添加输入图像和生成此二值图像的代码编辑问题您是否考虑过查找轮廓(cv2.findcontours)并使用凸度标准或大小标准过滤轮廓(以消除正方形)然后调整轮廓的大小(乘以点列表),最后在新的图像遮罩上绘制它们。这种方法的问题是,我有其他没有圆角框的图像,因此如果我对非圆角框应用相同的代码,它会删除字符并破坏图像。我补充说,我用于此目的可能需要一些小的修改
# threshold the gray image to binarize, and negate it
_,binary = cv2.threshold(image, 150, 255, cv2.THRESH_BINARY)
if flag :
binary = cv2.bitwise_not(binary)
# find external contours of all shapes
_,contours,_ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# create a mask for floodfill function, see documentation
h,w= image.shape
mask = np.zeros((h+2,w+2), np.uint8)
# determine which contour belongs to a square or rectangle
for cnt in contours:
poly = cv2.approxPolyDP(cnt, 0.05*cv2.arcLength(cnt,True),True)
if len(poly) == 4:
# if the contour has 4 vertices then floodfill that contour with black color
cnt = np.vstack(cnt).squeeze()
_,binary,_,_ = cv2.floodFill(binary, mask, tuple(cnt[0]), 0)
# convert image back to original color
if flag:
binary = cv2.bitwise_not(binary)