python opencv入门 轮廓的性质(19)
来源:互联网 发布:手机我的世界js编辑器 编辑:程序博客网 时间:2024/05/29 05:10
内容来自OpenCV-Python Tutorials 自己翻译整理
长宽比:
边界矩形的宽高比
import cv2import numpy as npimg = cv2.imread('3.jpg',0)#ret,thresh = cv2.threshold(img,127,255,0)img,contours,hierarchy = cv2.findContours(img, 1, 2)cnt = contours[1]x,y,w,h = cv2.boundingRect(cnt)#获取边界aspect_ratio = float(w)/h#计算比率print(aspect_ratio)
面积比
轮廓面积与边界矩形的面积比
import cv2import numpy as npimg = cv2.imread('3.jpg',0)#ret,thresh = cv2.threshold(img,127,255,0)img,contours,hierarchy = cv2.findContours(img, 1, 2)cnt = contours[1]area = cv2.contourArea(cnt)#获取轮廓面积x,y,w,h = cv2.boundingRect(cnt)#获取边界rect_area = w*hextent = float(area)/rect_areaprint(extent)
凸包面积比
轮廓面积与凸包面积的比
import cv2import numpy as npimg = cv2.imread('3.jpg',0)#ret,thresh = cv2.threshold(img,127,255,0)img,contours,hierarchy = cv2.findContours(img, 1, 2)cnt = contours[1]area = cv2.contourArea(cnt)hull = cv2.convexHull(cnt)hull_area = cv2.contourArea(hull)solidity = float(area)/hull_areaprint(solidity)
轮廓直径
与轮廓面积相等的圆形的直径
import cv2import numpy as npimg = cv2.imread('3.jpg',0)#ret,thresh = cv2.threshold(img,127,255,0)img,contours,hierarchy = cv2.findContours(img, 1, 2)cnt = contours[1]area = cv2.contourArea(cnt)equi_diameter = np.sqrt(4*area/np.pi)print(equi_diameter)
长短轴方向
对象的方向,下面的方法还会返回长轴和短轴的长度
(x,y),(MA,ma),angle = cv2.fitEllipse(cnt)
蒙板与像素
有时我们需要目标对象在图像里所有像素信息
(这里没使用灰度图)
import cv2import numpy as npimg = cv2.imread('3.jpg',0)#ret,thresh = cv2.threshold(img,127,255,0)img,contours,hierarchy = cv2.findContours(img, 1, 2)cnt = contours[1]mask = np.zeros(img.shape,np.uint8)cv2.drawContours(mask,[cnt],0,255,-1)pixelpoints = np.transpose(np.nonzero(mask))#像素
此处使用了两种方法,分别为numpy的函数和使用opencv的函数。结果相同,但是numpy给出的是行列值,而opencv给出的是(x,y)形式
最大值和最小值及位置
要用到mask 像素蒙板
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(img,mask = mask)
平均颜色和平均灰度
可以使用相同的蒙板求一个对象的平均颜色或平均灰度
import cv2import numpy as npimg = cv2.imread('3.jpg',0)#ret,thresh = cv2.threshold(img,127,255,0)img,contours,hierarchy = cv2.findContours(img, 1, 2)cnt = contours[1]mask = np.zeros(img.shape,np.uint8)mean_val = cv2.mean(img,mask = mask)print(mean_val)
极值点
就是求图像中某个对象的最高、最低、最左、最右的点
import cv2import numpy as npimg = cv2.imread('3.jpg',0)#ret,thresh = cv2.threshold(img,127,255,0)img,contours,hierarchy = cv2.findContours(img, 1, 2)cnt = contours[1]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])print(leftmost)
阅读全文
0 0
- python opencv入门 轮廓的性质(19)
- Opencv(Python) 教程-轮廓(3)轮廓的性质
- python opencv入门 更多关于轮廓的函数(20)
- python opencv入门 轮廓的层次结构(21)
- python opencv入门 轮廓(17)
- python opencv入门 轮廓特征(18)
- Opencv(Python) 教程-轮廓(2)轮廓特征求取
- OpenCV入门(二十六)-- 轮廓检测
- OpenCV入门笔记(五) 轮廓检测
- OpenCV-Python教程(11、轮廓检测)
- OpenCV-Python教程(11、轮廓检测)
- OpenCV-Python教程(11、轮廓检测)
- python-opencv-轮廓检测
- opencv-python(15):轮廓
- Python-OpenCV轮廓检测
- Python OpenCV -- 轮廓检测 (十四)
- Opencv-python接口:轮廓检测
- OpenCV 删除轮廓的方法(一)
- Centos7(Linux) 系统下的 各种环境部署 之 JDK
- maven--私服的搭建(Nexus的使用)
- 使用IDEA在Spring Boot上集成JSP
- OpenCV 相关函数记录汇总(持续更新)
- Java-date的拼接&Calendar的计算&Timestamp时间戳-表示时间的方法
- python opencv入门 轮廓的性质(19)
- XSS漏洞与SQL注入漏洞解决方案
- 小米推送点击无效的原因
- cordova build安卓apk,关于版本号的修改(可能有误,看看就好,别当真)
- 正则表达式运算符优先级
- 线段树
- Android内存泄露——全解析和处理办法
- Android动态设置margin和dp转为int
- OpenCV学习之采用金字塔方法进行图像分割