cv2小记——轮廓的性质
发布日期:2021-06-30 15:01:39 浏览次数:2 分类:技术文章

本文共 2031 字,大约阅读时间需要 6 分钟。

# coding: utf-8 # !/usr/bin/python"""@File       :   轮廓的性质.py@Author     :   jiaming@Modify Time:   2020/2/5 15:25    @Contact    :   https://blog.csdn.net/weixin_39541632@Version    :   1.0@Desciption :   None"""import osimport sysimport numpy as npimport cv2import pprintfrom matplotlib import pyplot as pltrawPath = os.path.abspath(__file__)currentFile = os.path.basename(sys.argv[0])dataPath = rawPath[:rawPath.find(currentFile)] + r'static\\'

边界矩形的宽高比

"""边界矩形的宽高比"""img = cv2.imread(dataPath + 'j.png', 0)ret, thresh = cv2.threshold(img, 127, 255, 0)contours, hierarchy = cv2.findContours(thresh, 1, 2)cnt = contours[0]x, y, w, h = cv2.boundingRect(cnt)aspect_ration = float(w) / h  # 1.375print('aspect_ration', aspect_ration)

轮廓面积与边界矩形面积的比

"""轮廓面积与边界矩形面积的比"""img = cv2.imread(dataPath + 'j.png', 0)ret, thresh = cv2.threshold(img, 127, 255, 0)contours, hierarchy = cv2.findContours(thresh, 1, 2)cnt = contours[0]x, y, w, h = cv2.boundingRect(cnt)area = cv2.contourArea(cnt)x, y, w, h = cv2.boundingRect(cnt)rect_area = w*hextent = float(area) / rect_areaprint('extent', extent)  # 0.5353535353535354

轮廓面积与凸包面积的比

"""轮廓面积与凸包面积的比area = cv2.contourArea(cnt)hull = cv2.convexHull(cnt)hull_area = cv2.contourArea(hull)solidity = float(area) / hull_area"""

与轮廓面积相等的圆形的直径

"""与轮廓面积相等的圆形的直径area = cv2.contourArea(cnt)equi_diameter = np.sqrt(4*area / np.pi)"""

方向

"""方向返回长轴和短轴的长度(x, y),(MA, ma),angle = cv2.fitEllipse(cnt)"""

掩模和像素点

"""掩模和像素点生成构成对象的所有像素点mask = np.zeros(imgray, shape, np.uint8)cv2.drawContours(mask, [cnt], 0, 255, -1)pixelpoints = np.transpose(np.nonzero(mask)) 或pixelpoints = cv2.findNonZero(mask)"""

最大值及它们的位置

"""最大值及它们的位置通过使用掩模图像得到这些参数min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(imgray, mask=mask)"""

平均颜色及平均灰度

"""平均颜色及平均灰度我们也可以使用相同的掩模求一个对象的平均颜色或平均灰度mean_val = cv2.mean(im, mask=mask)"""

极点

"""极点一个对象最上面,最下面,最左边,最右边的点。leftmost = tuple(cnt[cnt[:,:,0].argmin()][0]) rightmost = tuple(cnt[cnt[:,:,0].argmax()][0]) topmost = tuple(cnt[cnt[:,:,1].argmin()][0]) bottommost = tuple(cnt[cnt[:,:,1].argmax()][0]) """

转载地址:https://jiaming.blog.csdn.net/article/details/104184341 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:cv2小记——轮廓:其他函数
下一篇:Linux常用命令——搜索命令

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年04月14日 01时45分04秒