gpt4 book ai didi

python - 使用 OpenCV 和 Python 从图像中识别和裁剪文本的问题

转载 作者:行者123 更新时间:2023-12-02 17:44:26 24 4
gpt4 key购买 nike

我正在使用取自以下答案的代码: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]上面提到的可能是错误的原因,但没有它我可以让脚本完成。

最佳答案

问题在于 cv2.findContours()方法,实际上它对于 Opencv 2 和 Opencv 3 有不同的返回参数,您必须检查您正在使用的 Opencv 版本的文档,一般来说:

对于Opencv 2 :

contours, hierarchy = cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

对于 Opencv 3 :
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)

关于python - 使用 OpenCV 和 Python 从图像中识别和裁剪文本的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38319205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com