【问题标题】:To detect shapes in a irregular image检测不规则图像中的形状
【发布时间】:2018-01-20 10:04:38
【问题描述】:

尝试使用轮廓、边缘检测在不规则对象中找到圆形和矩形(或正方形),但无法正确获取输出。

  1. 我尝试更改 canny 值和 epsilon(contour approx) 的值,但无法检测到,

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

谁能帮我用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()

【问题讨论】:

  • 在这种情况下,您是否需要仅查找孔数,还是需要为许多图像查找孔数?
  • @GarvitaTiwariyes 用于许多图像。
  • 所有图像都有相同的金属物体吗?如果是,您可以仅对该金属对象进行基于颜色的分割(或使用模板匹配进行分割),然后您可以使用自适应二值化在分割图像中找到孔。在大多数情况下,它应该给出正确的输出。

标签: python opencv contour


【解决方案1】:

使用二值化。您将获得可以按大小、位置和其他几何标准区分的斑点。

【讨论】:

  • 看到 OpenCV 的用户在想要提取形状时如何急于进行边缘检测,这总是令人惊叹。在 99% 的情况下,Canny 无法使用,而简单或自适应二值化确实令人惊叹。
  • @vigneshs 不,我没有使用 OpenCV。但这是一个微不足道的二值化,一行代码。花费最少的精力。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多