Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2015
174
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. # def findNearestPointIndex(pt, Points):
  25. #     mindistance = 1e9
  26. #
  27. #     for ip in Points:
  28. #
  29. # #         print "ip", ip
  30. # #         print "pt", pt
  31. #
  32. #         distance = distanceBtwPoints(pt, ip)
  33. #         print 'distance', distance
  34. # #
  35. #         if distance < mindistance :
  36. #             mindistance =  distance
  37. #             nearestpoint = ip
  38. #             print "nearestpoint", nearestpoint
  39. #             nearestpointindex = nearestpoint.argmin()
  40. #             print "nearestpointindex", nearestpoint.argmin()
  41. #
  42. #
  43. #     return  nearestpointindex
  44.  
  45. if __name__ == '__main__':
  46.     img = cv2.imread("bridgeTestSource.png")
  47. #     src = cv2.imread("bridgeTestSource.png")
  48.  
  49.     gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  50.     gray = np.asarray(gray < 127, dtype=np.uint8) # need to cast back to uint8
  51.  
  52.  
  53.     contours, hierarchy = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
  54.  
  55.     cv2.drawContours(img, contours,-1, (0,0,0), 1)
  56.     cnt = contours[0]
  57.  
  58.     for i in range(len(contours)):
  59.         if len(cnt) < len(contours[i]):
  60.             cnt = contours[i]
  61.     for i in range(len(contours)):
  62.  
  63.         if not np.array_equal(cnt, contours[i]) and len(contours[i]) > 10:
  64.             for j in range(len(contours[i])):
  65.                 pt0 = contours[i][j]
  66.  
  67.                 cnt_nearIdx = cnt[findNearestPointIndex(pt0.squeeze(), cnt.squeeze())].squeeze()
  68.                 lcnt_nearIdx = cnt_nearIdx.tolist()
  69.                 lpt0 = (pt0.squeeze()).tolist()
  70.                 tcnt_nearIdx = tuple(lcnt_nearIdx)
  71.                 tpt0 = tuple(lpt0)
  72.                 cv2.line(img,tpt0, tcnt_nearIdx,(0,0,0),1,8)
  73.    
  74.     cv2.imwrite('nimg.jpg', img)
  75.  
  76.     gray_src = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  77.     gray_src1 = np.asarray(gray_src < 127, dtype=np.uint8) # need to cast back to uint8
  78.  
  79.  
  80.     contours1, hierarchy = cv2.findContours(gray_src1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  81.  
  82.     imgMask = np.ones(img.shape)*255
  83.  
  84.     cv2.drawContours(imgMask, contours1,-1, 0, 0)
  85.  
  86.     m_xor= np.ones(imgMask.shape) * 255
  87.  
  88.     ncnt = cnt.astype(np.int32)
  89.    
  90.     cv2.polylines(m_xor, [ncnt], 1, (0,0,0))
  91.     cv2.imwrite('resultpoly.jpg', m_xor)
  92.    
  93.  
  94.     newImg = (cv2.bitwise_xor(imgMask, m_xor))
  95.    
  96.     invNewImg = cv2.bitwise_not(newImg)
  97.  
  98.     cv2.imwrite('result.jpg', newImg)
  99.     cv2.imwrite('result2.jpg', invNewImg)
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement