Open CV系列学习笔记(十九)对象测量 2021-02-13

本文介绍了使用OpenCV进行对象检测和测量的基本步骤,包括对象检测的概念、对象测量的方法如弧长、面积计算,以及多边形拟合和几何矩计算。通过示例代码展示了如何应用这些技术来识别图像中的对象并分析其轮廓特征,包括轮廓的面积、边界框比例以及轮廓逼近真实形状的程度。
摘要由CSDN通过智能技术生成

Open CV系列学习笔记(十九)对象测量

对象测量

对象检测是指利用图像处理与模式识别等领域的理论和方法,检测出图像中存在的目标对象,确定这些目标对象的语义类别,并标定出目标对象在图像中的位置。对象检测是对象识别的前提。只有检测到对象才能对对象进行识别。
在计算机视觉研究领域,对象检测一般可以分为图形对象检测和视频对象检测,利用图像处理与模式识别等领域的理论和方法,从图像或视频中分离出有一定意义的实体——对象,如人、物体等。在对象检测中有一类通用的对象检测方法, 似物性度量(Objectness measure), 利用矩形框将图像中所有可能存在的对象区域定位出来并给出这个窗口内包含对象的概率。例如,通过贝叶斯框架将多种图像信息进行融合, 定量地计算出每个窗口包含对象的概率。
弧长与面积
多边形拟合
几何矩计算
相关API代码演示
轮廓发现
计算每个轮廓的弧长与面积, 像素单位
多边形拟合
获取轮廓的多边形拟合结果
-approxPolyDP
-contour
-epsilon越小越折线越逼近真实形状
-close – 是否为闭合区域
几何矩计算
在这里插入图片描述
代码:

def measure_object(image):
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    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)
    cloneImage ,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)
        approxCurve = cv.approxPolyDP(contour,4, True)
        print(approxCurve.shape)
        if approxCurve.shape[0] > 6:#判断形状并绘制
            cv.drawContours(dst, contours, i, (0, 255, 0), 2)
        if approxCurve.shape[0] == 4:
            cv.drawContours(dst, contours, i, (0, 0, 255), 2)
        if approxCurve.shape[0] == 3:
            cv.drawContours(dst, contours, i, (255, 0, 0), 2)
        if approxCurve.shape[0] == 5:
            cv.drawContours(dst, contours, i, (255, 255, 0), 2)
        if approxCurve.shape[0] == 6:
            cv.drawContours(dst, contours, i, (255, 0, 255), 2)
    cv.imshow("measure-contours", dst)

结果:

在这里插入图片描述
在这里插入图片描述
--------- Python OpenCV Tutorial ---------
threshold value : 137.0
rectangle rate : 0.8615384615384616
<class ‘dict’>
contour area 2510.0
(5, 1, 2)
rectangle rate : 0.9375
<class ‘dict’>
contour area 1971.0
(4, 1, 2)
rectangle rate : 0.9692307692307692
<class ‘dict’>
contour area 3010.0
(6, 1, 2)
rectangle rate : 0.6
<class ‘dict’>
contour area 1908.5
(8, 1, 2)
rectangle rate : 0.5054945054945055
<class ‘dict’>
contour area 4050.0
(4, 1, 2)
rectangle rate : 0.96
<class ‘dict’>
contour area 1799.5
(8, 1, 2)
rectangle rate : 0.6923076923076923
<class ‘dict’>
contour area 2143.0
(3, 1, 2)
rectangle rate : 0.9423076923076923
<class ‘dict’>
contour area 2447.0
(4, 1, 2)
完整代码:

import cv2 as cv
import numpy as np


def measure_object(image):
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    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)
    cloneImage ,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)
        approxCurve = cv.approxPolyDP(contour,4, True)
        print(approxCurve.shape)
        if approxCurve.shape[0] > 6:#判断形状并绘制
            cv.drawContours(dst, contours, i, (0, 255, 0), 2)
        if approxCurve.shape[0] == 4:
            cv.drawContours(dst, contours, i, (0, 0, 255), 2)
        if approxCurve.shape[0] == 3:
            cv.drawContours(dst, contours, i, (255, 0, 0), 2)
        if approxCurve.shape[0] == 5:
            cv.drawContours(dst, contours, i, (255, 255, 0), 2)
        if approxCurve.shape[0] == 6:
            cv.drawContours(dst, contours, i, (255, 0, 255), 2)
    cv.imshow("measure-contours", dst)


print("--------- Python OpenCV Tutorial ---------")
src = cv.imread("E:/picture/37.jpg")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
measure_object(src)
cv.waitKey(0)

cv.destroyAllWindows()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值