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中,图像轮廓检测是图像处理和分析中的一个重要步骤。下面介绍一些常用的图像轮廓检测和处理函数及其使用示例。

图像轮廓
findContours drawContours arcLength contourArea
检测图像中的轮廓 绘制图像中的轮廓 计算轮廓的周长 计算轮廓的面积
minAreaRect boundingRect minEnclosingCircle minEnclosingTriangle
找到面积最小的旋转矩形 计算包围轮廓的矩形 计算包围轮廓的最小圆 计算包围轮廓的最小三角形
approxPolyDP convexHull convexityDefects fitLine
使用多边形逼近曲线或轮廓 计算轮廓的凸包 计算轮廓的凸缺陷 用一条直线拟合轮廓
fitEllipse fitEllipseAMS fitEllipseDirect fillPoly
用一个椭圆拟合轮廓 用AMS方法拟合椭圆 用直接方法拟合椭圆 填充多边形
fillConvexPoly pointPolygonTest isContourConvex intersectConvexConvex
填充凸多边形 测试点与轮廓的相对位置 测试轮廓是否是凸的 计算两个凸多边形的交集
检测并绘制轮廓 (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。下面介绍这些函数及其使用示例。

图像大小调整函数
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中的各种函数来调整图像的大小和形状。根据具体的应用需求,可以灵活运用这些函数来实现复杂的图像处理任务。

相关推荐

  1. opencv函数学习_“干货“_6

    2024-07-18 17:18:01       19 阅读
  2. opencv函数学习_“干货“_总

    2024-07-18 17:18:01       22 阅读
  3. opencv函数学习_“干货“_9

    2024-07-18 17:18:01       32 阅读
  4. opencv函数学习_“干货“_10

    2024-07-18 17:18:01       32 阅读
  5. opencv函数学习_“干货“_7

    2024-07-18 17:18:01       23 阅读
  6. opencv函数学习_“干货“_8

    2024-07-18 17:18:01       25 阅读
  7. opencv函数学习_“干货“_11

    2024-07-18 17:18:01       24 阅读
  8. opencv函数学习_“干货“_5

    2024-07-18 17:18:01       25 阅读
  9. opencv函数学习_“干货“_4

    2024-07-18 17:18:01       21 阅读
  10. opencv函数学习_“干货“_3

    2024-07-18 17:18:01       16 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-18 17:18:01       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-18 17:18:01       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-18 17:18:01       58 阅读
  4. Python语言-面向对象

    2024-07-18 17:18:01       69 阅读

热门阅读

  1. web前端 Vue 框架面试120题(四)

    2024-07-18 17:18:01       18 阅读
  2. 富格林:可信办法阻挠虚假受骗

    2024-07-18 17:18:01       19 阅读
  3. ClickHouse中使用UNION

    2024-07-18 17:18:01       20 阅读
  4. vue3项目中pinia的用法详解(值得收藏)

    2024-07-18 17:18:01       20 阅读
  5. jd-gui反编译出现中文乱码问题

    2024-07-18 17:18:01       18 阅读
  6. CL11命令行解析使用实例

    2024-07-18 17:18:01       19 阅读
  7. PCB的层叠结构

    2024-07-18 17:18:01       18 阅读
  8. vim+cscope+ctags

    2024-07-18 17:18:01       23 阅读
  9. gitlab reset passwd

    2024-07-18 17:18:01       20 阅读
  10. 02-Redis未授权访问漏洞

    2024-07-18 17:18:01       21 阅读
  11. 开发一个商城app需要多少钱

    2024-07-18 17:18:01       21 阅读
  12. 【STM32】超声波一般常用哪两个引脚?

    2024-07-18 17:18:01       18 阅读
  13. Linux 之 ln 硬链接和软链接

    2024-07-18 17:18:01       20 阅读