zoukankan      html  css  js  c++  java
  • opencv 图像轮廓特征 图像面积,轮廓周长,外接矩形、最小外接矩形、最小外接圆、拟合椭圆

    找出图像轮廓
    contours, hierarchy = cv.findContours(thresh, 3, 2)

    画出图像轮廓
    cnt = contours[1]
    cv.drawContours(img_color1, [cnt], 0, (0, 0, 255), 2)

    计算轮廓面积
    area = cv.contourArea(cnt) #cnt:轮廓
    print(area)

    计算轮廓周长
    perimeter = cv.arcLength(cnt, True) #参数2:表示轮廓是否封闭
    print(perimeter)

    图像矩
    M = cv.moments(cnt)
    print(M)
    print(M['m00']) # 轮廓面积
    cx, cy = M['m10'] / M['m00'], M['m01'] / M['m00'] # 轮廓质心
    print(cx, cy)

    图像外接矩形
    x, y, w, h = cv.boundingRect(cnt) # 外接矩形
    cv.rectangle(img_color1, (x, y), (x + w, y + h), (0, 255, 0), 2)

    最小外接矩形
    rect = cv.minAreaRect(cnt) # 最小外接矩形
    box = np.int0(cv.boxPoints(rect)) # 矩形的四个角点并取整
    cv.drawContours(img_color1, [box], 0, (255, 0, 0), 2)

    最小外接圆
    (x, y), radius = cv.minEnclosingCircle(cnt)
    (x, y, radius) = map(int, (x, y, radius)) # 这也是取整的一种方式噢
    cv.circle(img_color2, (x, y), radius, (0, 0, 255), 2)

    拟合椭圆
    ellipse = cv.fitEllipse(cnt)
    cv.ellipse(img_color2, ellipse, (0, 255, 0), 2)

    实验:计算下图中数字1的面积、轮廓周长,绘制数字1的外接矩形、最小外接矩形、最小外接圆、拟合椭圆

    实验用图

    import cv2 as cv
    import numpy as np
    
    # 载入手写数字图片
    img = cv.imread('handwriting.jpg', 0)
    # 将图像二值化
    _, thresh = cv.threshold(img, 0, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU)
    contours, hierarchy = cv.findContours(thresh, 3, 2)
    
    # 创建出两幅彩色图用于绘制
    img_color1 = cv.cvtColor(img, cv.COLOR_GRAY2BGR)
    img_color2 = np.copy(img_color1)
    # 创建一幅彩色图像用作结果对比
    img_color = np.copy(img_color1)
    
    # 计算数字1的轮廓特征
    cnt = contours[1]
    cv.drawContours(img_color1, [cnt], 0, (0, 0, 255), 2)
    
    # 1.轮廓面积
    area = cv.contourArea(cnt)  # 6289.5
    print(area)
    
    # 2.轮廓周长
    perimeter = cv.arcLength(cnt, True)  # 527.4041
    print(perimeter)
    
    # 3.图像矩
    M = cv.moments(cnt)
    print(M)
    print(M['m00'])  # 轮廓面积
    cx, cy = M['m10'] / M['m00'], M['m01'] / M['m00']  # 轮廓质心
    print(cx, cy)
    
    # 4.图像外接矩形和最小外接矩形
    x, y, w, h = cv.boundingRect(cnt)  # 外接矩形
    cv.rectangle(img_color1, (x, y), (x + w, y + h), (0, 255, 0), 2)
    
    rect = cv.minAreaRect(cnt)  # 最小外接矩形
    box = np.int0(cv.boxPoints(rect))  # 矩形的四个角点并取整
    cv.drawContours(img_color1, [box], 0, (255, 0, 0), 2)
    
    # 5.最小外接圆
    (x, y), radius = cv.minEnclosingCircle(cnt)
    (x, y, radius) = map(int, (x, y, radius))  # 这也是取整的一种方式噢
    cv.circle(img_color2, (x, y), radius, (0, 0, 255), 2)
    
    # 6.拟合椭圆
    ellipse = cv.fitEllipse(cnt)
    cv.ellipse(img_color2, ellipse, (0, 255, 0), 2)
    
    result = np.hstack((img_color,img_color1,img_color2))
    cv.imshow('result',result)
    cv.waitKey(0)
    cv.destroyAllWindows()
    

    实验结果

    程序控制台输出
    程序图像绘制结果

  • 相关阅读:
    java类型比较_Java数据类型的比较
    学习方法-1:海绵学习法
    性能测试:TPS和QPS的区别
    代码反思
    网站TLS升级 1.0&1.1--1.2
    Mysql常用语法
    初级测试工程师面试指南
    postman实战之断言
    postman预处理脚本实战
    什么是HTTP超文本协议
  • 原文地址:https://www.cnblogs.com/wojianxin/p/12605121.html
Copyright © 2011-2022 走看看