Bank card number identification project - puzzle points

reference:

OpenCV C++ Case Actual Combat Twenty "Bank Card Number Recognition"_Zero___Chen's Blog-CSDN Blog_opencv c++ card detection

Problems in Python: Possible summary and solutions of ValueError: not enough values ​​to unpack (expected x, got x) - Left Hand の Tomorrow's Blog - CSDN Blog

10. Bank card number recognition_Suyuoa's blog-CSDN blog_Bank card number recognition

Python+OpenCV bank card number recognition (including complete code)_nicec1's blog-CSDN blog Bank card number recognition system based on Python+Opencv (with complete code)_Slightly difficult blog-CSDN blog_ref_, refcnts, hierarchy = cv2 .findcontours(ref.co

Since the actual operation of the relevant projects is after the update of the openCV4 version, some grammatical rules such as the use of some functions and the number of parameters have changed before and after the update. Here is a summary of the problems I encountered when completing the project. points and solutions.

My openCV version:

 

 Parameter problem:

When implementing contour detection, you need to use the cv2.findcontours function. In the old version, there are three return values. Many tutorials and codes on the Internet use three return values, and often report errors. Only two returns are required here. value is fine.

digitCnts,hierarchy = cv2.findContours(group.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

 The groupOutput type is wrong:

        In the project, the cv2.putText function needs to be used to print out the detected numbers when finally drawing the outline on the face of the bank card, and the join function that appears in the parameters only allows variables of type str that can be iterated. In some codes , the reason for the error reported in this part is very likely that the type of groupOut is wrong, it may be the list type, so that the error is reported,

    cv2.rectangle(image,(gX-5,gY-5),(gX+gW+5,gY+gH+5),(0,0,255),1)
    cv2.putText(image,''.join(groupOutput),(gX,gY-15),cv2.FONT_HERSHEY_SIMPLEX,0.65,(0,0,255),2)
    cv_show('image',image)
    output.extend(groupOutput)

 You can try to use the traversal method to print one by one

cv2.putText(image_input, "".join((for i in groupOutput)), (gX, gY - 15), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 255), 2)

But when I tried this method, I found that there was a problem with the result

The initial feeling is that there is a problem with the process of obtaining ROI when screening the contour obtained after the contour detection of the image to be detected

Release the code for this part

#计算轮廓
threshCnts,hierarchy = cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = threshCnts
cur_img = image_input.copy()
cv2.drawContours(cur_img,cnts,-1,(0,0,255),3)
cv_show('img',cur_img)
locs = []

#遍历轮廓
for (i,c) in enumerate(cnts):
    (x,y,w,h) = cv2.boundingRect(c)
    ar = w /float(h)

    if 2.5 < ar < 4.0 and (40 < w < 55) and (10 < h < 20):
        locs.append((x,y,w,h))

#将符合的轮廓从左到右排序
locs = sorted(locs, key = lambda ix: ix[0])
output = []

#遍历每一个轮廓中的数字
for (i,(gX,gY,gW,gH)) in enumerate(locs):
    groupOutput = []

    group = gray[gY - 5:gY+gH +5 ,gX-5 :gX + gW +5]
    cv_show('group',group)

    #预处理
    group = cv2.threshold(group,0,255,cv2.THRESH_OTSU)[1]
    cv_show('group',group)

    #计算每一组轮廓
    digitCnts,hierarchy = cv2.findContours(group.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
    digitCnts = imutils.contours.sort_contours(digitCnts,method = 'left-to-right')[0]

    #计算每一组的每个数值
    for c in digitCnts:
        (x,y,w,h) = cv2.boundingRect(c)
        roi = group[y:y + h, x:x + w]
        roi = cv2.resize(roi,(57,88))
        cv_show('roi',roi)
        scores = []
        for (digit,digitROI) in digits.items():
            result = cv2.matchTemplate(roi,digitROI,cv2.TM_CCOEFF)
            (_,score, _, _) = cv2.minMaxLoc(result)
            scores.append(score)
        #获取最合适的数字
        groupOutput.append(str(np.argmax(scores)))

Guess you like

Origin blog.csdn.net/Crabfishhhhh/article/details/128706720