2017-08-11 282 views
1

形狀嘗試使用輪廓,邊緣檢測,但不能正確得到輸出找到一個不規則的物體的圓和矩形(或正方形)。爲了檢測在不規則圖像

  1. 我試圖改變精明值和小量(輪廓約)的值,但無法檢測,

  2. 另一個困難IAM飾面大量手寫字符是否有在所述金屬物體,所以我的代碼正在檢測,也作爲一個形狀

任何人都可以請幫助我檢測這個對象使用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() 
+0

你有隻或許多圖像發現在這種情況下,孔數? –

+0

@GarvitaTiwariyes許多圖像。 –

+0

並且所有的圖像都有這個相同的金屬物體?如果是可以做到這一點的金屬物體僅(或分割使用模板匹配)的基於顏色的分割,然後就可以找到使用自適應二值化的分割圖像中的孔。它應該在大多數情況下給出正確的輸出。 –

回答

2

使用二值化。你會得到你可以通過大小,位置和其他幾何標準進行區分的斑點。

enter image description here

+0

總是令人驚訝地看到OpenCV的用戶如何急於邊緣檢測時,他們要提取的形狀。在99%的案例中,Canny無法使用,而簡單或適應性二值化確實令人驚歎。 –

+0

可以共享代碼 –

+0

@vigneshs不,我沒有使用OpenCV的。但這是一個微不足道的二值化,一行代碼。花費最少的努力。 –