原始圖像為
import cv2
import numpy as np
import matplotlib.pyplot as plt
def cv_show(name, img):
cv2.imshow(name, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 讀取圖像
img = cv2.imread('contours.png')
cv_show('contours', img)
# 灰度化和二值化
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)[1]
cv_show('thresh', thresh)
# 尋找輪廓
cnt = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[1]
經過輪廓檢測,輸出的Value值為cnt,類型為list,元素個數為11,即包含11個輪廓
?c0為在cnt中索引為0,代表第一輪廓,即三角形輪廓
c0 = cnt[0]
res = cv2.drawContours(draw, [c0], -1, (0, 0, 255), 2)
cv_show('res', res)
?文章來源地址http://www.zghlxwxcb.cn/news/detail-617918.html
?下面進行輪廓近似
epsilon = 0.1 * cv2.arcLength(c0, True)
approx = cv2.approxPolyDP(c0, epsilon, True)
draw_img = img.copy()
res = cv2.drawContours(draw_img, [approx], -1, (0, 0, 255), 2)
cv_show('res', res)
cv2.apaproxPloyDP函數輸出的approx,元素個數為3,即三角形三個頂點
?文章來源:http://www.zghlxwxcb.cn/news/detail-617918.html
?
到了這里,關于cv2.approxPolyDP()函數的輸出,為近似多邊形的頂點坐標的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!