Example #1
0
def find_marker(image, boundary):
    for idx, (lower, upper) in enumerate(boundary):
        lower = np.array(lower, dtype='uint8')
        upper = np.array(upper, dtype='uint8')

        # Threshold the HSV image
        mask = cv2.inRange(image, lower, upper)
        output = cv2.bitwise_and(image, image, mask=mask)

        h, s, v = cv2.split(output)

        blurred = cv2.GaussianBlur(v, (3, 3), 0)
        thresh = cv2.threshold(blurred, 50, 255, cv2.THRESH_BINARY)[1]

        cnts, max_cont, box = geom.find_main_contour(thresh.copy())
        c = max(cnts, key=cv2.contourArea)
        return cv2.minAreaRect(c)
def find_marker(image, boundary):
    for idx, (lower, upper) in enumerate(boundary):
        lower = np.array(lower, dtype = 'uint8')
        upper = np.array(upper, dtype = 'uint8')

        # Threshold the HSV image
        mask = cv2.inRange(image, lower, upper)
        output = cv2.bitwise_and(image, image, mask = mask)

        h, s, v = cv2.split(output)

        blurred = cv2.GaussianBlur(v, (3, 3), 0)
        thresh = cv2.threshold(blurred, 50, 255, cv2.THRESH_BINARY)[1]

        # cnts, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        # cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:1]
        cnts, max_cont, box = geom.find_main_contour(thresh.copy())
        c = max(cnts, key = cv2.contourArea)
        # cnts = imutils.grab_contours(cnts)
        return cv2.minAreaRect(c)
Example #3
0
# cv2.waitKey(0)

boundaries = [([65, 180, 80], [80, 255, 110])]  #GREEN

lowerG = np.array(boundaries[0][0], dtype='uint8')
upperG = np.array(boundaries[0][1], dtype='uint8')
# mask = cv2.inRange(hsv, lowerG, upperG)
# kernel = np.ones((3,3),np.uint8)
# morph = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
# morph = cv2.morphologyEx(morph, cv2.MORPH_OPEN, kernel)
mask = cv2.inRange(hsv, lowerG, upperG)
# output = cv2.bitwise_and(img, img, mask=mask)
cv2.imshow('mask', mask)
# cv2.imshow('output', output)
# cv2.imshow('morph', morph)
# cv2.waitKey(0)
# output = cv2.bitwise_and(img, img, mask = morph)
cnts, max_cont, box = geom.find_main_contour(mask)
c = max(cnts, key=cv2.contourArea)
# cnts, hierarchy = cv2.findContours(mask, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
# c = sorted(cnts, key=cv2.contourArea, reverse=True)[:1]
cv2.drawContours(img, c, -1, (255, 0, 0), 2)
cv2.imshow('contour', img)
cv2.waitKey(0)

mark = cv2.minAreaRect(c)
box = cv2.boxPoints(mark)
box = np.int0(box)
cv2.drawContours(img, [box], -1, (0, 0, 255), 2)
cv2.imshow('box', img)
cv2.waitKey(0)
# boundaries = [([60, 90, 80], [90, 255, 110])]  #GREEN
# boundaries = [([50, 80, 0], [90, 150, 20])] # sterling_demo 201
boundaries = [([40, 80, 0], [70, 255, 80])]  # sterling_demo 102

lowerG = np.array(boundaries[0][0], dtype='uint8')
upperG = np.array(boundaries[0][1], dtype='uint8')

kernel = np.ones((3, 3), np.uint8)
mask = cv2.inRange(hsv, lowerG, upperG)
cv2.imshow('mask', mask)
# cv2.waitKey(0)
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
# cv2.waitKey(0)

img_copy = img.copy()
cnts, max_cont, approx_cnt = geom.find_main_contour(mask)
cv2.drawContours(img_copy, max_cont, -1, (255, 0, 0), 2)
cv2.imshow('first contour', img_copy)
cv2.drawContours(img_copy, [approx_cnt], -1, (0, 0, 255), 2)
cv2.imshow('approx contour', img_copy)
# cv2.waitKey(0)
# img_copy = img.copy()
# cv2.drawContours(img_copy, [box], -1, (255, 0, 0), 2)
# cv2.imshow('box', img_copy)
#
# warp = four_point_transform(img, box)
# cv2.imshow('warp image', warp)
# cv2.waitKey(0)

# our_cnt = None
peri = cv2.arcLength(approx_cnt, True)
Example #5
0
image = cv2.imread(imgpath)
w, h = image.shape[:2]
# Convert to HSV color space
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# Define range of white color in HSV
lower_yellow = np.array([20, 40, 240])
upper_yellow = np.array([230, 255, 255])
# Threshold the HSV image
mask = cv2.inRange(hsv, lower_yellow, upper_yellow)
# Remove noise
kernel_erode = np.ones((4, 4), np.uint8)
eroded_mask = cv2.erode(mask, kernel_erode, iterations=1)
kernel_dilate = np.ones((6, 6), np.uint8)
dilated_mask = cv2.dilate(eroded_mask, kernel_dilate, iterations=1)

conts, max_cont, box = geom.find_main_contour(dilated_mask)

if max_cont is not None:
    M = cv2.moments(max_cont)

    cx = int(M['m10'] / M['m00'])
    cy = int(M['m01'] / M['m00'])
    print("Centroid of the biggest area: ({}, {})".format(cx, cy))
    cv2.circle(image, (cx, cy), 1, (0, 0, 255))
    # cv2.rectangle(image, )
    p1, p2 = geom.calc_box_vector(box)
    if p1 is not None:
        angle = geom.get_vert_angle(p1, p2, w, h)
        shift = geom.get_horz_shift(p1[0], w)
        print("angle of biggest area: {} degrees".format(angle))