def intersectionRate(s1, s2):

    x1, y1, x2, y2 = s1
    s1 = np.array([[x1, y1], [x2,y1], [x2, y2], [x1, y2]])

    area, _intersection = cv2.intersectConvexConvex(s1, np.array(s2))
    return 2 * area / (cv2.contourArea(s1) + cv2.contourArea(np.array(s2)))
Exemple #2
0
def intersectionRate(s1, s2):

    x1, y1, x2, y2 = s1
    s1 = np.array([[x1, y1], [x2, y1], [x2, y2], [x1, y2]])

    area, _intersection = cv2.intersectConvexConvex(s1, np.array(s2))
    return 2 * area / (cv2.contourArea(s1) + cv2.contourArea(np.array(s2)))
def findBillsContrast(img, coins):
    surArea = img.shape[0] * img.shape[1]
    contours = []
    contrast = apply_brightness_contrast(img, 0, 127)
    contrast = 255 - contrast
    #cv2.imshow('Contrast', contrast)
    gray = cv2.cvtColor(contrast, cv2.COLOR_BGR2GRAY)
    _, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY)
    #cv2.imshow('Binary', binary)
    _, cont, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL,
                                  cv2.CHAIN_APPROX_SIMPLE)
    cont = sorted(cont, key=cv2.contourArea, reverse=True)[:10]
    for cnt in cont:
        peri = cv2.arcLength(cnt, True)
        approx = cv2.approxPolyDP(cnt, 0.02 * peri, True)
        area = cv2.contourArea(cnt)

        if 7 > len(approx) > 3 and 0.05 * surArea < area < 0.30 * surArea:
            peri2 = 6 * np.sqrt(area * 1.25 / 2)
            if 1.15 > peri / peri2 > 0.85:
                contours.append(approx)

    coins2 = []
    if coins is not None:
        for c in coins:
            inside = False
            for cnt in contours:
                area, intersection = cv2.intersectConvexConvex(c, cnt)
                if area > 0:
                    inside = True
            if not inside:
                coins2.append(c)

    #print("findBillsContrast found: ", len(contours))
    return contours, coins2
Exemple #4
0
def convContoursIntersectiponRate(c1, c2):

    s1 = cv.contourArea(c1)
    s2 = cv.contourArea(c2)

    s, _ = cv.intersectConvexConvex(c1, c2)

    return 2*s/(s1+s2)
Exemple #5
0
def rectangleness(hull):
    rect = cv2.boundingRect(hull)
    rectPoints = np.array([[rect[0], rect[1]], [rect[0] + rect[2], rect[1]],
                           [rect[0] + rect[2], rect[1] + rect[3]],
                           [rect[0], rect[1] + rect[3]]])
    intersection_area = cv2.intersectConvexConvex(np.array(rectPoints),
                                                  hull)[0]
    rect_area = cv2.contourArea(rectPoints)
    rectangleness = intersection_area / rect_area
    return rectangleness
Exemple #6
0
def calc_iou_area(poly1, poly2, size1, size2):
    #size1 = cv2.contourArea((poly1[0:8].reshape((4,2))).astype(np.float32))
    #size2 = cv2.contourArea((poly2[0:8].reshape((4,2))).astype(np.float32))
    inter = cv2.intersectConvexConvex((poly1[0:8].reshape(
        (4, 2))).astype(np.float32), (poly2[0:8].reshape(
            (4, 2))).astype(np.float32))
    inter_size = inter[0]
    if size1 + size2 - inter_size == 0:
        print("calc_iou error, size1 + size2 - inter_size == 0 !!!!!!!!!!!!")
        return 0
    iou = inter_size / (size1 + size2 - inter_size)
    return iou
