opencv计算植物叶面积_叶直径_叶周长_python_opencv 根据面积计算直径(2)

文章详细介绍了如何使用Python的OpenCV库进行图像处理,包括二值化、轮廓检测、轮廓排序、周长计算以及透视变换,旨在展示如何通过编程实现对图像内容的分析和操作。
摘要由CSDN通过智能技术生成

img1为带黑边的原始图像,warp_imgs为图像矫正透视变换后的去黑边图像,其余py文件分享在下面,计算周长的代码自行去获取轮廓周长套公式(1)进行计算:

get_area_final.py:

import cv2
import numpy as np

def sort_contours_size(cnts):
    """根据大小对轮廓进行排序"""
    cnts_sizes = []
    for contour in cnts:
        cnt_size = cv2.contourArea(contour)
        cnts_sizes.append([cnt_size,contour])
    cnts_sizes.sort(key=lambda x:x[0], reverse=True)
    return cnts_sizes

def get_area_main(path):
    img = cv2.imread(path)
    #变成单通道的黑白图片
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    #二值化 返回阈值和二值化后图
    thresh,binary = cv2.threshold(gray,150,255,cv2.THRESH_BINARY)
    # cv2.imshow("img1",binary)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()
    # 查找轮廓 返回轮廓和层级
    contours, hierarchy = cv2.findContours(binary.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    # 绘制轮廓会直接修改原图
    img_copy = img.copy()
    cv2.drawContours(img_copy, contours, -1, (0, 0, 255), 2)
    # cv2.imshow('img_con', img_copy)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()
    contour_sizes = sort_contours_size(contours)
    cnt = contour_sizes[1][1]
    (x, y), radius = cv2.minEnclosingCircle(cnt)
    center = (int(x), int(y))
    radius = int(radius)
    # print(center,radius)
    cv2.circle(img_copy, center, radius, (255, 0, 0), 2)
    
  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值