检测文本之间的间距(OpenCV、Python)

2024-05-18 06:12:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码(实际上这只是运行我正在处理的所有项目所需的4个部分的1部分):

#python classify.py --model models/svm.cpickle --image images/image.png

from __future__ import print_function
from sklearn.externals import joblib
from hog import HOG
import dataset
import argparse
import mahotas
import cv2

ap = argparse.ArgumentParser()
ap.add_argument("-m", "--model", required = True,
    help = "path to where the model will be stored")
ap.add_argument("-i", "--image", required = True,
    help = "path to the image file")
args = vars(ap.parse_args())

model = joblib.load(args["model"])

hog = HOG(orientations = 18, pixelsPerCell = (10, 10),
    cellsPerBlock = (1, 1), transform = True)

image = cv2.imread(args["image"])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(blurred, 30, 150)
(_, cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

cnts = sorted([(c, cv2.boundingRect(c)[0]) for c in cnts], key =
    lambda x: x[1])

for (c, _) in cnts:
    (x, y, w, h) = cv2.boundingRect(c)

    if w >= 7 and h >= 20:
        roi = gray[y:y + h, x:x + w]
        thresh = roi.copy()
        T = mahotas.thresholding.otsu(roi)
        thresh[thresh > T] = 255
        thresh = cv2.bitwise_not(thresh)

        thresh = dataset.deskew(thresh, 20)
        thresh = dataset.center_extent(thresh, (20, 20))

        cv2.imshow("thresh", thresh)

        hist = hog.describe(thresh)
        digit = model.predict([hist])[0]
        print("I think that number is: {}".format(digit))

        cv2.rectangle(image, (x, y), (x + w, y + h),
        (0, 255, 0), 1)
        cv2.putText(image, str(digit), (x - 10, y - 10),
        cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 255, 0), 2)
        cv2.imshow("image", image)
        cv2.waitKey(0)

该代码是从图像中检测和识别手写数字。下面是一个例子:

Image

假设我不在乎准确度识别。在

我的问题是:正如您所看到的,程序获取所有他能看到的数字并在控制台中打印出来。从控制台我可以保存在一个文本文件,如果我想,但我不能告诉程序之间的数字之间有空格。在

Image2

我想要的是,如果我在文本文件中打印数字,它们应该像在图像中那样分开(抱歉,这有点难以解释……)。 数字不应该全部打印在一起(即使在控制台中),但是如果有空白,也要打印空白区域。在

看看冷杉的图片。在前10位数字之后,图像中有一个空白,而控制台中没有。在

总之,这里有一个完整代码的链接。有4个.py文件和3个文件夹。要执行,请在文件夹中打开一个CMD并粘贴命令python classify.py --model models/svm.cpickle --image images/image.png,其中image.png是images文件夹中一个文件的名称。在

Full Code

提前谢谢。在我看来,所有这些工作都必须用神经网络来完成,但我想先用这种方式来尝试。我是新来的。在


Tags: 代码frompyimageimportmodelpngargs
2条回答

这是一个初级解决方案。在

我暂时没有Python中的任何东西,但是转换它应该不难,再加上OpenCV函数调用是相似的,我在下面链接了它们。在


TLDR;

找到边界的中心,然后找出它们之间的距离。如果一个矩形距离某个阈值,你可以假设它是一个空间。在


首先,找出边界矩形的中心

vector<Point2f> centres;

for(size_t index = 0; index < contours.size(); ++index)
{
    Moments moment = moments(contours[index]);

    centres.push_back(Point2f(static_cast<float>(moment.m10/moment.m00), static_cast<float>(moment.m01/moment.m00)));
}

(可选但推荐)

你可以画出中心来直观地了解它们。在

^{2}$

这样,只需迭代它们,确认到下一个的距离在一个合理的阈值内

for(size_t index = 0; index < centres.size(); ++index)
{
    // this is just a sample value. Tweak it around to see which value actually makes sense
    double distance = 0.5;
    Point2f current = centres[index];
    Point2f nextPoint = centres[index + 1];

    // norm calculates the euclidean distance between two points
    if(norm(nextPoint - current) >= distance)
    {
        // TODO: This is a potential space??
    }
}

您可以阅读Python中有关momentsnormcircle drawing调用的更多信息。在

编码愉快, 干杯伙计:)

用这个代码来做这个工作。它检测图像中的文本/数字区域。在

import cv2

image = cv2.imread("C:\\Users\\Bob\\Desktop\\PyHw\\images\\test5.png")
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) # grayscale
_,thresh = cv2.threshold(gray,150,255,cv2.THRESH_BINARY_INV) # threshold
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
dilated = cv2.dilate(thresh,kernel,iterations = 13) # dilate
_, contours, hierarchy = cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) # get contours


idx =0
# for each contour found, draw a rectangle around it on original image
for contour in contours:

    idx += 1

    # get rectangle bounding contour
    [x,y,w,h] = cv2.boundingRect(contour)

    # discard areas that are too large
    if h>300 and w>300:
        continue

    # discard areas that are too small
    if h<40 or w<40:
        continue

    # draw rectangle around contour on original image
    #cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,255),2)

    roi = image[y:y + h, x:x + w]

    cv2.imwrite('C:\\Users\\Bob\\Desktop\\' + str(idx) + '.jpg', roi)

    cv2.imshow('img',roi)
    cv2.waitKey(0)

代码基于另一个问题/答案:Extracting text OpenCV

相关问题 更多 >