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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2024年04月14日 01时45分04秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
【超越白皮书7】你需要知道关于ETH2.0的几个事实
2019-04-30
对于时间复杂度的通俗理解
2019-04-30
如何输入多组数据并输出每组数据的和?
2019-04-30
行阶梯型矩阵
2019-04-30
MATLAB指定路径保存图片方法
2019-04-30
JAVA学习笔记6 - 数组
2019-04-30
JAVA学习笔记10 - 继承
2019-04-30
Android 开发学习笔记 00 - Getting Started
2019-04-30
【学习笔记】Android Activity
2019-04-30
Android使用Retrofit_01_OAuth2 + GitHub
2019-04-30
location区段
2019-04-30
nginx访问控制、基于用户认证、https配置
2019-04-30
linux内存的寻址方式
2019-04-30
how2heap-double free
2019-04-30
how2heap-fastbin_dup_consolidate
2019-04-30
fmt在bss段(neepusec_easy_format)
2019-04-30
tensorflow 数据格式
2019-04-30
tf keras SimpleRNN源码解析
2019-04-30
MyBatisPlus简单入门(SpringBoot)
2019-04-30