Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python OpenCV findcontours()无法检测轮廓_Python_Opencv - Fatal编程技术网

Python OpenCV findcontours()无法检测轮廓

Python OpenCV findcontours()无法检测轮廓,python,opencv,Python,Opencv,我正试图从下面的图像中读取中的数字。 我使用OpenCV中的模板匹配来查找与图像对应的数字。因此,中的问题分为三个步骤: 第1步:检测MICR E-13B字体,使用FindOntours()和检测矩形即可轻松完成。实现以下输出。 第2步:在第2步中,我们从上面获取每个轮廓(包含MICR E-13B字体),以便将它们分开,以便进一步单独断开数字。 步骤3:这就是错误发生的地方。获得上述输出后,现在的任务是将数字组分解为单个数字,以便使用OpenCV中的模板匹配来匹配它们。但当我应用findco

我正试图从下面的图像中读取中的数字。

我使用OpenCV中的模板匹配来查找与图像对应的数字。因此,中的问题分为三个步骤:
第1步:检测MICR E-13B字体,使用FindOntours()和检测矩形即可轻松完成。实现以下输出。

第2步:在第2步中,我们从上面获取每个轮廓(包含MICR E-13B字体),以便将它们分开,以便进一步单独断开数字。
步骤3:
这就是错误发生的地方。
获得上述输出后,现在的任务是将数字组分解为单个数字,以便使用OpenCV中的模板匹配来匹配它们。但当我应用findcontour方法并继续为每个轮廓检测boundRect时,我没有得到任何边界矩形。
以下是各个步骤的代码:
步骤1:

# apply a tophat (whitehat) morphological operator to find light
# regions against a dark background
tophat = cv2.morphologyEx(gray, cv2.MORPH_TOPHAT, rectKernel)

# compute the Scharr gradient of the tophat image, then scale
# the rest back into the range [0, 255]
gradX = cv2.Sobel(tophat, ddepth=cv2.CV_32F, dx=1, dy=0,
    ksize=-1)
gradX = np.absolute(gradX)
(minVal, maxVal) = (np.min(gradX), np.max(gradX))
gradX = (255 * ((gradX - minVal) / (maxVal - minVal)))
gradX = gradX.astype("uint8")

# apply a closing operation using the rectangular kernel to help
# cloes gaps in between credit card number digits, then apply
# Otsu's thresholding method to binarize the image
gradX = cv2.morphologyEx(gradX, cv2.MORPH_CLOSE, rectKernel)
thresh = cv2.threshold(gradX, 0, 255,
    cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

# apply a second closing operation to the binary image, again
# to help close gaps between credit card number regions
thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, sqKernel)

