Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import cv2 as cv2
- import imutils
- # initialize list of reference points
- # Flag for cropping
- ref_point = []
- cropping = False
- # Location of the Image
- imgLocation = 'C:/Users/diego/OneDrive/Desktop/hw2_3.jpg'
- # Assigning image to variable
- img = cv2.imread(imgLocation, cv2.IMREAD_UNCHANGED)
- img = imutils.resize(img, height=600)
- img = imutils.resize(img, width=600)
- # Displaying original image
- cv2.imshow('hw2', img)
- cv2.waitKey(0)
- # CHANGING TO GREY
- gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
- gray = cv2.GaussianBlur(gray, (7, 7), 0)
- # Displaying Original image with Canny Edge detection
- edges = cv2.Canny(gray, 50, 100)
- #cv2.imshow("Image with Canny Algorithm", edges)
- #cv2.waitKey(0)
- # Dilation and erosion to close gaps in image
- edges = cv2.dilate(edges, None, iterations=2)
- edges = cv2.erode(edges, None, iterations=2)
- # find contours in the edge map
- contours = cv2.findContours(edges.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
- contours = imutils.grab_contours(contours)
- drawing = np.zeros([600, 600],np.uint16)
- counter = 0
- for c in contours:
- # Center of contour
- M = cv2.moments(c)
- pointX = int(M["m10"] / M["m00"])
- pointY = int(M["m01"] / M["m00"])
- cv2.drawContours(drawing, [c], 0, (13000000), 2)
- cv2.putText(drawing, str(counter), (pointX - 5, pointY - 5), cv2.FONT_HERSHEY_DUPLEX, 0.5, (13000000), 1)
- counter = counter + 1
- def contour_counter(event, x, y, flags, param):
- # grab references to the global variables
- global ref_point
- # if the left mouse button was clicked, record the starting
- # (x, y) coordinates and indicate that cropping is being performed
- if event == cv2.EVENT_LBUTTONDOWN:
- ref_point = [(x, y)]
- # check to see if the left mouse button was released
- elif event == cv2.EVENT_LBUTTONUP:
- # record the ending (x, y) coordinates and indicate that
- # the cropping operation is finished
- ref_point.append((x, y))
- # draw a rectangle around the region of interest
- rect = cv2.rectangle(drawing, ref_point[0], ref_point[1], (13000000), 1)
- cv2.imshow("image", drawing)
- if (ref_point[0][0] < ref_point[1][0] and ref_point[0][1] < ref_point[1][1]):
- parent_contour = np.array([[[ref_point[0][0], ref_point[0][1]],
- [ref_point[1][0], ref_point[0][1]],
- [ref_point[1][0], ref_point[1][1]],
- [ref_point[0][0], ref_point[1][1]]]])
- elif (ref_point[0][0] < ref_point[1][0] and ref_point[0][1] > ref_point[1][1]):
- parent_contour = np.array([[ref_point[0][0], ref_point[0][1]],
- [ref_point[0][0], ref_point[1][1]],
- [ref_point[1][0], ref_point[1][1]],
- [ref_point[1][0], ref_point[0][1]]])
- elif (ref_point[0][0] > ref_point[1][0] and ref_point[0][1] < ref_point[1][1]):
- parent_contour = np.array([[ref_point[0][0], ref_point[0][1]],
- [ref_point[0][0], ref_point[1][1]],
- [ref_point[1][0], ref_point[1][1]],
- [ref_point[1][0], ref_point[0][1]]])
- else:
- parent_contour = np.array([[ref_point[0][0], ref_point[0][1]],
- [ref_point[1][0], ref_point[0][1]],
- [ref_point[1][0], ref_point[1][1]],
- [ref_point[0][0], ref_point[1][1]]])
- inside_rect = 0
- outside_rect = 0
- touching_rect = 0
- counterooo = -1
- for c in contours:
- flag_inside = False
- flag_outside = False
- flag_touching = False
- counterooo = counterooo+1
- for i in range(len(c)):
- x = c[i][0][0]
- y = c[i][0][1]
- result = cv2.pointPolygonTest(parent_contour, (x, y), False)
- if result > 0:
- flag_inside = True
- elif cv2.pointPolygonTest(parent_contour, (x, y), False) == 0:
- flag_touching = True
- else:
- flag_outside = True
- if flag_inside and flag_outside:
- print ("Contour with ID: " + str(counterooo), " is touching")
- touching_rect = touching_rect + 1
- elif flag_inside and flag_touching:
- print("Contour with ID: " + str(counterooo), " is touching")
- touching_rect = touching_rect + 1
- elif flag_inside and not(flag_touching) and not(flag_outside):
- print("Contour with ID: " + str(counterooo), " is inside")
- inside_rect = inside_rect + 1
- else:
- outside_rect = outside_rect + 1
- print ("Number of Contours fully inside the square: ", inside_rect)
- print("Number of Contours touching the square: ", touching_rect)
- cv2.namedWindow("image")
- cv2.setMouseCallback("image", contour_counter)
- cv2.imshow("image", drawing)
- cv2.waitKey(0)
- # keep looping until the 'q' key is pressed
- while True:
- # display the image and wait for a keypress
- cv2.imshow("image", drawing)
- key = cv2.waitKey(1) & 0xFF
- # press 'r' to reset the window
- if key == ord("r"):
- image = drawing.copy()
- # if the 'c' key is pressed, break from the loop
- elif key == ord("c"):
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement