Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. import numpy as np
  2. import cv2 as cv2
  3. import imutils
  4.  
  5. # initialize list of reference points
  6. # Flag for cropping
  7. ref_point = []
  8.  
  9. cropping = False
  10.  
  11. # Location of the Image
  12. imgLocation = 'C:/Users/diego/OneDrive/Desktop/hw2_3.jpg'
  13.  
  14. # Assigning image to variable
  15. img = cv2.imread(imgLocation, cv2.IMREAD_UNCHANGED)
  16. img = imutils.resize(img, height=600)
  17. img = imutils.resize(img, width=600)
  18.  
  19. # Displaying original image
  20. cv2.imshow('hw2', img)
  21. cv2.waitKey(0)
  22.  
  23. # CHANGING TO GREY
  24. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  25. gray = cv2.GaussianBlur(gray, (7, 7), 0)
  26.  
  27. # Displaying Original image with Canny Edge detection
  28. edges = cv2.Canny(gray, 50, 100)
  29.  
  30. #cv2.imshow("Image with Canny Algorithm", edges)
  31. #cv2.waitKey(0)
  32.  
  33. # Dilation and erosion to close gaps in image
  34. edges = cv2.dilate(edges, None, iterations=2)
  35. edges = cv2.erode(edges, None, iterations=2)
  36.  
  37. # find contours in the edge map
  38. contours = cv2.findContours(edges.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  39. contours = imutils.grab_contours(contours)
  40. drawing = np.zeros([600, 600],np.uint16)
  41.  
  42. counter = 0
  43. for c in contours:
  44. # Center of contour
  45. M = cv2.moments(c)
  46. pointX = int(M["m10"] / M["m00"])
  47. pointY = int(M["m01"] / M["m00"])
  48.  
  49. cv2.drawContours(drawing, [c], 0, (13000000), 2)
  50. cv2.putText(drawing, str(counter), (pointX - 5, pointY - 5), cv2.FONT_HERSHEY_DUPLEX, 0.5, (13000000), 1)
  51. counter = counter + 1
  52.  
  53. def contour_counter(event, x, y, flags, param):
  54. # grab references to the global variables
  55. global ref_point
  56. # if the left mouse button was clicked, record the starting
  57. # (x, y) coordinates and indicate that cropping is being performed
  58. if event == cv2.EVENT_LBUTTONDOWN:
  59. ref_point = [(x, y)]
  60.  
  61. # check to see if the left mouse button was released
  62. elif event == cv2.EVENT_LBUTTONUP:
  63. # record the ending (x, y) coordinates and indicate that
  64. # the cropping operation is finished
  65. ref_point.append((x, y))
  66.  
  67. # draw a rectangle around the region of interest
  68. rect = cv2.rectangle(drawing, ref_point[0], ref_point[1], (13000000), 1)
  69. cv2.imshow("image", drawing)
  70.  
  71. if (ref_point[0][0] < ref_point[1][0] and ref_point[0][1] < ref_point[1][1]):
  72. parent_contour = np.array([[[ref_point[0][0], ref_point[0][1]],
  73. [ref_point[1][0], ref_point[0][1]],
  74. [ref_point[1][0], ref_point[1][1]],
  75. [ref_point[0][0], ref_point[1][1]]]])
  76. elif (ref_point[0][0] < ref_point[1][0] and ref_point[0][1] > ref_point[1][1]):
  77. parent_contour = np.array([[ref_point[0][0], ref_point[0][1]],
  78. [ref_point[0][0], ref_point[1][1]],
  79. [ref_point[1][0], ref_point[1][1]],
  80. [ref_point[1][0], ref_point[0][1]]])
  81. elif (ref_point[0][0] > ref_point[1][0] and ref_point[0][1] < ref_point[1][1]):
  82. parent_contour = np.array([[ref_point[0][0], ref_point[0][1]],
  83. [ref_point[0][0], ref_point[1][1]],
  84. [ref_point[1][0], ref_point[1][1]],
  85. [ref_point[1][0], ref_point[0][1]]])
  86. else:
  87. parent_contour = np.array([[ref_point[0][0], ref_point[0][1]],
  88. [ref_point[1][0], ref_point[0][1]],
  89. [ref_point[1][0], ref_point[1][1]],
  90. [ref_point[0][0], ref_point[1][1]]])
  91. inside_rect = 0
  92. outside_rect = 0
  93. touching_rect = 0
  94. counterooo = -1
  95. for c in contours:
  96. flag_inside = False
  97. flag_outside = False
  98. flag_touching = False
  99. counterooo = counterooo+1
  100. for i in range(len(c)):
  101. x = c[i][0][0]
  102. y = c[i][0][1]
  103. result = cv2.pointPolygonTest(parent_contour, (x, y), False)
  104.  
  105. if result > 0:
  106. flag_inside = True
  107. elif cv2.pointPolygonTest(parent_contour, (x, y), False) == 0:
  108. flag_touching = True
  109. else:
  110. flag_outside = True
  111.  
  112. if flag_inside and flag_outside:
  113. print ("Contour with ID: " + str(counterooo), " is touching")
  114. touching_rect = touching_rect + 1
  115. elif flag_inside and flag_touching:
  116. print("Contour with ID: " + str(counterooo), " is touching")
  117. touching_rect = touching_rect + 1
  118. elif flag_inside and not(flag_touching) and not(flag_outside):
  119. print("Contour with ID: " + str(counterooo), " is inside")
  120. inside_rect = inside_rect + 1
  121. else:
  122. outside_rect = outside_rect + 1
  123.  
  124.  
  125. print ("Number of Contours fully inside the square: ", inside_rect)
  126. print("Number of Contours touching the square: ", touching_rect)
  127.  
  128. cv2.namedWindow("image")
  129. cv2.setMouseCallback("image", contour_counter)
  130. cv2.imshow("image", drawing)
  131. cv2.waitKey(0)
  132.  
  133. # keep looping until the 'q' key is pressed
  134. while True:
  135.  
  136. # display the image and wait for a keypress
  137. cv2.imshow("image", drawing)
  138. key = cv2.waitKey(1) & 0xFF
  139.  
  140. # press 'r' to reset the window
  141. if key == ord("r"):
  142. image = drawing.copy()
  143.  
  144. # if the 'c' key is pressed, break from the loop
  145. elif key == ord("c"):
  146. break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement