目录
检测并绘制轮廓 (findContours 和 drawContours)
计算轮廓的周长和面积 (arcLength 和 contourArea)
计算包围轮廓的最小矩形和最小圆 (minAreaRect 和 minEnclosingCircle)
测试点与轮廓的相对位置 (pointPolygonTest)
http://t.csdnimg.cn/i8pqt —— opencv—常用函数学习_“干货“_总(VIP)
散的正在一部分一部分发,不需要VIP。
资料整理不易,有用话给个赞和收藏吧。
十七、图像轮廓
在OpenCV中,图像轮廓检测是图像处理和分析中的一个重要步骤。下面介绍一些常用的图像轮廓检测和处理函数及其使用示例。
图像轮廓 | |||
findContours | drawContours | arcLength | contourArea |
检测图像中的轮廓 | 绘制图像中的轮廓 | 计算轮廓的周长 | 计算轮廓的面积 |
minAreaRect | boundingRect | minEnclosingCircle | minEnclosingTriangle |
找到面积最小的旋转矩形 | 计算包围轮廓的矩形 | 计算包围轮廓的最小圆 | 计算包围轮廓的最小三角形 |
approxPolyDP | convexHull | convexityDefects | fitLine |
使用多边形逼近曲线或轮廓 | 计算轮廓的凸包 | 计算轮廓的凸缺陷 | 用一条直线拟合轮廓 |
fitEllipse | fitEllipseAMS | fitEllipseDirect | fillPoly |
用一个椭圆拟合轮廓 | 用AMS方法拟合椭圆 | 用直接方法拟合椭圆 | 填充多边形 |
fillConvexPoly | pointPolygonTest | isContourConvex | intersectConvexConvex |
填充凸多边形 | 测试点与轮廓的相对位置 | 测试轮廓是否是凸的 | 计算两个凸多边形的交集 |
检测并绘制轮廓 (findContours
和 drawContours
)
import cv2
import numpy as np
# 读取图像并转换为灰度图
image = cv2.imread('path_to_image.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 应用阈值处理
_, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 检测轮廓
contours, _ = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# 绘制轮廓
cv2.drawContours(image, contours, -1, (0, 255, 0), 2)
cv2.imshow('Contours', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
计算轮廓的周长和面积 (arcLength
和 contourArea
)
for contour in contours:
perimeter = cv2.arcLength(contour, True)
area = cv2.contourArea(contour)
print(f"Perimeter: {perimeter}, Area: {area}")
计算包围轮廓的最小矩形和最小圆 (minAreaRect
和 minEnclosingCircle
)
for contour in contours:
# 最小旋转矩形
rect = cv2.minAreaRect(contour)
box = cv2.boxPoints(rect)
box = np.int0(box)
cv2.drawContours(image, [box], 0, (0, 0, 255), 2)
# 最小包围圆
(x, y), radius = cv2.minEnclosingCircle(contour)
center = (int(x), int(y))
radius = int(radius)
cv2.circle(image, center, radius, (255, 0, 0), 2)
cv2.imshow('Min Area Rect and Enclosing Circle', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
多边形逼近 (approxPolyDP
)
for contour in contours:
epsilon = 0.02 * cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, epsilon, True)
cv2.drawContours(image, [approx], -1, (0, 255, 255), 2)
cv2.imshow('Approx Poly DP', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
计算轮廓的凸包 (convexHull
)
for contour in contours:
hull = cv2.convexHull(contour)
cv2.drawContours(image, [hull], -1, (255, 255, 0), 2)
cv2.imshow('Convex Hull', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
用椭圆拟合轮廓 (fitEllipse
)
for contour in contours:
if len(contour) >= 5: # 拟合椭圆要求至少有5个点
ellipse = cv2.fitEllipse(contour)
cv2.ellipse(image, ellipse, (0, 255, 255), 2)
cv2.imshow('Fit Ellipse', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
测试点与轮廓的相对位置 (pointPolygonTest
)
point = (50, 50)
for contour in contours:
dist = cv2.pointPolygonTest(contour, point, True)
print(f"Distance from point {point} to contour: {dist}")
这些示例展示了如何使用OpenCV中的各种轮廓检测和处理函数来处理图像。根据具体的应用需求,可以灵活运用这些函数来实现复杂的图像轮廓检测和分析任务。
十八、图像大小
在OpenCV中,调整图像大小是一个常见的操作,主要通过以下几个函数实现:pyrDown
、pyrUp
、resize
、reduce
和 reshape
。下面介绍这些函数及其使用示例。
图像大小调整函数 | ||||
pyrDown | pyrUp | resize | reduce | reshape |
降低图像分辨率(降采样) | 提高图像分辨率(升采样) | 调整图像大小 | 沿某个轴缩小图像 | 改变图像的形状 |
降采样 (pyrDown
)
import cv2
import numpy as np
# 读取图像
image = cv2.imread('path_to_image.jpg')
# 降采样
pyr_down_image = cv2.pyrDown(image)
cv2.imshow('PyrDown Image', pyr_down_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
升采样 (pyrUp
)
# 升采样
pyr_up_image = cv2.pyrUp(image)
cv2.imshow('PyrUp Image', pyr_up_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
调整图像大小 (resize
)
# 调整图像大小
resized_image = cv2.resize(image, (300, 300)) # 指定新的宽和高
cv2.imshow('Resized Image', resized_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 按比例缩放
resized_image_aspect = cv2.resize(image, None, fx=0.5, fy=0.5) # 宽和高分别缩小一半
cv2.imshow('Resized Image with Aspect Ratio', resized_image_aspect)
cv2.waitKey(0)
cv2.destroyAllWindows()
沿某个轴缩小图像 (reduce
)
# 沿列方向缩小图像(求和)
reduced_image = cv2.reduce(image, dim=1, rtype=cv2.REDUCE_SUM)
cv2.imshow('Reduced Image', reduced_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
改变图像的形状 (reshape
)
# 改变图像的形状
reshaped_image = image.reshape((-1, 3)) # 改为一行三通道的图像
print("Reshaped Image Shape:", reshaped_image.shape)
这些示例展示了如何使用OpenCV中的各种函数来调整图像的大小和形状。根据具体的应用需求,可以灵活运用这些函数来实现复杂的图像处理任务。