OpenCV-可视化用cv2.approxPolyDP()提取的多边形曲线 [英] OpenCV - visualize polygonal curve(s) extracted with cv2.approxPolyDP()

查看:2034
本文介绍了OpenCV-可视化用cv2.approxPolyDP()提取的多边形曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想可视化用cv2.approxPolyDP()提取的多边形曲线.这是我正在使用的图像:

I want to visualize polygonal curve(s) extracted with cv2.approxPolyDP(). Here's the image I am using:

我的代码尝试隔离主岛,并定义和绘制轮廓近似值和轮廓船体.我用绿色绘制了轮廓,用红色绘制了近似值:

My code attempts to isolate the main island and define and plot the contour approximation and contour hull. I have plotted the contour found in green, the approximation in red:

import numpy as np
import cv2

# load image and shrink - it's massive
img = cv2.imread('../data/UK.png')
img = cv2.resize(img, None,fx=0.25, fy=0.25, interpolation = cv2.INTER_CUBIC)

# get a blank canvas for drawing contour on and convert img to grayscale
canvas = np.zeros(img.shape, np.uint8)
img2gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# filter out small lines between counties
kernel = np.ones((5,5),np.float32)/25
img2gray = cv2.filter2D(img2gray,-1,kernel)

# threshold the image and extract contours
ret,thresh = cv2.threshold(img2gray,250,255,cv2.THRESH_BINARY_INV)
im2,contours,hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)


# find the main island (biggest area)
cnt = contours[0]
max_area = cv2.contourArea(cnt)

for cont in contours:
    if cv2.contourArea(cont) > max_area:
        cnt = cont
        max_area = cv2.contourArea(cont)

# define main island contour approx. and hull
perimeter = cv2.arcLength(cnt,True)
epsilon = 0.01*cv2.arcLength(cnt,True)
approx = cv2.approxPolyDP(cnt,epsilon,True)

hull = cv2.convexHull(cnt)

# cv2.isContourConvex(cnt)

cv2.drawContours(canvas, cnt, -1, (0, 255, 0), 3)
cv2.drawContours(canvas, approx, -1, (0, 0, 255), 3)
## cv2.drawContours(canvas, hull, -1, (0, 0, 255), 3) # only displays a few points as well.

cv2.imshow("Contour", canvas)
k = cv2.waitKey(0)

if k == 27:         # wait for ESC key to exit
    cv2.destroyAllWindows()

以下是生成的图像:

第一个图像以绿色绘制轮廓.第二个用红色绘制了近似值-如何将这个近似值绘制为连续的闭合曲线?

The first image plots the contour in green. The second plots the approximation in red - how do I plot this approximation as a continuous closed curve?

文档不是非常清晰,教程,但我的理解是cv2.approxPolyDP()应该定义一条连续的闭合曲线,我应该可以使用cv2.drawContours()进行绘制.那是对的吗?如果是这样,我在做什么错了?

The documentation isn't terribly clear and neither is the tutorial, but my understanding is that cv2.approxPolyDP() should define a continuous, closed curve, which I should be able to plot with cv2.drawContours(). Is that correct? If so, what am I doing wrong?

推荐答案

问题仅在于可视化:drawContours期望轮廓的数组(在python中为列表),而不仅仅是一个numpy数组(从<返回c4>).

The problem is in visualization only: drawContours expects array (list in case of python) of contours, not just one numpy array (which is returned from approxPolyDP).

解决方法如下:更换

cv2.drawContours(canvas, approx, -1, (0, 0, 255), 3)

cv2.drawContours(canvas, [approx], -1, (0, 0, 255), 3)

这篇关于OpenCV-可视化用cv2.approxPolyDP()提取的多边形曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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