python正方形检测_python cv2在多个图像中错误地检测正方形形状

该博客介绍了使用Python和OpenCV库进行正方形检测的方法。通过读取图片,转换为灰度,阈值处理,寻找轮廓,并使用approxPolyDP和matchShapes函数来判断形状。博客中遇到的问题是程序错误地将一些非正方形形状识别为正方形,包括长方形、平行四边形等,并展示了不同形状的识别结果。
摘要由CSDN通过智能技术生成

#!/usr/bin/env python

# coding: utf-8

import numpy as np

import cv2

import argparse

import math

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)

ret, thresh = cv2.threshold(gray, 230, 255, 1)

cv2.imshow('thresh', thresh)

# cv2.imwrite('./wni230.png',thresh)

cv2.waitKey(0)

square_cnts = []

##################################################

shape = cv2.imread('./shape1.png')

shape_gray = cv2.cvtColor(shape, cv2.COLOR_BGR2GRAY)

ret, shape_thresh = cv2.threshold(shape_gray, 0, 255, 0)

tmpimage, contours, h = cv2.findContours(shape_thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

shape_cnt0 = contours[0]

shape_approx = []

for i in contours:

approx = cv2.approxPolyDP(i, 0.01*cv2.arcLength(i, True), True)

# print len(approx)

if len(approx) == 4:

shape_approx.append(len(approx))

##################################################

# tmpimage,contours,h = cv2.findContours(thresh,1,2)

# cv2.RETR_TREE

tmpimage, contours, h = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

cnt0 = contours[0]

# from skimage import measure

# contours = measure.find_contours(thresh, 0.8)

for cnt in contours[::-1]:

print cv2.arcLength(cnt, True)

print cnt

approx = cv2.approxPolyDP(cnt, 0.1*cv2.arcLength(cnt, True), True)

print len(approx)

if len(approx) == 5:

print "pentagon"

cv2.drawContours(img, [cnt], 0, 255, 2)

cv2.imshow('tmppentagon', img)

cv2.waitKey(0)

elif len(approx) == 3:

print "triangle"

cv2.drawContours(img, [cnt], 0, (0, 255, 0), 2)

cv2.imshow('tmptriangle', img)

cv2.waitKey(0)

elif len(approx) == 2:

print 'approx:'

print approx

ret = cv2.matchShapes(cnt, cnt0, 1, 0.0)

print 'match shape ret:%s' % ret

print "two approx line"

cv2.drawContours(img, [cnt], 0, (0, 255, 0), 2)

cv2.imshow('twoline?', img)

cv2.waitKey(0)

elif len(approx) == 4:

ret = cv2.matchShapes(cnt, cnt0, 1, 0.0)

print 'match shape ret:%s' % ret

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, [approx], 0, (0, 0, 255), 2)

cv2.imshow('tmpsquare', img)

cv2.waitKey(0)

if int(cv2.arcLength(cnt, True)) >= 96:

if math.fabs(math.sqrt((approx[0][0][0]-approx[1][0][0])**2+(approx[0][0][1]-approx[1][0][1])**2) - math.sqrt((approx[1][0][0]-approx[2][0][0])**2+(approx[1][0][1]-approx[2][0][1])**2)) <= 5:

x, y, w, h = cv2.boundingRect(cnt)

cv2.imshow('final',img[y:y+h,x:x+w])

cv2.waitKey(0)

print 'target but long squere detected...'

cv2.waitKey(0)

elif len(approx) == 9:

print "half-circle"

cv2.drawContours(img,[cnt],0,(255,255,0),2)

cv2.imshow('tmphalfcircle',img)

cv2.waitKey(0)

elif len(approx) > 15:

print "circle"

cv2.drawContours(img,[cnt],0,(0,255,255),2)

cv2.imshow('tmpcircle',img)

cv2.waitKey(0)

cv2.imshow('img', img)

cv2.imwrite('tmp.png', img)

cv2.waitKey(0)

cv2.destroyAllWindows()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值