The main process of opencv image recognition

function settings

In order to make the process of image recognition more intuitive, we set a function in advance to display the images in each step of the image recognition process.

def cv_show(name,img):
    cv2.imshow(name,img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

Using this function can make each step in the image recognition process more intuitive, and can also be used to detect whether each step is running correctly.

read a template image

The principle of our image recognition is to extract the template of ten numbers from 0-9, and then perform a template matching on the bank card one by one, so as to identify the bank card number we need.

Therefore, our first step is to read the digital template.

img = cv2.imread("模板的路径")
cv_show('img',img)

The next step is to perform a contour detection on the template to obtain the image information of the template.

    • convert to grayscale

ref = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv_show('ref',ref)

The above is the result of converting to grayscale image.

    • convert to binary image

ref = cv2.threshold(ref,10,255,cv2.THRESH_BINARY_INV)[1]
cv_show('ref',ref)

This involves a binary image conversion function cv2.threshold()

First introduce the simple threshold function: cv2.threshold(src, thresh, maxval, type[, dst]), the return value is retval, dst

in:

src is the grayscale image

thresh is the starting threshold

maxval is the maximum value

type is to define how to deal with the relationship between data and threshold. There are the following types:

options

Pixel value>thresh

Other cases

cv2.THRESH_BINARY

maxval

0

cv2.THRESH_BINARY_INV

0

maxval

cv2.THRESH_TRUNC

thresh

current gray value

cv2.THRESH_TOZERO

current gray value

0

cv2.THRESH_TOZERO_INV

0

current gray value

Note: In the above example, we added a [1] at the end, which means we only take the second return value dst, and generally speaking, we will only use the second return value dst of the function

    • Calculate contour

refCnts = cv2.findContours(ref.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[1]
cv2.drawContours(img,refCnts,-1,(0,0,255),3)
cv_show('img',img) 

There are two functions for computing contours used here.

cv2.findContours():

cv2.findContours(image, mode, method, contours=None, hierarchy=None, offset=None)

contour return value

cv2.findContours()函数首先返回一个list,list中每个元素都是图像中的一个轮廓信息,list中每个元素(轮廓信息)类型为ndarray。len(contours[1]) 表示第一个轮廓储存的元素个数,即该轮廓中储存的点的个数。

hierarchy返回值

该函数还可返回一个可选的hiararchy结果,这是一个ndarray,其中的元素个数和轮廓个数相同,每个轮廓contours[i]对应4个hierarchy元素hierarchy[i][0] ~hierarchy[i][3],分别表示后一个轮廓、前一个轮廓、父轮廓、内嵌轮廓的索引编号,如果没有对应项,则该值为-1。

第一个数:表示同一级轮廓的下个轮廓的编号,如果这一级轮廓没有下一个轮廓,一般是这一级轮廓的最后一个的时候,则为-1。

第二个数:表示同一级轮廓的上个轮廓的编号,如果这一级轮廓没有上一个轮廓,一般是这一级轮廓的第一个的时候,则为-1。

第三个数:表示该轮廓包含的下一级轮廓的第一个的编号,假如没有,则为-1。

第四个数: 表示该轮廓的上一级轮廓的编号,假如没有上一级,则为-1。

cv2.drawContours():

cv2.drawContours(image, contours, contourIdx, color, thickness=None, lineType=None, hierarchy=None, maxLevel=None, offset=None)

Guess you like

Origin blog.csdn.net/m0_74043494/article/details/128814089