目录
opencv python版 拟合多边形
用例:
std::vector<std::vector<cv::Point>> contours;
std::vector<cv::Vec4i> hierachy;
cv::findContours(binary, contours, hierachy, cv::RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point(-1,-1));
std::vector<std::vector<cv::Point>> contours_ploy(contours.size());
for(int i=0; i<contours.size(); i++)
{
approxPolyDP(contours[i], contours_ploy[i], contours[i].size() / 4, true);
}
参考:opencv 多边形拟合
一、逼近多边形
逼近多边形,是通过对轮廓外形无限逼近,删除非关键点、得到轮廓的关键点,不断逼近轮廓真实形状的方法,OpenCV中多边形逼近的函数与参数解释如下:
approxCourve= cv2.approxPolyDP(curve,epsilon,closed)
1
参数解析:
curve:轮廓点的集合。
epsilon:指定近似精度的参数, 这是原始曲线和它的近似之间最大距离。
closed:如果为true,则闭合近似曲线(其第一个和最后一个顶点为连接的);否则,不闭合。二、参考代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#author:Kong DeXing
#案例:Fu Xianjun. All Rights Reserved.
import cv2
import numpy as np
img = cv2.imread('hand.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,binary = cv2.threshold(gray,60,255,0)#阈值处理
contours,hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)#查找轮廓
print(len(contours))
x = 0
for i in range(len(contours)):
area = cv2.contourArea(contours[i])
if area>10000:
print(area)
x = i
cnt = contours[x]
img1 = img.copy()
approx1 = cv2.approxPolyDP(cnt,3,True)#拟合精确度
img1 =cv2.polylines(img1,[approx1],True,(255,255,0),2)
cv2.imshow('approxPolyDP1',img1)
img2 = img.copy()
approx2 = cv2.approxPolyDP(cnt,5,True)#拟合精确度
img2 =cv2.polylines(img2,[approx2],True,(255,255,0),2)
cv2.imshow('approxPolyDP2',img2)
img3 = img.copy()
approx3 = cv2.approxPolyDP(cnt,7,True)#拟合精确度
img3 =cv2.polylines(img3,[approx3],True,(255,255,0),2)
cv2.imshow('approxPolyDP3',img3)
cv2.imwrite("dst.png",img1)
print(len(approx1))
cv2.waitKey(0)
cv2.destroyAllWindows()
可以看到,cv.approxPolyDP 函数 参数2(epsilon)越小,得到的多边形角点越多,对原图像的多边形近似效果越好。
原文链接:https://blog.csdn.net/juluwangriyue/article/details/117957920
python版拟合多边形(指定边数)
拟合多边形示例
import cv2
import numpy as np
def myApprox(con,side_size):# con为预先得到的最大轮廓
num = 0.001
# 初始化时不需要太小,因为四边形所需的值并不很小
ep = num * cv2.arcLength(con, True)
con = cv2.approxPolyDP(con, ep, True)
while (1):
if len(con) <= side_size:#防止程序崩溃设置的<=4
break
else:
num = num * 1.5
ep = num * cv2.arcLength(con, True)
con = cv2.approxPolyDP(con, ep, True)
continue
return con
img=np.zeros((200,200),dtype=np.uint8)
img[50:150,50:150]=255
ret,thresh=cv2.threshold(img,127,255,0)#threshold阈值
contours,hierarchy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
color=cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
contour=np.array([[10,10],[50,10],[100,100],[80,200],[40,100],[10,50],[5,30]])
contour= myApprox(contour,side_size=3)
print(contour)
图片轮廓,多边形拟合
import cv2
import numpy as np
def myApprox(con,side_size):# con为预先得到的最大轮廓
num = 0.001
# 初始化时不需要太小,因为四边形所需的值并不很小
ep = num * cv2.arcLength(con, True)
con = cv2.approxPolyDP(con, ep, True)
while (1):
if len(con) <= side_size:#防止程序崩溃设置的<=4
break
else:
num = num * 1.5
ep = num * cv2.arcLength(con, True)
con = cv2.approxPolyDP(con, ep, True)
continue
return con
img=np.zeros((200,200),dtype=np.uint8)
img[50:150,50:150]=255
ret,thresh=cv2.threshold(img,127,255,0)#threshold阈值
contours,hierarchy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
color=cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
contours_new=[]
for contour in contours:
contour= myApprox(contour,side_size=3)
contours_new.append(contour)
img=cv2.drawContours(color,contours_new,-1,(0,255,0),2)
cv2.imshow('contours',color)
cv2.waitKey()
cv2.destroyAllWindows()
原文链接:https://blog.csdn.net/only_ctrl/article/details/115642155
opencv c++版(指定边数):
opencv多边形拟合(指定边数)_qq_33415794的博客-CSDN博客_opencv 四边形拟合
opencv的多边形拟合函数cv::approxPolyDP(),不能指定想要拟合的多边形的边数,然后往往很多时候,我们是已知多边形的边数的,这里在approxPolyDP的基础之上通过二分法去寻找满足要拟合的多边形的epsilon,然后得到要拟合的多边形。
//利用二分法逼近epsilon的四边形(多边形)拟合
bool myapproxPolyDP(std::vector<cv::Point> contours, std::vector<cv::Point>& rects,double minepsilon = 1, double maxepsilon = 20,int sides=4);
myapproxPolyDP(std::vector<cv::Point> contours, std::vector<cv::Point>& rects, double minepsilon, double maxepsilon, int sides)
{
std::vector<cv::Point> rect1;
std::vector<cv::Point> rect2;
cv::approxPolyDP(contours, rect1, minepsilon, true);
cv::approxPolyDP(contours, rect2, maxepsilon, true);
if (rect1.size() > sides && rect2.size() > sides)
{
rects = contours;
return false;
}
if (rect1.size() < sides && rect2.size() < sides)
{
rects = contours;
return false;
}
else
{
if (rect1.size() == sides)
{
rects.resize(sides);
for (int i = 0; i < sides; i++)
{
rects[i] = rect1[i];
}
return true;
}
else if (rect2.size() == sides)
{
rects.resize(sides);
for (int i = 0; i < sides; i++)
{
rects[i] = rect2[i];
}
return true;
}
else
{
double midepsilon = (minepsilon + maxepsilon) / 2.0;
std::vector<cv::Point> rect3;
cv::approxPolyDP(contours, rect3, midepsilon, true);
if (rect3.size() == sides)
{
rects.resize(sides);
for (int i = 0; i < sides; i++)
{
rects[i] = rect3[i];
}
return true;
}
else if (rect3.size() < sides)
{
return myapproxPolyDP(contours, rects, minepsilon, midepsilon);
}
else
{
return myapproxPolyDP(contours, rects, midepsilon, minepsilon);
}
}
}
return true;
}
c++测试代码,
参数sides需要小于点的个数。
int main(int argc, char** argv) {
std::vector<cv::Point> contours;
cv::Point bb(0.5f, 0.6f);
cv::Point aa(0.3f, 0.f);
contours.push_back(cv::Point2f(0, 0));
contours.push_back(cv::Point2f(50, 10));
contours.push_back(cv::Point2f(100, 100));
contours.push_back(cv::Point2f(80, 200));
contours.push_back(cv::Point2f(40, 150));
contours.push_back(cv::Point2f(10, 50));
contours.push_back(cv::Point2f(0, 30));
std::vector<cv::Point> rects;
int sides = 5;
bool res;
res=myapproxPolyDP(contours, rects, 1,20, sides);
if (res) {
printf("myapproxPolyDP ok ");
}
else {
printf("myapproxPolyDP failed ");
}
return 0;
}