我试图使用OpenCV分割输入图像 . 目的是将图像分成不同的边界矩形(例如:一个用于Aroma的矩形,一个用于CAFE,等等)我稍后将采用每个有界矩形并尝试识别其中的文本 .
我面临的问题是某些图像,findContours函数没有按预期工作 . 我得到了不同的投资回报率,但这不是我所期待的 . PFB一些样品 . 我得到一些缩放的输出 . 有人可以帮我弄清楚我错过了什么
我需要在图像中获取AROMA,在另一个图像中获取CAFE,在另一个图像中获取1211等等?
轮廓数量:108
import cv2
import numpy as np
import matplotlib.pyplot as plt
#import image
image = cv2.imread('receipt-1.jpg')
plt.figure(figsize=(10,10))
#grayscale
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
plt.imshow(gray,cmap='gray')
plt.show()
# Remove blur
image = cv2.medianBlur(image,5)
# Adaptive
thresh = cv2.adaptiveThreshold(gray,255,1,1,11,2)
thresh_color = cv2.cvtColor(thresh,cv2.COLOR_GRAY2BGR)
#dilation
kernel = np.ones((5,5), np.uint8)
img_dilation = cv2.dilate(thresh, kernel, iterations=1)
plt.figure(figsize=(10,10))
plt.imshow(img_dilation)
plt.show()
# find contours
_,ctrs,_ = cv2.findContours(img_dilation,cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
# Sort contours
#sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0])
sorted_ctrs = sorted(ctrs,key=cv2.contourArea)
# print rect
idx = 0
for i, ctr in enumerate(sorted_ctrs):
#if cv2.contourArea(ctr)>1000:
# Get bounding box
x, y, w, h = cv2.boundingRect(ctr)
#print x,x+w,y,y+h
#print ctr
# Getting ROI
roi = thresh[y:y+h, x:x+w]
#cv2.imwrite(str(idx) + '.jpg', roi)
idx+=1
plt.figure(figsize=(10,10))
plt.imshow(roi)
plt.show()
#plt.figure(figsize=(10,10))
# show ROI