# Load image, convert to HSV format, define lower/upper ranges, and perform
# color segmentation to create a binary mask
image = cv2.imread('1.jpg')
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower = np.array([0, 0, 218])
upper = np.array([157, 54, 255])
mask = cv2.inRange(hsv, lower, upper)
使用纵横比过滤连接文本轮廓和删除的非文本轮廓的放大图像
# Create horizontal kernel and dilate to connect text characters
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,3))
dilate = cv2.dilate(mask, kernel, iterations=5)
# Find contours and filter using aspect ratio
# Remove non-text contours by filling in the contour
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
x,y,w,h = cv2.boundingRect(c)
ar = w / float(h)
if ar < 5:
cv2.drawContours(dilate, [c], -1, (0,0,0), -1)
按位和同时遮罩和反转,为OCR的结果做好准备
# Bitwise dilated image with mask, invert, then OCR
result = 255 - cv2.bitwise_and(dilate, mask)
data = pytesseract.image_to_string(result, lang='eng',config=' psm 6')
print(data)
import cv2
def captch_ex(file_name):
img = cv2.imread(file_name)
img_final = cv2.imread(file_name)
img2gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 180, 255, cv2.THRESH_BINARY)
image_final = cv2.bitwise_and(img2gray, img2gray, mask=mask)
ret, new_img = cv2.threshold(image_final, 180, 255, cv2.THRESH_BINARY) # for black text , cv.THRESH_BINARY_INV
'''
line 8 to 12 : Remove noisy portion
'''
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3,
3)) # to manipulate the orientation of dilution , large x means horizonatally dilating more, large y means vertically dilating more
dilated = cv2.dilate(new_img, kernel, iterations=9) # dilate , more the iteration more the dilation
# for cv2.x.x
_, contours, hierarchy = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) # findContours returns 3 variables for getting contours
# for cv3.x.x comment above line and uncomment line below
#image, contours, hierarchy = cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
for contour in contours:
# get rectangle bounding contour
[x, y, w, h] = cv2.boundingRect(contour)
# Don't plot small false positives that aren't text
if w < 35 and h < 35:
continue
# draw rectangle around contour on original image
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 255), 2)
'''
#you can crop image and send to OCR , false detected will return no text :)
cropped = img_final[y :y + h , x : x + w]
s = file_name + '/crop_' + str(index) + '.jpg'
cv2.imwrite(s , cropped)
index = index + 1
'''
# write original image with added contours to disk
cv2.imshow('captcha_result', img)
cv2.waitKey()
file_name = 'your_image.jpg'
captch_ex(file_name)
由于没有人发布完整的解决方案,这里有一个方法。通过观察所需的文本是白色的,并且单词是以水平对齐的方式构造的,我们可以使用颜色分割来提取和OCR字母
执行颜色分割。我们加载图像,转换为HSV格式,定义下限/上限范围,并使用^{} 执行颜色分割以获得二值遮罩
扩展以连接文本字符。我们使用^{} 创建一个水平形状的内核,然后使用^{} 扩展以将单个字母组合成一个轮廓
删除非文本轮廓。我们使用^{} 查找轮廓,并使用aspect ratio进行过滤以删除非文本字符。由于文本处于水平方向,如果确定轮廓小于预定义的纵横比阈值,则我们通过使用^{} 填充轮廓来移除非文本轮廓
执行OCR。我们使用初始遮罩按位和放大图像,以仅分离文本字符,并反转图像,使文本为黑色,背景为白色。最后,我们将图像放入Pytesseract OCR中
以下是每个步骤的可视化:
输入图像
由颜色分割生成的掩模
使用纵横比过滤连接文本轮廓和删除的非文本轮廓的放大图像
按位和同时遮罩和反转,为OCR的结果做好准备
PyteSeract OCR使用
psm 6
配置设置产生的结果,以假定一个统一的文本块。查看here了解更多配置选项完整代码
HSV下/上颜色范围是使用此HSV颜色阈值脚本确定的
如果您不介意弄脏手,您可以尝试将这些文本区域扩展为一个更大的矩形区域,然后一次将其馈送给tesseract
我还建议尝试对图像进行多次阈值设置,然后分别将每个阈值输入tesseract,看看这是否有帮助。您可以将输出与字典单词进行比较,以自动确定特定OCR结果是否良好
相关问题 更多 >
编程相关推荐