2017-08-11 13 views
1

輪郭を使って不規則なオブジェクトで円と長方形(または正方形)を見つけようとすると、エッジ検出が正しく出力されません。別の困難IAM対面がたくさん手文字を書かれている不規則な画像の形を検出するには

    1. 私は気の利いた値とイプシロン(輪郭約)の値を変更しようとしたが、検出することができませんでしたが、私のコードので、金属物であります形状としても検出しています。

    誰でもこのオブジェクトのこの形状を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は使用できませんが、単純または適応2値化は驚異的です。 –

    +0

    あなたはコードを共有できます –

    +0

    @vigneshsいいえ、私はOpenCVを使用しませんでした。しかし、これは簡単な二進化であり、1行のコードです。最低限の努力をしてください。 –

    関連する問題