首页 文章

检测不规则图像中的形状

提问于
浏览
1

尝试使用轮廓,边缘检测但不能正确地找到不规则对象中的圆和矩形(或方形) .

  • 我尝试更改canny值和epsilon(轮廓近似)的值,但无法检测到,

  • 我面临的另一个困难是金属物体中有很多手写字符,所以我的代码也检测到它也是一个形状

谁能帮助我使用opencv-python在这个对象上检测这个必需的形状 .

金属物体

import imutils
import cv2
import numpy as np
import matplotlib.pyplot as plt

image = cv2.imread('part1.jpg')

#image = cv2.imread('C:\Python27\plates\plates2.1.jpg')#$episolon==0.04,len=5,6
#image = cv2.imread('C:\Python27\plates\plates4.jpg')
#image = cv2.imread('C:\Python27\plates\plates1.jpg')
#image = cv2.imread('C:\Python27\plates\plates3.jpg')#episilon=0.0370,len=5
#image = cv2.imread('C:\Python27\plates\plates5.jpg') #change the episilon to 0.01
#image = cv2.imread('C:\Python27\plates\plates6.jpg')#not working properly


cv2.namedWindow('Image')




#for angle in xrange(0, 360, 90):
# rotate the image and display it
#image = imutils.rotate(image, angle=angle)


#gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
#blurred = cv2.GaussianBlur(gray, (5, 5), 0)


#edges=cv2.Canny(image,200,650)#plates3.jpg,plates1.jpg,plates5.jpg,
#edges=cv2.Canny(image,200,500)#plates4.jpg
#edges=cv2.Canny(image,200,589)#plates2.1.jpg
#edges=cv2.Canny(image,100,450)
edges=cv2.Canny(image,300,589)
kernel = np.ones((5,5),np.uint8)


#thresh = cv2.erode(edges,kernel,iterations = 1)
#thresh = cv2.dilate(edges,kernel,iterations = 1)
#thresh = cv2.morphologyEx(edges, cv2.MORPH_OPEN, kernel)
thresh = cv2.morphologyEx(edges, cv2.MORPH_CLOSE, kernel)

cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
sd = ShapeDetector()
print len(cnts)
for c in cnts:

        shape = "unidentified"
        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.0373* peri, True)





        if len(approx) == 4:

            (x, y, w, h) = cv2.boundingRect(approx)
            #ar = w / float(h)


            #shape = "slots" if ar >= 0.95 and ar <= 1.05 else "slots"
            shape="slots"
            #cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
            rect = cv2.minAreaRect(c)
            box = cv2.boxPoints(rect)
            box = np.int0(box)
            cv2.drawContours(image,[box],0,(0,0,255),2)
            #cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),2)
            cv2.putText(image, shape, (int(x), int(y)), cv2.FONT_HERSHEY_SIMPLEX,0.5, (255, 255, 255), 4)

        elif len(approx)==2:
            shape="nothing"

            (x,y),radius = cv2.minEnclosingCircle(c)
            center = (int(x),int(y))
            radius = int(radius)
            #cv2.circle(image,center,radius,(0,255,0),2)
            #cv2.putText(image, shape, (int(x), int(y)), cv2.FONT_HERSHEY_SIMPLEX,0.5, (255, 255, 255), 4)

        elif len(approx)==5:
            shape="nothing"
        elif len(approx)==3:
            shape="nothing"

        elif len(approx)==6:
            shape="nothing"





        else:
            shape = "c"+str(len(approx))
            (x,y),radius = cv2.minEnclosingCircle(c)
            center = (int(x),int(y))
            radius = int(radius)
            cv2.circle(image,center,radius,(0,255,0),2)
            cv2.putText(image, shape, (int(x), int(y)), cv2.FONT_HERSHEY_SIMPLEX,0.5, (255, 255, 255), 2)


cv2.imshow("Image",image)
cv2.imshow("edges", thresh)

cv2.waitKey(0)
cv2.destroyAllWindows()

1 回答

  • 2

    使用二值化 . 您将获得可以根据大小,位置和其他几何标准区分的blob .

    enter image description here

相关问题