首页 > 解决方案 > 使用 cv2 在距离上裁剪图像

问题描述

我是深度学习和图像处理的新手,但我已经实现了以下用于拆分阿拉伯字符的代码,但我的代码无法识别字符,它只能找到整个单词。 这是代码检测到的整个单词

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline

image = cv.imread('hamd.png')
height, width, depth = image.shape
print(image.shape)

image = cv.resize(image, dsize=(width*5, height*4), interpolation=cv.INTER_CUBIC)
print(image.shape)

gray = cv.cvtColor(image,cv.COLOR_BGR2GRAY)
ret,thresh = cv.threshold(gray,127,255,cv.THRESH_BINARY_INV)

kernel = np.ones((5,5), np.uint8)
img_dilation = cv.dilate(thresh, kernel, iterations=1)

gsblur=cv.GaussianBlur(img_dilation,(5,5),0)

ctrs, hier = cv.findContours(gsblur.copy(), cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
m = list()
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv.boundingRect(ctr)[0])
pchl = list()
dp = image.copy()
for i, ctr in enumerate(sorted_ctrs):
    x, y, w, h = cv.boundingRect(ctr)
    cv.rectangle(dp,(x-10,y-10),( x + w + 10, y + h + 10 ),(90,0,255),9)
plt.imshow(dp)

现在我想知道如何在两个字符之间的距离上分割字符?我的意思如下图所示: 这就是我想要的

标签: pythonopencvimage-processingdeep-learningcharacter

解决方案


推荐阅读