zhiqingstudy

Be a young person with knowledge and content

What is image contour?

The image contour is a curve of continuous points with the same color or intensity.
Function of contour:
1. For graphic analysis
2. Object recognition and detection
Note:
1. In order to detect the accuracy, the image needs to be binarized or Canny operated first
2. The input image will be modified when the outline is drawn
Contour lookup api: findContours (img, mode, ApproximationMode)
Two return values, contours and hierarchy
Value of mode:
1、RETR_EXTERNAL=0,表示只检测外轮廓
2、RETR_LIST=1,检测的轮廓不建立等级关系
3、RETR_CCOMP=2,每层最多两级
4、RETR_TREE=3,按树形存储轮廓
Value of ApprovationMode:
1、CHAIN_ APPROX_ NONE, save points on all contours
2、CHAIN_ APPROX_ SIMPLE, save corners only

How to draw a profile

Draw contours api: drawContours (img, contours, contourIdx, color, thickness,...)
Img: picture to be drawn
Contours: coordinate points of these contours
ContourIdx: All contours are sequential. You can set the sequence number to display the contours we want to display. - 1 means to draw all contours
Color: the color of the outline
Thickness: line width, - 1 is full fill

Area and perimeter of contour

The calculation of contour area and perimeter is often encountered in daily life. For example, some small contours of the searched contour are not what we want, so we can filter out the small contours through the contour area.
Api of contour area: contourArea (contour)
Api: arcLength (curve, closed) of contour perimeter
Curve: a contour
Closed: whether the contour is closed, closed is set to true, and not closed is set to false

Polygon Approximation and Convex Hull

What is polygon approximation? What is a convex hull? What is the connection between them? In the following figure, the left effect is polygon approximation, and the right effect is convex hull. For an irregular image, if we draw it when looking for the contour, it is drawn strictly according to the contour, which leads to a very large number. In some cases, we only need to save some feature points, which is the function of polygon approximation, reducing the data storage and saving the feature points of the contour.
Convex hull is to describe the outline, and it is convex, not concave.
Opencv image contour operation, opencv image contour rendering method Polygon approximation api: approxPolyDP(curve,epsilon,closed)
Curve: curve
Epsilon: precision
Closed: Closed or not
Convex hull api: convexHull(points,clockwise,...)
Closed: contour
Clockwise: clockwise or counterclockwise

Circumscribed rectangle

The Opnecv circumscribed rectangle includes two types: the minimum circumscribed rectangle and the maximum circumscribed rectangle. In the following figure, the red rectangle is the smallest circumscribed rectangle of the white image, and the green rectangle is the largest circumscribed matrix of the white image.
Opencv image contour operation, opencv image contour rendering method
Minimum bounding rectangle api: minAreaRect(points)
Points: contour points
The return value of the minimum circumscribed rectangle is RotatedRect, which consists of three important components:
  1. x. Y: starting point
  2. Width, height: width and height
  3. Angle: angle
Maximum bounding rectangle api: boundingRect(array)
Array: contour points
The return value of the largest circumscribed rectangle is Rect. Rect consists of 2 parts, starting point, width and height.

import cv2
import numpy as np

# 画线函数
def drawLine(img,points,line_color):
    index=0
    while index=2000:
        # 绘制轮廓线
        cv2.drawContours(img,contour,-1,(0,0,255),1)
        # 获取多边形逼近的点
        approx=cv2.approxPolyDP(contour,20,True)
        # 获取凸包的点
        hull=cv2.convexHull(contour)
        print(approx,len(approx))
        # 使用多边形逼近的点绘制最小外接矩形
        r=cv2.minAreaRect(approx)
        box=cv2.boxPoints(r)
        box=np.int0(box)
        # 绘制最小外接矩形
        cv2.drawContours(img,[box],-1,(255,0,0),1)
        # 使用多边形逼近的点获得最大外接矩形起始点,宽度和高度
        x,y,w,h=cv2.boundingRect(approx)
        # 绘制最大外接矩形
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),1)

        # 在图片上画多边形逼近和凸包
        drawLine(img,approx,line_color=(0,255,0))
        drawLine(img,hull,line_color=(255,0,0))


# 显示图片
cv2.imshow('img',img)
cv2.imshow('gray',gray)
cv2.imshow('binary',binary)
cv2.waitKey(0)


Code execution rendering:
Opencv image contour operation, opencv image contour rendering method
comment
head sculpture
Code:
Related

Why you shouldn't stay at a job for more than 2 years?

3 harsh facts long-distance relationships

how to keep your girlfriend interested in a long-distance relationship




Unless otherwise specified, all content on this website is original. If the reprinted content infringes on your rights, please contact the administrator to delete it
Contact Email:2380712278@qq.com

Filing number:皖ICP备19012824号