轮廓图像将其分为四个部分(Python,OpenCV)

时间:2018-06-19 14:21:57

标签: python-3.x opencv3.0 opencv-contour

这里绝对是新手...

我正在分析用于在图像中查找文本(四个字母的验证码)的python代码并将其分成四个单独的图像。

请参见下面的代码:

# find the contours (continuous blobs of pixels) the image
contours = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Hack for compatibility with different OpenCV versions
contours = contours[0] if imutils.is_cv2() else contours[1]

letter_image_regions = []

# Now we can loop through each of the four contours and extract the letter
# inside of each one
for contour in contours:
    # Get the rectangle that contains the contour
    (x, y, w, h) = cv2.boundingRect(contour)

    # Compare the width and height of the contour to detect letters that
    # are conjoined into one chunk
    if w / h > 1.25:
        # This contour is too wide to be a single letter!
        # Split it in half into two letter regions!
        half_width = int(w / 2)
        letter_image_regions.append((x, y, half_width, h))
        letter_image_regions.append((x + half_width, y, half_width, h))
    else:
        # This is a normal letter by itself
        letter_image_regions.append((x, y, w, h))

我的目标是修改此代码,以使图像(存储在变量 thresh 中)在水平方向上分成四个相等的部分,并分配给一个 letter_image_regions 数组

我没有运气尝试过的方法是:

letter_image_regions = []
letter_image_regions.append(thresh[0:34, 0:18])
letter_image_regions.append(thresh[0:34, 18:36])
letter_image_regions.append(thresh[0:34, 36:55])
letter_image_regions.append(thresh[0:34, 55:73])

如何修改这段代码,以便在不使用cv2.findContours的情况下将图像的四个水平相等的部分作为数组的轮廓返回(之所以这样,是因为我的某些CAPTCHAS被删除了并且不会被正确地选择)

我正在从事的项目涉及到对公共数据库的自动查询的创建,并且与DDOS或任何其他问题无关。

非常感谢和期待。

0 个答案:

没有答案