OpenCV study notes 05--graphic detection--contour detection

Introduction: In digital image processing, detecting local feature information in an image is an important part, because sometimes we are not interested in the entire image, but just want to extract part of the information in the image, such as license plate recognition, for For the whole image, what we are interested in is only the part of the license plate, and other information is redundant. So, is there any way to help us realize the method of extracting local information? There are some methods encapsulated in the opencv library, and we only need to call these methods to achieve our goal. Let's talk about the meaning of the function first, and then practice it to better understand the usage of the function.

1. Contour detection

        How to understand the word outline, for example, take the world map, if we only want to get the Chinese plate, then we will frame the border of China, which is an outline relative to the world map. Another example is that we have a map of China, and we want to get the area of ​​our capital Beijing, then we can just select the border belonging to Beijing, which is the outline. If we now have an image containing a person, and our target is the person in the image, then we can frame the person through contour detection.

        The function used for contour detection is:

contours, hierarchy = cv2.findContours(img, mode, method)

        Parameter Description:

img: Our target image, that is, which image we want to operate on. It should be noted that the image must be a single-channel binary image

mode: The mode in which we detect contours. There may be many contours in an image, so we need to specify which contours to detect. Its modes mainly include:

        RETR_EXTERNAL: Only detect the outermost contours
        RETR_LIST: Detect all contours, do not establish the relationship between contours
        RETR_CCOMP: Detect all contours, establish a two-level contour hierarchical relationship
        RETR_TREE: Retrieve all contours, establish a tree-structured hierarchical relationship

method: The method by which we store the points of the detected contour, that is, whether we store all the points or some of the points of the contour. The methods mainly include:

        CHAIN_APPROX_NONE: Stores all points on the contour
        CHAIN_APPROX_SIMPLE: Stores the endpoints of horizontal, vertical, or diagonal contours

Contours: It is used to save all the contour points we detected, and each point is represented by x, y coordinates, then all the detected contour points are stored in contours, which is a list.

hierarchy: The hierarchical relationship between contours.

         Generally speaking, after we detect the contour, if we want to visualize the contour we detected, then we need to draw the detected contour. opencv also provides a method for drawing the contour.

        The method to draw the outline:

img = cv2.drawContours(img, contours, contourIdx, color, thickness, lineType, hierarchy, offset, img)

There are a lot of parameters, you don’t need to memorize them by rote, only a few parameters are usually used.

img: We want to draw the outline on the img image, this image can be multi-channel or single-channel, unlimited

contours: This is the list of contour points we saved in the above contour detection

contourIdx: The index to draw the contour, because contours is a list with more than one contour in it, if we want to draw a certain contour, we need to specify an index, if it is -1, it will draw all contours

color: The color of the drawn outline, which is composed of a GBR three-channel number. The first four are mandatory parameters, and the latter are optional parameters

thickness: the thickness of the drawn line

lineType: the type of line drawn

hierarchy: This is the hierarchical relationship of the contours we saved during the contour detection above

offset: the overall offset of the drawn outline

img: Its function is to enable our original image to be drawn with an outline.

        After analyzing the above two methods, let's practice it and see how it works. The image I used is the one below.

        code show as below:

import cv2
# 读取图片的位置信息
filename = r'./picture/img.png'
# 读取图片,cv2.IMREAD_UNCHANGED意思是不改变读取的图片的类型
img = cv2.imread(filename, cv2.IMREAD_UNCHANGED)
cv2.imshow('img', img)
# 由于轮廓检测必须是二值图像,所以我们先将其转换为灰度图像
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 再经阈值处理处理为二值图像,cv2.THRESH_OTSU的意思是我这里不设定阈值,由函数自己选定一个最合适的阈值
reval, img_gray_two = cv2.threshold(img_gray, 0, 255, cv2.THRESH_OTSU)
cv2.imshow('img_gray_two', img_gray_two)
# 在二值图像上进行轮廓检测,cv2.RETR_LIST检测所有轮廓,cv2.CHAIN_APPROX_NONE保存所有轮廓点
contours, hierarchy = cv2.findContours(img_gray_two, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
# 在彩色图像上绘制我们的轮廓,-1表示绘制所有轮廓,(0, 255, 0)按照BGR表示颜色是绿色,线的粗细程度为5
img_con = cv2.drawContours(img, contours, -1, (0, 255, 0), 5)
cv2.imshow('img_con', img_con)
cv2.waitKey()
cv2.destroyAllWindows()

        I have commented on each line of code. If there are functions and methods that you don’t understand, you can read my previous notes. These functions are all mentioned.

        The result of the operation is as follows:

 

Guess you like

Origin blog.csdn.net/BaoITcore/article/details/124947629