如何从图像中提取前景文本?
How to extract foreground text from an image?
如何使用opencv完美提取下图中的数字文字?文字颜色是动态的。
我会从 pyimage 搜索中查看这个 post,它基本上为您执行此操作:https://www.pyimagesearch.com/2017/07/17/credit-card-ocr-with-opencv-and-python/。肯定有更好的分类器,但这是一个很好的起点。
很简单。以下代码可以高度优化(我做得很快 - @Silencer 可以做到这一点)并且也可以与其他图像一起工作(测试)(对于某些你将不得不调整一些值)。
import cv2
import numpy as np
# import image
image = cv2.imread('image.png')
cv2.imshow('original', image)
cv2.waitKey(0)
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
cv2.imshow('hsv', hsv[:, :, 1])
cv2.waitKey(0)
# this calculate the histogram of the image you input
# if this is under/below a certain value (which depend of the colors in the image), a certain thresh will be choosed among another
hist, bins = np.histogram(hsv.ravel(), 256, [0, 256])
print(hist[-1])
if hist[-1] > 15000:
# binary
ret, thresh = cv2.threshold(hsv[:, :, 0], 55, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
cv2.imshow('second', thresh)
cv2.waitKey(0)
# dilation
kernel = np.ones((1, 1), np.uint8)
img_dilation = cv2.dilate(thresh, kernel, iterations=1)
cv2.imshow('dilated', img_dilation)
cv2.waitKey(0)
# find contours
im2, ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# sort contours
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0])
for i, ctr in enumerate(sorted_ctrs):
# Get bounding box
x, y, w, h = cv2.boundingRect(ctr)
# Getting ROI
roi = image[y:y + h, x:x + w]
# show ROI
# cv2.imshow('segment no:'+str(i),roi)
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
# cv2.waitKey(0)
if w > 15 and h > 15:
cv2.imwrite('roi{}.png'.format(i), roi)
cv2.imshow('marked areas', image)
cv2.waitKey(0)
else:
# binary
ret, thresh = cv2.threshold(hsv[:, :, 0], 55, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
cv2.imshow('second', thresh)
cv2.waitKey(0)
# dilation
kernel = np.ones((1, 1), np.uint8)
img_dilation = cv2.dilate(thresh, kernel, iterations=1)
cv2.imshow('dilated', img_dilation)
cv2.waitKey(0)
# find contours
im2, ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# sort contours
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0])
for i, ctr in enumerate(sorted_ctrs):
# Get bounding box
x, y, w, h = cv2.boundingRect(ctr)
# Getting ROI
roi = image[y:y + h, x:x + w]
# show ROI
# cv2.imshow('segment no:'+str(i),roi)
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
# cv2.waitKey(0)
if w > 15 and h > 15:
cv2.imwrite('roi{}.png'.format(i), roi)
cv2.imshow('marked areas', image)
cv2.waitKey(0)
如何使用opencv完美提取下图中的数字文字?文字颜色是动态的。
我会从 pyimage 搜索中查看这个 post,它基本上为您执行此操作:https://www.pyimagesearch.com/2017/07/17/credit-card-ocr-with-opencv-and-python/。肯定有更好的分类器,但这是一个很好的起点。
很简单。以下代码可以高度优化(我做得很快 - @Silencer 可以做到这一点)并且也可以与其他图像一起工作(测试)(对于某些你将不得不调整一些值)。
import cv2
import numpy as np
# import image
image = cv2.imread('image.png')
cv2.imshow('original', image)
cv2.waitKey(0)
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
cv2.imshow('hsv', hsv[:, :, 1])
cv2.waitKey(0)
# this calculate the histogram of the image you input
# if this is under/below a certain value (which depend of the colors in the image), a certain thresh will be choosed among another
hist, bins = np.histogram(hsv.ravel(), 256, [0, 256])
print(hist[-1])
if hist[-1] > 15000:
# binary
ret, thresh = cv2.threshold(hsv[:, :, 0], 55, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
cv2.imshow('second', thresh)
cv2.waitKey(0)
# dilation
kernel = np.ones((1, 1), np.uint8)
img_dilation = cv2.dilate(thresh, kernel, iterations=1)
cv2.imshow('dilated', img_dilation)
cv2.waitKey(0)
# find contours
im2, ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# sort contours
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0])
for i, ctr in enumerate(sorted_ctrs):
# Get bounding box
x, y, w, h = cv2.boundingRect(ctr)
# Getting ROI
roi = image[y:y + h, x:x + w]
# show ROI
# cv2.imshow('segment no:'+str(i),roi)
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
# cv2.waitKey(0)
if w > 15 and h > 15:
cv2.imwrite('roi{}.png'.format(i), roi)
cv2.imshow('marked areas', image)
cv2.waitKey(0)
else:
# binary
ret, thresh = cv2.threshold(hsv[:, :, 0], 55, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
cv2.imshow('second', thresh)
cv2.waitKey(0)
# dilation
kernel = np.ones((1, 1), np.uint8)
img_dilation = cv2.dilate(thresh, kernel, iterations=1)
cv2.imshow('dilated', img_dilation)
cv2.waitKey(0)
# find contours
im2, ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# sort contours
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0])
for i, ctr in enumerate(sorted_ctrs):
# Get bounding box
x, y, w, h = cv2.boundingRect(ctr)
# Getting ROI
roi = image[y:y + h, x:x + w]
# show ROI
# cv2.imshow('segment no:'+str(i),roi)
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
# cv2.waitKey(0)
if w > 15 and h > 15:
cv2.imwrite('roi{}.png'.format(i), roi)
cv2.imshow('marked areas', image)
cv2.waitKey(0)