opencv - 如何在 python 中获取平面图的外部轮廓?

标签 opencv image-processing scikit-image

获取平面图外部轮廓的最佳方法是什么?

enter image description here

Snakes 算法效果不佳,因为某些平面图过于凸出。

最佳答案

你只需要调整灰度图像的阈值以包括灰色虚线路径,同时找到轮廓,由于输入图像的主要部分是白色所以我们可以选择接近255的阈值,比如230。然后找到轮廓阈值。

您可以使用 cv2.approxPolyDP 来计算多项式的近似形状,但这不会有太大帮助,因此该步骤是可选的。

代码片段可能如下所示:

import cv2

img = cv2.imread("/Users/anmoluppal/Downloads/1tl6D.jpg")

img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

ret, thresh = cv2.threshold(img_gray, 230, 255, cv2.THRESH_BINARY_INV)

img_, contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

largest_contour_area = 0
for cnt in contours:
    if (cv2.contourArea(cnt) > largest_contour_area):
        largest_contour_area = cv2.contourArea(cnt)
        largest_contour = cnt

epsilon = 0.001*cv2.arcLength(largest_contour,True)
approx = cv2.approxPolyDP(largest_contour,epsilon,True)

final = cv2.drawContours(img, [approx], 0, [0, 255, 0])

enter image description here

关于opencv - 如何在 python 中获取平面图的外部轮廓?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38366439/

相关文章:

python - Raspberry Pi 4 的相机与 OpenCV 集成

macos - 在 mac el capitan 上将公式降级到以前的版本 (opencv3/3.0.0)

opencv - 长度比的计算机视觉

javascript - 在 map 图像上检测到 "concave hull"

python - opencv:从图像创建二进制掩码

c++ - 使 OpenCV 调整大小与 Matlab/Octave imresize 相同

python - 使用 OpenCV 获取帧时如何测试相机的实际分辨率?

image-processing - 如何自动确定机器学习的特征?

python - Skimage合并过度分割的区域

python - Pyplot 彩色图显示不同的结果