thresh = cv2.dilate(thresh, None, iterations = 3)
clone = np.dstack([thresh.copy()] * 3
# find contours in the thresholded image, then initialize the
# list of digit locations
cnts = cv2.findContours(thresh.copy(), cv2.RETR_TREE,
    cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
locs = []

# loop over the contours
for (i, c) in enumerate(cnts):
    (x, y, w, h) = cv2.boundingRect(c)
    if i<=3:   #First 4 images are the MICR font
        cv2.rectangle(clone, (x,y), (x+w, y+h), (255,0,0), 1) #color is set to blue but it shows white (error?)
        locs.append((x, y, w, h))
locs = sorted(locs, key=lambda x:x[0])
output = []
print (locs)

for (i, (gX, gY, gW, gH)) in enumerate(locs):
    if i==0:
        group = gray[gY:gY+gH, gX:gX+gW]
        cv2.rectangle(gray, (gX, gY), (gX+gW, gY+gH), (255,0,0), 1)
        group = cv2.threshold(group, 0, 255,
            cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

         # re-initialize the clone image so we can draw on it again
         clone = np.dstack([group.copy()]*3)
         cv2.rectangle(clone, (gX, gY), (gX+gW, gY+gH), (255,0,0), 1)
         cv2.imshow('Image5', clone)
         cv2.waitKey()
# detect the contours of each individual digit in the group,
# then sort the digit contours from left to right
digitCnts = cv2.findContours(group.copy(), cv2.RETR_TREE,
        cv2.CHAIN_APPROX_SIMPLE)
digitCnts = digitCnts[0] if imutils.is_cv2() else digitCnts[1]
cv2.drawContours(clone, digitCnts, -1, (0,255,0), 1)
cv2.imshow('Image6', clone)
cv2.waitKey()
for c in digitCnts:
    # compute the bounding box of the individual digit, extract
    # the digit, and resize it to have the same fixed size as
    # the reference MICR images
    (x, y, w, h) = cv2.boundingRect(c)
    print (x, y, x+w, y+h)
    cv2.rectangle(clone, (x,y), (x+w, y+h), (255,0,0), 3)
    cv2.imshow('Image7', clone) 
    cv2.waitKey()
错误输出:绿色遮罩未正确绑定矩形。

第三步:

# apply a tophat (whitehat) morphological operator to find light
# regions against a dark background
tophat = cv2.morphologyEx(gray, cv2.MORPH_TOPHAT, rectKernel)

# compute the Scharr gradient of the tophat image, then scale
# the rest back into the range [0, 255]
gradX = cv2.Sobel(tophat, ddepth=cv2.CV_32F, dx=1, dy=0,
    ksize=-1)
gradX = np.absolute(gradX)
(minVal, maxVal) = (np.min(gradX), np.max(gradX))
gradX = (255 * ((gradX - minVal) / (maxVal - minVal)))
gradX = gradX.astype("uint8")

# apply a closing operation using the rectangular kernel to help
# cloes gaps in between credit card number digits, then apply
# Otsu's thresholding method to binarize the image
gradX = cv2.morphologyEx(gradX, cv2.MORPH_CLOSE, rectKernel)
thresh = cv2.threshold(gradX, 0, 255,
    cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

# apply a second closing operation to the binary image, again
# to help close gaps between credit card number regions
thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, sqKernel)

thresh = cv2.dilate(thresh, None, iterations = 3)
clone = np.dstack([thresh.copy()] * 3
# find contours in the thresholded image, then initialize the
# list of digit locations
cnts = cv2.findContours(thresh.copy(), cv2.RETR_TREE,
    cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
locs = []

# loop over the contours
for (i, c) in enumerate(cnts):
    (x, y, w, h) = cv2.boundingRect(c)
    if i<=3:   #First 4 images are the MICR font
        cv2.rectangle(clone, (x,y), (x+w, y+h), (255,0,0), 1) #color is set to blue but it shows white (error?)
        locs.append((x, y, w, h))
locs = sorted(locs, key=lambda x:x[0])
output = []
print (locs)

for (i, (gX, gY, gW, gH)) in enumerate(locs):
    if i==0:
        group = gray[gY:gY+gH, gX:gX+gW]
        cv2.rectangle(gray, (gX, gY), (gX+gW, gY+gH), (255,0,0), 1)
        group = cv2.threshold(group, 0, 255,
            cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

         # re-initialize the clone image so we can draw on it again
         clone = np.dstack([group.copy()]*3)
         cv2.rectangle(clone, (gX, gY), (gX+gW, gY+gH), (255,0,0), 1)
         cv2.imshow('Image5', clone)
         cv2.waitKey()
# detect the contours of each individual digit in the group,
# then sort the digit contours from left to right
digitCnts = cv2.findContours(group.copy(), cv2.RETR_TREE,
        cv2.CHAIN_APPROX_SIMPLE)
digitCnts = digitCnts[0] if imutils.is_cv2() else digitCnts[1]
cv2.drawContours(clone, digitCnts, -1, (0,255,0), 1)
cv2.imshow('Image6', clone)
cv2.waitKey()
for c in digitCnts:
    # compute the bounding box of the individual digit, extract
    # the digit, and resize it to have the same fixed size as
    # the reference MICR images
    (x, y, w, h) = cv2.boundingRect(c)
    print (x, y, x+w, y+h)
    cv2.rectangle(clone, (x,y), (x+w, y+h), (255,0,0), 3)
    cv2.imshow('Image7', clone) 
    cv2.waitKey()
错误输出


这就是产生错误的地方,因为应该将步骤2的输出分割为单独的数字,如“9”、“5”等,这些数字可以传递到模板匹配。但等高线的长度为11,并且在上一步的输出中没有绘制边界框来表示单独的数字。

前景应为白色,背景应为黑色。调用
findContours
之前,反转图像(
255-image
)。此外,图像应该只有黑白,但我看到一些灰色区域。一定要创建一个二进制掩码@Miki,我编辑了代码和图像,使前景为白色,背景为黑色。但是,检测所有MICR数字并应用
findContours
后的输出会产生错误
绿色掩码
,并且使用上述轮廓进一步分离数字的
红色掩码
也会产生错误。