现在,我有一个项目使用有关检测数字的图像处理,但是当数字覆盖矩形区域时,我不知道如何提取。
以下是输入内容:
我想得到下面的图片:
答案 0 :(得分:2)
可以使用OpenCV使用轮廓概念提取数字。以下实现是在Python中实现的。
执行以下步骤:
提取轮廓后,需要使用单个凸包。
代码:
img = cv2.imread(r'C:\Users\Jackson\Desktop\2_new.jpg', 1)
img2 = img.copy()
cv2.imshow("original.jpg", img)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, threshed_img = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
image, contours, hier = cv2.findContours(threshed_img, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
#--- Black image to be used to draw individual convex hull ---
black = np.zeros_like(img)
#cv2.imshow("black.jpg", black)
contours = sorted(contours, key=lambda ctr: cv2.boundingRect(ctr)[0]) #added by OP : this sorts contours left to right, so images come in order
for cnt in contours:
if cv2.contourArea(cnt) > 200:
hull = cv2.convexHull(cnt)
black2 = black.copy()
#--- Here is where I am filling the contour after finding the convex hull ---
cv2.drawContours(black2, [hull], -1, (255, 255, 255), -1)
g2 = cv2.cvtColor(black2, cv2.COLOR_BGR2GRAY)
_, t2 = cv2.threshold(g2, 127, 255, cv2.THRESH_BINARY)
cv2.imshow("t2.jpg", t2)
masked = cv2.bitwise_and(img2, img2, mask = t2)
cv2.imshow("masked.jpg", masked)
print(len(hull))
cv2.waitKey(0)
cv2.destroyAllWindows()
您应该能够获得以下内容: