Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2015
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.89 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3.  
  4. def distanceBtwPoints(p0, p1):
  5.     return np.sqrt((p0[0] - p1[0])**2 + (p0[1] - p1[1])**2)
  6.  
  7. def findNearestPointIndex(pt, Points):
  8.     mindistance = 1e9
  9.  
  10.     for i in range(len(Points)):
  11.         print "Points i", Points[i]
  12.         print "pt", pt
  13.         distance = distanceBtwPoints(pt, Points[i])
  14.         print 'distance', distance
  15.  
  16.         if distance < mindistance :
  17.             mindistance =  distance
  18.             nearestpointindex = i
  19.  
  20.  
  21.  
  22.     return  nearestpointindex
  23.  
  24. if __name__ == '__main__':
  25.     img = cv2.imread("bridgeTestSource.png")
  26.     src = cv2.imread("bridgeTestSource.png")
  27.  
  28.     gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  29.     gray = np.asarray(gray < 127, dtype=np.uint8) # need to cast back to uint8
  30.  
  31.  
  32.     contours, hierarchy = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
  33.  
  34.     cv2.drawContours(img, contours,-1, (0,0,255), 2)
  35.  
  36.  
  37.     for i in range(len(contours)):
  38.         print "Contour ", i , "points", contours[i]
  39.         print
  40.         print " - Contour ", i, "area :", cv2.contourArea(contours[i])
  41.         print
  42.         for j in range(contours[i].shape[0]):
  43.             print "contour x y", contours[i][j].squeeze()
  44.         print "the contour" , i, "distance between ", contours[i][0].squeeze(),contours[i][1].squeeze(), "is " , distanceBtwPoints(contours[i][0].squeeze(), contours[i][1].squeeze())
  45.  
  46.  
  47.     cnt = contours[0]
  48.  
  49.     for i in range(len(contours)):
  50.         if len(cnt) < len(contours[i]):
  51.             cnt = contours[i]
  52.  
  53.     for i in range(len(contours)):
  54.  
  55.         if not np.array_equal(cnt, contours[i]) and len(contours[i]) > 4:
  56.             for j in range(len(contours[i])):
  57.                 pt0 = contours[i][j]
  58.  
  59.                 cnt_nearIdx = cnt[findNearestPointIndex(pt0.squeeze(), cnt.squeeze())].squeeze()
  60.  
  61.                 print "pt0", pt0
  62.                 print "cnt_nearIdx", cnt_nearIdx
  63.                 lcnt_nearIdx = cnt_nearIdx.tolist()
  64.                 lpt0 = (pt0.squeeze()).tolist()
  65.                 print "lpt0", lpt0
  66.  
  67.                 tcnt_nearIdx = tuple(lcnt_nearIdx)
  68.                 tpt0 = tuple(lpt0)
  69.                
  70.                 print "tpt0", tpt0
  71.                 print "tcnt_near", tcnt_nearIdx
  72.  
  73.                 cv2.line(img,tpt0, tcnt_nearIdx,(0,0,0),1,8)
  74.    
  75.  
  76.     cv2.imwrite('nimg.jpg', img)
  77.  
  78.     gray_src = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
  79.     gray_src = np.asarray(gray_src < 127, dtype=np.uint8) # need to cast back to uint8
  80.  
  81.  
  82.     contours1, hierarchy = cv2.findContours(gray_src, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  83.  
  84.     cv2.drawContours(src, contours1,-1, (0,0,0), 0)
  85.  
  86.     m_xor= np.ones(src.shape[:2], dtype="uint8") * 255
  87.  
  88.     print type(cnt)
  89.     print cnt
  90.     ncnt = cnt.astype(np.int32)
  91.     print type(ncnt)
  92.    
  93.     cv2.polylines(m_xor, [ncnt], 1, (0,0,0))
  94.  
  95.  
  96.     m_xor = 255 - (src or m_xor)
  97.  
  98.     cv2.imwrite('result.jpg', m_xor)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement