gpt4 book ai didi

python - 如何使用 Python OpenCV 裁剪图像上的每个字符?

转载 作者:行者123 更新时间:2023-12-02 00:04:48 27 4
gpt4 key购买 nike

我已经生成了这样的 OpenCV 图像

enter image description here

从最后一行代码开始,如何分别裁剪并显示当前图像中的每个字符?

代码

    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's threshold
  • 查找轮廓、从左到右对轮廓进行排序以及使用轮廓区域进行过滤
  • 提取投资返回率
<小时/>

经过大津阈值处理获得二值图像后,我们使用imutils.contours.sort_contours()从左到右对轮廓进行排序。 。这确保了当我们迭代每个轮廓时,每个字符的顺序都是正确的。此外,我们使用最小阈值区域进行过滤以去除小噪声。这是检测到的字符

enter image description here

我们可以使用 Numpy 切片提取每个字符。这是每个已保存角色的投资返回率

enter image description here

import cv2
from imutils import contours

# Load image, grayscale, Otsu's threshold
image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray,0,255,cv2.THRESH_OTSU + cv2.THRESH_BINARY)[1]

# Find contours, sort from left-to-right, then crop
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts, _ = contours.sort_contours(cnts, method="left-to-right")

ROI_number = 0
for c in cnts:
area = cv2.contourArea(c)
if area > 10:
x,y,w,h = cv2.boundingRect(c)
ROI = 255 - image[y:y+h, x:x+w]
cv2.imwrite('ROI_{}.png'.format(ROI_number), ROI)
cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)
ROI_number += 1

cv2.imshow('thresh', thresh)
cv2.imshow('image', image)
cv2.waitKey()

关于python - 如何使用 Python OpenCV 裁剪图像上的每个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60482696/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com