def findBillsD(img, coins):
    contours = []
    surArea = img.shape[0] * img.shape[1]
    #convert to HSV color scheme
    flat_object_resized_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    # split HSV to three chanels
    hue, saturation, value = cv2.split(flat_object_resized_hsv)
    # threshold to find the contour

    retval, thresholded = cv2.threshold(
        saturation, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)

    #wypełnienie dziur
    thresholded_open = cv2.morphologyEx(thresholded, cv2.MORPH_OPEN, (7, 7))
    thresholded_close = cv2.morphologyEx(thresholded_open, cv2.MORPH_CLOSE,
                                         (7, 7))

    _, cont, _ = cv2.findContours(thresholded_close, cv2.RETR_EXTERNAL,
                                  cv2.CHAIN_APPROX_SIMPLE)
    cont = sorted(cont, key=cv2.contourArea, reverse=True)[:10]

    for cnt in cont:
        # approximate the contour
        # These methods are used to approximate the polygonal curves of a contour.
        # In order to approximate a contour, you need to supply your level of approximation precision.
        # In this case, we use 2% of the perimeter of the contour. The precision is an important value to consider.
        # If you intend on applying this code to your own projects, you’ll likely have to play around with the precision value.
        peri = cv2.arcLength(cnt, True)
        approx = cv2.approxPolyDP(cnt, 0.02 * peri, True)
        area = cv2.contourArea(cnt)
        # if our approximated contour has four points, then
        # we can assume that we have found our screen
        if 7 > len(approx) > 3 and 0.05 * surArea < area < 0.30 * surArea:
            peri2 = 6 * np.sqrt(area * 1.25 / 2)
            if 1.15 > peri / peri2 > 0.85:
                contours.append(approx)
    #print("findBillsD found: ", len(contours))
    coins2 = []
    if coins is not None:
        for c in coins:
            inside = False
            for cnt in contours:
                area, intersection = cv2.intersectConvexConvex(c, cnt)
                if area > 0:
                    inside = True
            if not inside:
                coins2.append(c)

    return contours, coins2
Exemple #8
0
def compareContoursArtur(cnt1, cnt2):
    contours = []
    if len(cnt1) > 0:
        for c1 in cnt1:
            contours.append(c1)
    if len(cnt2) > 0:
        for c2 in cnt2:
            intersect = False
            for c1 in cnt1:
                area, intersection = cv2.intersectConvexConvex(c1, c2)
                #print('Intersect: {}'.format(area))
                if area > 0:
                    intersect = True
            if not intersect:
                contours.append(c2)
    return contours
Exemple #9
0
def detect_leds(
    frame: _np.ndarray,
    ksize: Tuple[int, int],
    aperture: int,
    low_threshold: int,
    low_threshold_mult: int,
) -> LedDetectionResults:
    rectangles: List[Rect] = []

    blurred = _cv.blur(frame, ksize)
    edges = _cv.Canny(
        blurred,
        low_threshold,
        low_threshold * low_threshold_mult,
        apertureSize=aperture,
    )
    contours, _ = _cv.findContours(edges, _cv.RETR_EXTERNAL,
                                   _cv.CHAIN_APPROX_SIMPLE)
    hulls: List[_np.ndarray] = []
    for c in contours:
        hulls.append(_cv.convexHull(c))
    # Find the indices of hulls that lie inside another hull.
    nested_hull_indices: Set[int] = set()
    if len(hulls) > 1:
        for j, h1 in enumerate(hulls):
            for k, h2 in enumerate(hulls):
                if h1 is h2:
                    continue
                area, _ = _cv.intersectConvexConvex(h1, h2, handleNested=True)
                if area > 0.0:
                    if area == _cv.contourArea(h1):
                        nested_hull_indices.add(j)
                    elif area == _cv.contourArea(h2):
                        nested_hull_indices.add(k)
    # Create bounding rectanges
    for j, h in enumerate(hulls):
        if j in nested_hull_indices:
            continue
        # boundingRect -> (x, y, w, h)
        rectangles.append(Rect(*_cv.boundingRect(h)))

    return LedDetectionResults(rectangles, contours, hulls)
Exemple #10
0
def cond_minrect_overlap(ra, rb, min_iou):
    """Check if IoU of minimum area (rotated) bounding rectangles is at least
    `min_iou`.

    Parameters
    ----------
    ra: array like
        First rectangle defined by the coordinates of four corners.
    rb: array like
        Second rectangle defined by the coordinates of four corners.
    min_iou: float
        Minimum overlap defined by intersection over union of bounding boxes.

    Returns
    -------
    bool
        True if area of overlap is greater or equal to `min_iou`.
    """
    area_i, _ = cv2.intersectConvexConvex(ra, rb)
    area_u = cv2.contourArea(ra) + cv2.contourArea(rb) - area_i
    return area_i >= min_iou * area_u  # Avoids divide by zero
