如何在python中获取平面图的外部轮廓? [英] How to get the external contour of a floorplan in python?

查看:280
本文介绍了如何在python中获取平面图的外部轮廓?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What is the best way to get a floorplan external contour?

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

Snakes algorithm doesn't work well because some floorplans are too convex.

推荐答案

您只需要在查找轮廓时调整grayScale图像的阈值以包括灰色虚线路径,因为输入图像的主要部分是白色,因此我们可以选择接近255(例如230)的阈值.然后找到轮廓阈值.

You just need to adjust the threshold of the grayScale image to include the gray dotted lines path while finding the contours, As the major part of input image is white so we can choose the threshold close to 255, say 230. And then find the contours thresholding.

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

You may use cv2.approxPolyDP to calculate the approximate polynomial shape, but it won't help much, so that step is optional.

代码段可能如下所示:

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])

这篇关于如何在python中获取平面图的外部轮廓?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