Python 什么';cv2.approxPolyDP检测点的s规则?

Python 什么';cv2.approxPolyDP检测点的s规则?,python,opencv,image-processing,Python,Opencv,Image Processing,我有以下两个输入图像: 第一: 第二: 现在我想检测正方形区域 这是我的代码: #!/usr/bin/env python # coding: utf-8 import numpy as np import cv2 import argparse import math import sys ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", required=True, help="path to input

我有以下两个输入图像:

第一:

第二:

现在我想检测正方形区域

这是我的代码:

#!/usr/bin/env python
# coding: utf-8

import numpy as np
import cv2
import argparse
import math
import sys

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="path to input image")
args = vars(ap.parse_args())

img = cv2.imread(args["image"])
# img = cv2.bitwise_not(img,img)
# gray = cv2.imread(args["image"],0)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

gray = cv2.GaussianBlur(gray, (5, 5), 0)
cv2.imshow('gray', gray)
cv2.waitKey(0)

gray = cv2.normalize(gray, gray, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)
cv2.imshow('normalized', gray)
cv2.waitKey(0)
# sys.exit()

ret, thresh = cv2.threshold(gray, 230, 255, cv2.THRESH_BINARY_INV)
cv2.imshow('thresh', thresh)
# cv2.imwrite('./wni230.png',thresh)
cv2.waitKey(0)

square_cnts = []

thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, np.ones((5, 5), np.uint8))
cv2.imshow('dilated', thresh)
cv2.waitKey(0)
# sys.exit()

tmpimage, contours, h = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnt0 = contours[0]

tmp_h = h[::-1]
for index, cnt in enumerate(contours[::-1]):
    print '!!!!!!!!!!! new round !!!!!!!!!!'
    print cv2.arcLength(cnt, True)
    # print cnt
    approx = cv2.approxPolyDP(cnt, 0.1*cv2.arcLength(cnt, True), True)
    print '**********approx*****'
    print approx
    print len(approx)
    print '**********approx end*****'

    if len(approx) == 4:
        print 'h[i][2]:%s' % tmp_h[0][index][2]
        print 'h[i][3]:%s' % tmp_h[0][index][3]
        if ret > 0.5:
            print "Parallelogram"
        elif 0.3 < ret < 0.5:
            print "Rectangle"
        elif 0 < ret < 0.3:
            print "Rhombus"
        else:
            print "square"
        print cv2.arcLength(cnt, True)
        print approx
        cv2.drawContours(img, [cnt], 0, (0, 0, 255), 2)
        cv2.imshow('tmpsquare', img)
        cv2.waitKey(0)
        if int(cv2.arcLength(cnt, True)) >= 96:

            x, y, w, h = cv2.boundingRect(approx)
            for j in approx:
                cv2.circle(img, (int(j[0][0]), int(j[0][1])), 1, (0, 255, 0), 2)
            cv2.imshow('final', img[y:y+h, x:x+w])
            cv2.waitKey(0)
            print 'target but long squere detected...'
            cv2.waitKey(0)

cv2.imshow('img', img)
cv2.imwrite('tmp.png', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
#/usr/bin/env python
#编码:utf-8
将numpy作为np导入
进口cv2
导入argparse
输入数学
导入系统
ap=argparse.ArgumentParser()
ap.add_参数(“-i”,“--image”,required=True,help=“输入图像的路径”)
args=vars(ap.parse_args())
img=cv2.imread(args[“image”])
#img=cv2。按位\u非(img,img)
#gray=cv2.imread(args[“image”],0)
灰色=cv2.CVT颜色(img,cv2.COLOR\U BGR2GRAY)
灰色=cv2.高斯模糊(灰色,(5,5,0)
cv2.imshow(“灰色”,灰色)
cv2.等待键(0)
gray=cv2.规格化(gray,gray,alpha=0,beta=255,norm\u type=cv2.norm\u MINMAX,dtype=cv2.CV\u 8UC1)
cv2.imshow(“标准化”,灰色)
cv2.等待键(0)
#sys.exit()
ret,thresh=cv2.阈值(灰色,230,255,cv2.thresh\u二进制\u INV)
cv2.imshow('thresh',thresh)
#cv2.imwrite('./wni230.png',thresh)
cv2.等待键(0)
平方英尺=[]
thresh=cv2.morphologyEx(thresh,cv2.MORPH\u CLOSE,np.one((5,5),np.uint8))
cv2.imshow(“放大”,阈值)
cv2.等待键(0)
#sys.exit()
t图像,轮廓,h=cv2.找到的轮廓(脱粒,cv2.RETR\u外部,cv2.链近似\u简单)
cnt0=等高线[0]
tmp_h=h[:-1]
对于索引,枚举中的cnt(等高线[:-1]):
打印“!!!!!!!!!!!新一轮
打印cv2.arcLength(cnt,真)
#印刷碳纳米管
近似=cv2.近似聚合度(cnt,0.1*cv2.弧长(cnt,真),真)
打印“**********约*******”
打印大约
打印透镜(约)
打印“**********大约结束******”
如果len(近似值)=4:
打印'h[i][2]:%s'%tmp_h[0][index][2]
打印'h[i][3]:%s'%tmp_h[0][index][3]
如果ret>0.5:
打印“平行四边形”
elif 0.3=96:
x、 y,w,h=cv2.边界矩形(近似值)
对于j英寸,大约:
圆(img,(int(j[0][0]),int(j[0][1]),1,(0,255,0),2)
cv2.imshow(“最终”,img[y:y+h,x:x+w])
cv2.等待键(0)
打印“目标但检测到长时间静噪…”
cv2.等待键(0)
cv2.imshow(“img”,img)
cv2.imwrite('tmp.png',img)
cv2.等待键(0)
cv2.destroyAllWindows()
因此,我得到了两个输出图像:

第一:

第二:

红色的线是轮廓,绿色的点是approxPolyDP检测到的点

所以,第一个是我想要的

但是,approxPolyDP如何定位第二个点的最高点

谁能解释一下规则吗

环境:

Python:2.7.10

Opencv:3.2.0

谢谢


韦斯利

有一个关于Ramer–Douglas–Peucker算法的很好的描述,上面有动画。根据这是使用的算法
approxPolyDP
而且OpenCV是开源的,如果您对detailsThx感兴趣,您只需查看代码即可。我将查看文档或代码以了解更多详细信息。实际上,我想知道如何检测第二幅图像的正方形。