Exemple #11
0
def py_cpu_nms(dets, thresh):
    """Pure Python NMS baseline."""
    scores = dets[:, 8]
    order = scores.argsort()[::-1]
    keep = []
    while order.size > 0:
        i = order[0]
        keep.append(i)
        anchor = (np.array(dets[i, 0:8]).reshape((4, 2))).astype(np.float32)
        size1 = cv2.contourArea(anchor)
        others = np.array(dets[order[1:], :])
        ovr = np.zeros((others.shape[0]))
        for j in range(others.shape[0]):
            proposal = (others[j, 0:8].reshape((4, 2))).astype(np.float32)
            size2 = cv2.contourArea(proposal)
            inter = cv2.intersectConvexConvex(anchor, proposal)
            inter_size = inter[0]
            iou = inter_size / (size1 + size2 - inter_size)
            ovr[j] = iou

        inds = np.where(ovr <= thresh)[0]
        order = order[inds + 1]
    return keep
Exemple #12
0
    def _intersection_rate(self, s1, s2):
        area, _intersection = cv.intersectConvexConvex(np.array(s1),
                                                       np.array(s2))

        return area / (cv.contourArea(np.array(s1)) +
                       cv.contourArea(np.array(s2)) - area)
def findBillsA(img, coins):
    surArea = img.shape[0] * img.shape[1]
    contours = []

    # do the laplacian filtering
    kernel = np.array([[1, 1, 1], [1, -8, 1], [1, 1, 1]], dtype=np.float32)
    imgLaplacian = cv2.filter2D(img, cv2.CV_32F, kernel)
    sharp = np.uint8(img)
    imgResult = sharp - imgLaplacian

    # convert back to 8bits gray scale
    imgLaplacian = np.clip(imgLaplacian, 0, 255)
    imgLaplacian = np.uint8(imgLaplacian)
    imgResult = np.clip(imgResult, 0, 255)
    imgResult = imgResult.astype('uint8')

    #contrast and brightness
    gray = cv2.cvtColor(imgResult, cv2.COLOR_BGR2GRAY)
    if np.mean(gray) > 150:
        imgResult = apply_brightness_contrast(imgResult, 0, 127)
    elif 120 < np.mean(gray) < 150:
        imgResult = apply_brightness_contrast(imgResult, -70, 127)
    else:
        imgResult = apply_brightness_contrast(imgResult, 80, 127)

    #transform image
    gray = cv2.cvtColor(imgResult, cv2.COLOR_BGR2GRAY)
    if np.mean(gray) > 180:
        gray = 255 - gray
    _, binary = cv2.threshold(gray, 0, 255,
                              cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    kernel1 = np.array([[0, 0, 1, 0, 0], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1],
                        [1, 1, 1, 1, 1], [0, 0, 1, 0, 0]],
                       dtype=np.uint8)
    kernel2 = np.array(
        [[0, 1, 1, 0], [1, 1, 1, 1], [1, 1, 1, 1], [0, 1, 1, 0]],
        dtype=np.uint8)
    kernel3 = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]], dtype=np.uint8)
    binary = cv2.dilate(binary, kernel3, iterations=2)
    _, binary = cv2.threshold(binary, 1, 128, cv2.THRESH_BINARY_INV)
    binary = cv2.erode(binary, kernel3, iterations=1)
    if np.mean(binary) > 64:
        binary = 128 - binary

    #watershed
    dist = cv2.distanceTransform(binary, cv2.DIST_L2, 3)
    cv2.normalize(dist, dist, 0, 1.0, cv2.NORM_MINMAX)

    _, dist = cv2.threshold(dist, 0.05, 1.0, cv2.THRESH_BINARY)

    kernel2 = np.ones((3, 3), dtype=np.uint8)
    dist = cv2.dilate(dist, kernel2)
    dist_8u = dist.astype('uint8')

    _, cont, _ = cv2.findContours(dist_8u, cv2.RETR_EXTERNAL,
                                  cv2.CHAIN_APPROX_SIMPLE)
    cont = sorted(cont, key=cv2.contourArea, reverse=True)[:10]

    for cnt in cont:
        # approximate the contour
        # These methods are used to approximate the polygonal curves of a contour.
        # In order to approximate a contour, you need to supply your level of approximation precision.
        # In this case, we use 2% of the perimeter of the contour. The precision is an important value to consider.
        # If you intend on applying this code to your own projects, you’ll likely have to play around with the precision value.
        peri = cv2.arcLength(cnt, True)
        approx = cv2.approxPolyDP(cnt, 0.02 * peri, True)
        area = cv2.contourArea(cnt)
        # if our approximated contour has four points, then
        # we can assume that we have found our screen
        if 7 > len(approx) > 3 and 0.05 * surArea < area < 0.30 * surArea:
            peri2 = 6 * np.sqrt(area * 1.25 / 2)
            if 1.15 > peri / peri2 > 0.85:
                contours.append(approx)
    #print("findBillsA found: ", len(contours))
    coins2 = []
    if coins is not None:
        for c in coins:
            inside = False
            for cnt in contours:
                area, intersection = cv2.intersectConvexConvex(c, cnt)
                if area > 0:
                    inside = True
            if not inside:
                coins2.append(c)

    return contours, coins2
