opencv learning eighteen: object measurement

Object measurement

The contour features in opencv include: such as area, perimeter, centroid, bounding box, etc.
Polygon Fitting API to
get the polygon fitting results of the contour

The python-opencv API provides methods:
cv2.moments() is used to calculate the central moment in the image (up to the third order),
cv2.HuMoments() is used to calculate the Hu moment from the central moment,
and the function cv2.contourArea() is also used. Calculate the contour area
and cv2.arcLength() to calculate the contour or curve length

cv2.approxPolyDP(contour, epsilon, close)
parameters:
contour contour
epsilon is smaller, the polyline is closer to the real shape
close-whether it is a closed area

The function cv2.boundingRect returns four parameters (x, y) for the coordinates of the upper left corner of the rectangle, and (w, h) is the width and height of the rectangle. The function cv2.rectangle is to draw a rectangle function

The function cv2.minAreaRect returns a Box2D structure , which contains: the coordinates of the upper left corner of the rectangle (x, y), the width and height of the rectangle (w, h), and the rotation angle.
But to draw this rectangle requires 4 corner points of the rectangle, which can be obtained by the function cv2.boxPoints(), and finally draw the rotated bounding rectangle.

The function cv2.minEnclosingCircle can help us find the circumscribed circle of an object. It is the smallest area of ​​any circle that can completely include the object.

The return value of the function cv2.fitEllipse is actually the inscribed circle of the rotating bounding rectangle

Insert picture description hereInsert picture description hereInsert picture description hereGeometric Moment Calculates
an M×N digital image ƒ(i,j), its p+q-order geometric moment mpq and central moment μpq are:
Insert picture description here p+q = 0 is 0-order moment

p+q = 1 is the first moment

p+q = 2 is the second moment

。。。

Among them ƒ(i,j) is the gray value of the image at the coordinate point (i,j).

code show as below:

import cv2 as cv
import numpy as np


def measure_object(image):
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    #灰度图gary转为二值图(黑白图)--->输出ret 阈值、binary 二值图
    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
    print("threshold value : %s"%ret)#打印阈值
    cv.imshow("binary image", binary)#显示二值图像
    dst = cv.cvtColor(binary, cv.COLOR_GRAY2BGR)
    # 找二值图binary的轮廓,cv.RETR_EXTERNAL(只检索外部轮廓)、cv.RETR_TREE(检索全部轮廓)
    contours, hireachy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
    #遍历全部轮廓
    for i, contour in enumerate(contours):
        area = cv.contourArea(contour)# 求轮廓面积
        x, y, w, h = cv.boundingRect(contour)# 求轮廓外接矩形
        rate = min(w, h)/max(w, h)
        print("rectangle rate : %s"%rate)#宽高比
        mm = cv.moments(contour)# 求几何矩,返回字典类型
        print(type(mm))
        # 求得图形的重心坐标
        cx = mm['m10']/mm['m00']
        cy = mm['m01']/mm['m00']
        cv.circle(dst, (np.int(cx), np.int(cy)), 3, (0, 255, 255), -1)#绘制轮廓中心

        cv.rectangle(dst, (x, y), (x+w, y+h), (0, 0, 255), 2)#在原图上,给轮廓绘制矩形
        print("contour area %s"%area)
    cv.imshow("measure-contours", dst)



src = cv.imread("C:/Users/lenovo/Desktop/opencv/daima/banknum/template-matching-ocr/images/ocr_a_reference.png")  #读取图片位置
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
measure_object(src)
cv.waitKey(0)
cv.destroyAllWindows()

Run screenshot:
Insert picture description hereCalculate the area and aspect ratio of each number, which can be used for simple number recognition.
Note that the numbers in the original image are black and need to be de-binarized to make the numbers an outline because the outline is white.
Supplementary knowledge points:
outImg, contours, hireachy = cv2.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) The
function has 3 parameters Arguments (variables)
1. The source image (source image) is generally binary image
2. Contour retrieval mode (contour retrieval mode) ) Generally use cv.RETR_EXTERNAL (only retrieve external contours) cv.RETR_TREE (retrieve all contours)
3. Contour approximation method (contour approximation method) generally use cv.CHAIN_APPROX_SIMPLE

ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
1, gray-scale image converted from source BGR image
2, pixel threshold
3, cv.THRESH_BINARY_INV | cv.THRESH_OTSU-binary value Image conversion method

Guess you like

Origin blog.csdn.net/weixin_44145452/article/details/112794456