我已经生成了这样的OpenCV图像
从最后一行代码开始,我如何裁剪并分别显示当前图像中的每个字符
代码
labels = measure.label(thresh, connectivity=2, background=0)
charCandidates = np.zeros(thresh.shape, dtype="uint8")
for label in np.unique(labels):
if label == 0:
continue
labelMask = np.zeros(thresh.shape, dtype="uint8")
labelMask[labels == label] = 255
cnts = cv2.findContours(labelMask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
if len(cnts) > 0:
c = max(cnts, key=cv2.contourArea)
(boxX, boxY, boxW, boxH) = cv2.boundingRect(c)
aspectRatio = boxW / float(boxH)
solidity = cv2.contourArea(c) / float(boxW * boxH)
heightRatio = boxH / float(crop_frame.shape[0])
keepAspectRatio = aspectRatio < 1.0
keepSolidity = solidity > 0.15
keepHeight = heightRatio > 0.4 and heightRatio < 0.95
if keepAspectRatio and keepSolidity and keepHeight:
hull = cv2.convexHull(c)
cv2.drawContours(charCandidates, [hull], -1, 255, -1)
charCandidates = segmentation.clear_border(charCandidates)
cnts = cv2.findContours(charCandidates.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
cv2.imshow("Original Candidates", charCandidates)
thresh = cv2.bitwise_and(thresh, thresh, mask=charCandidates)
cv2.imshow("Char Threshold", thresh)
多谢各位
这里有一个简单的方法:
在Otsu阈值化以获得二值图像之后,我们使用^{} 从左到右对轮廓进行排序。这确保了当我们遍历每个轮廓时,每个字符的顺序都是正确的。此外,我们使用最小阈值面积来滤除小噪声。以下是检测到的字符
我们可以使用Numpy切片提取每个角色。下面是每个保存的角色ROI
相关问题 更多 >
编程相关推荐