def findBillsArtur(img, coins):
    surArea = img.shape[0] * img.shape[1]
    contours = []

    blur = cv2.GaussianBlur(img, (15, 15), 0)
    #cv2.imshow('Blur',blur)
    contrast = apply_brightness_contrast(blur, 0, 85)
    #cv2.imshow('Contrast',contrast)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    if np.mean(gray) > 127:
        contrast = 255 - contrast

    kernel1 = np.array([[-1, -1, -1], [-1, 30, -1], [-1, -1, -1]])
    sharp = cv2.filter2D(contrast, -1, kernel1)
    #cv2.imshow('Sharp',sharp)
    gray = cv2.cvtColor(sharp, cv2.COLOR_BGR2GRAY)
    _, binary = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY)
    #cv2.imshow('Binary',binary)

    if np.mean(binary) > 127:
        binary = 255 - binary
    #cv2.imshow('Binary2',binary)

    cont1 = []
    _, cont, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL,
                                  cv2.CHAIN_APPROX_SIMPLE)
    cont = sorted(cont, key=cv2.contourArea, reverse=True)[:10]

    for cnt in cont:
        peri = cv2.arcLength(cnt, True)
        approx = cv2.approxPolyDP(cnt, 0.02 * peri, True)
        area = cv2.contourArea(cnt)

        if 7 > len(approx) > 3 and 0.03 * surArea < area < 0.30 * surArea:
            peri2 = 6 * np.sqrt(area * 1.25 / 2)
            if 1.15 > peri / peri2 > 0.85:
                cont1.append(approx)

    coins2 = []
    if coins is not None:
        for c in coins:
            inside = False
            for cnt in cont1:
                area, intersection = cv2.intersectConvexConvex(c, cnt)
                if area > 0:
                    inside = True
            if not inside:
                cv2.drawContours(binary, [c], 0, (0, 0, 0), 2)
                coins2.append(c)

    kernel2 = np.ones((5, 5), dtype=np.uint8)
    kernel3 = np.ones((8, 8), dtype=np.uint8)
    binary = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel3)
    binary = cv2.dilate(binary, kernel2, iterations=6)
    binary = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel3)
    binary = cv2.erode(binary, kernel2, iterations=3)

    if np.mean(binary) > 150:
        binary = 255 - binary

    cont2 = []
    _, cont, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL,
                                  cv2.CHAIN_APPROX_SIMPLE)
    cont = sorted(cont, key=cv2.contourArea, reverse=True)[:10]

    for cnt in cont:
        peri = cv2.arcLength(cnt, True)
        approx = cv2.approxPolyDP(cnt, 0.02 * peri, True)
        area = cv2.contourArea(cnt)

        if 7 > len(approx) > 3 and 0.05 * surArea < area < 0.30 * surArea:
            peri2 = 6 * np.sqrt(area * 1.25 / 2)
            if 1.15 > peri / peri2 > 0.85:
                cont2.append(approx)

    contours = compareContoursArtur(cont1, cont2)

    #cv2.imshow('Binary2',binary)
    #print("findBillsArtur found: ", len(contours))
    return contours, coins2
Exemple #15
0
def intersectionRate(s1, s2):
    area, _intersection = cv2.intersectConvexConvex(np.array(s1), np.array(s2))
    return 2 * area / (cv2.contourArea(np.array(s1)) + cv2.contourArea(np.array(s2)))
Exemple #16
0
def convContoursIntersectiponRate(ps1, ps2):
    area, _ = cv2.intersectConvexConvex(np.array(ps1), np.array(ps2))
    return 2.0 * area / (cv2.contourArea(ps1) + cv2.contourArea(ps2))
Exemple #17
0
def intersectionRate(s1, s2):
    area, _intersection = cv2.intersectConvexConvex(np.array(s1), np.array(s2))
    return 2 * area / (cv2.contourArea(np.array(s1)) +
                       cv2.contourArea(np.array(s2)))