我正在使用从此答案中获取的代码:Detect text region in image using Opencv
我正在使用的代码是:
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
contours = cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)[0] # get contours
index = 0
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) + '.png'
cv2.imwrite(s , cropped)
index = index + 1
# write original image with added contours to disk
file_name ='rec_5.png'
captch_ex(file_name)
最重要的区别是:
contours = cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)[0]
添加了[0]
,因为我一直收到此错误
Traceback (most recent call last):
File "test2.py", line 38, in <module>
captch_ex(file_name)
File "test2.py", line 20, in captch_ex
[x,y,w,h] = cv2.boundingRect(contour)
TypeError: points is not a numpy array, neither a scalar
不幸的是,我找不到源代码,但我在某处读到版本3的此方法发生了变化,现在这是必需的。
我的问题是,当我向这个函数提供一个图像时,我会收到数百张1 px.
宽度的裁剪图像,这些图像无法完成该函数在参考答案中明显解决的问题。
截至目前,我猜测上面提到的额外[0]
可能是导致错误的原因,但如果没有它,我可以让脚本完成。
答案 0 :(得分:1)
问题在于cv2.findContours()
方法,实际上它有不同的Opencv 2和Opencv 3的返回参数,你必须检查你正在使用的Opencv版本的文档,一般来说:
contours, hierarchy = cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
image, contours, hierarchy = cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
现在您不需要[0]
黑客访问轮廓,您可以继续:
for contour in contours:
# get rectangle bounding contour
[x,y,w,h] = cv2.boundingRect(contour)