VIB*_*ROT 2 python opencv machine-learning image-processing computer-vision
我有一个如图所示的单词的二值图像,并且我想在不同图像中裁剪每个字符的图像。输出应具有 k、7、2、f、5 和 m 的不同图像。我尝试在 python 中使用 OpenCV,但由于某种原因我无法提取它。如果我可以在每个文本上绘制一个框,那就足够了。
这是一个简单的方法:
在 Otsu 进行阈值处理以获得二值图像后,我们使用 从左到右对轮廓进行排序imutils.contours.sort_contours()
。这确保了当我们迭代每个轮廓时,每个字符的顺序都是正确的。此外,我们使用最小阈值区域进行过滤以去除小噪声。这是检测到的字符
我们可以使用 Numpy 切片提取每个字符。这是每个已保存角色的投资回报率
如果您想要其他方式,只需将其反转即可
ROI = 255 - image[y:y+h, x:x+w]
Run Code Online (Sandbox Code Playgroud)
import cv2
from imutils import contours
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]
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), 1)
ROI_number += 1
cv2.imshow('thresh', thresh)
cv2.imshow('image', image)
cv2.waitKey()
Run Code Online (Sandbox Code Playgroud)