opencv—常用函数学习_“干货“_6

目录

十七、图像轮廓

检测并绘制轮廓 (findContours 和 drawContours)

计算轮廓的周长和面积 (arcLength 和 contourArea)

计算包围轮廓的最小矩形和最小圆 (minAreaRect 和 minEnclosingCircle)

多边形逼近 (approxPolyDP)

计算轮廓的凸包 (convexHull)

用椭圆拟合轮廓 (fitEllipse)

测试点与轮廓的相对位置 (pointPolygonTest)

十八、图像大小

降采样 (pyrDown)

升采样 (pyrUp)

调整图像大小 (resize)

沿某个轴缩小图像 (reduce)

改变图像的形状 (reshape)

http://t.csdnimg.cn/i8pqt —— opencv—常用函数学习_“干货“_总(VIP)

散的正在一部分一部分发,不需要VIP。

资料整理不易,有用话给个赞和收藏吧。


十七、图像轮廓

        在OpenCV中,图像轮廓检测是图像处理和分析中的一个重要步骤。下面介绍一些常用的图像轮廓检测和处理函数及其使用示例。

图像轮廓
findContoursdrawContoursarcLengthcontourArea
检测图像中的轮廓绘制图像中的轮廓计算轮廓的周长计算轮廓的面积
minAreaRectboundingRectminEnclosingCircleminEnclosingTriangle
找到面积最小的旋转矩形计算包围轮廓的矩形计算包围轮廓的最小圆计算包围轮廓的最小三角形
approxPolyDPconvexHullconvexityDefectsfitLine
使用多边形逼近曲线或轮廓计算轮廓的凸包计算轮廓的凸缺陷用一条直线拟合轮廓
fitEllipsefitEllipseAMSfitEllipseDirectfillPoly
用一个椭圆拟合轮廓用AMS方法拟合椭圆用直接方法拟合椭圆填充多边形
fillConvexPolypointPolygonTestisContourConvexintersectConvexConvex
填充凸多边形测试点与轮廓的相对位置测试轮廓是否是凸的计算两个凸多边形的交集
检测并绘制轮廓 (findContoursdrawContours)
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()
计算轮廓的周长和面积 (arcLengthcontourArea)
for contour in contours:
    perimeter = cv2.arcLength(contour, True)
    area = cv2.contourArea(contour)
    print(f"Perimeter: {perimeter}, Area: {area}")
计算包围轮廓的最小矩形和最小圆 (minAreaRectminEnclosingCircle)
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中,调整图像大小是一个常见的操作,主要通过以下几个函数实现:pyrDownpyrUpresizereducereshape。下面介绍这些函数及其使用示例。

图像大小调整函数
pyrDownpyrUpresizereducereshape
降低图像分辨率(降采样)提高图像分辨率(升采样)调整图像大小沿某个轴缩小图像改变图像的形状
降采样 (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中的各种函数来调整图像的大小和形状。根据具体的应用需求,可以灵活运用这些函数来实现复杂的图像处理任务。

  • 12
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值