使用Python中的openCV分割手写数字彼此接触以进行MNIST预处理

时间:2019-04-11 20:27:11

标签: python opencv split mnist handwriting-recognition

我想在图像上分割数字。我的主要问题是数字可能会互相影响。因此,所有在网上找到的带有OpenCV轮廓的技术都行不通。

这是我到目前为止已完成的步骤。它与数字分开的图像效果很好,但是数字彼此不接触。我尝试解决的图像之一:

One of the image i try to solve

我正在尝试将其作为预处理步骤的一部分,以便以后可以在MNIST模型中使用该数字。

我的实际代码:

import cv2
import numpy as np


# 3 - detect and extract ROI's
#image = cv2.imread('imgs/01.jpg')
#image = cv2.imread('imgs/03.jpg')
image = cv2.imread('imgs/01_tresh_210.jpg')
#cv2.imshow('i', image)
#cv2.waitKey(0)

# grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow('gray', gray)
cv2.waitKey(0)

# binary
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)
cv2.imshow('thresh', thresh)
cv2.waitKey(0)

# dilation
kernel = np.ones((10, 1), np.uint8)  # values set for this image only - need to change for different images
img_dilation = cv2.dilate(thresh, kernel, iterations=1)
cv2.imshow('dilated', img_dilation)
cv2.waitKey(0)

# find contours
im2, ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# sort contours
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0])

for i, ctr in enumerate(sorted_ctrs):
    # Get bounding box
    x, y, w, h = cv2.boundingRect(ctr)

    # Getting ROI
    roi = image[y:y + h, x:x + w]

    # show ROI
    #cv2.imshow('segment no:'+str(i),roi)
    cv2.imwrite('imgs\\roi\\{}.png'.format(i), roi)
    cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 0), 1)
    #cv2.waitKey(0)

    # save only the ROI's which contain a valid information
    if h > 20 and w > 75:
        cv2.imwrite('roi\\{}.png'.format(i), roi)

cv2.imshow('marked areas', image)
cv2.waitKey(0)

0 个答案:

没有答案