Esempio n. 1
0
def show_contour_masking(image, image_gray):
    contours = get_contours(image_gray, cv2.RETR_EXTERNAL)
    for idx, contour in enumerate(contours):
        mask = np.zeros(image_gray.shape, dtype="uint8")
        cv2.drawContours(mask, [contour], -1, (255), -1)
        cv2.imshow("Original", image)
        cv2.imshow("Mask", mask)
        cv2.imshow(f"Masked contours: {idx}",
                   cv2.bitwise_and(image, image, mask=mask))
        wait_and_destroy_all_windows()
Esempio n. 2
0
def describe_shapes(bgr_image):
    shape_features = []
    gray = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(gray, (13, 13), 0)
    thresholded = cv2.threshold(blurred, 50, 255, cv2.THRESH_BINARY)[1]
    thresholded = cv2.dilate(thresholded, None, iterations=4)
    thresholded = cv2.erode(thresholded, None, iterations=2)
    cnts = get_contours(thresholded, cv2.RETR_EXTERNAL)
    for cnt in cnts:
        mask = np.zeros(image.shape[:2], dtype="uint8")
        cv2.drawContours(mask, [cnt], -1, 255, -1)
        x, y, w, h = cv2.boundingRect(cnt)
        roi = mask[y:y + h, x:x + w]
        moments = mahotas.features.zernike_moments(
            roi, cv2.minEnclosingCircle(cnt)[1], degree=8)
        shape_features.append(moments)
    return cnts, shape_features
Esempio n. 3
0
from contourutils import get_contours


def get_args():
    ap = ArgumentParser()
    ap.add_argument("-d",
                    "--dataset",
                    required=True,
                    help="Path to the dataset directory")
    return vars(ap.parse_args())


args = get_args()
image_paths = sorted(glob.glob(args["dataset"] + "/*.jpg"))
data = []
for image_path in image_paths:
    image = cv2.imread(image_path)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    thresh = cv2.threshold(gray, 5, 255, cv2.THRESH_BINARY)[1]
    cnts = get_contours(thresh, cv2.RETR_EXTERNAL)
    biggest_cnt = max(cnts, key=cv2.contourArea)
    x, y, w, h = cv2.boundingRect(biggest_cnt)
    roi = cv2.resize(thresh[y:y + h, x:x + w], (50, 50))
    moments = cv2.HuMoments(cv2.moments(roi)).flatten()
    data.append(moments)
distances = pairwise_distances(data).sum(axis=1)
i = np.argmax(distances)
image = cv2.imread(image_paths[i])
print(f"Found square: {image_paths[i]}")
cv2.imshow("Outlier", image)
cv2.waitKey(0)
Esempio n. 4
0
import cv2
from contourutils import get_contours

image = cv2.imread("../../assets/more_shapes_example.png")
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cnts = get_contours(image, cv2.RETR_EXTERNAL)
for idx, cnt in enumerate(cnts):
    x, y, w, h = cv2.boundingRect(cnt)
    roi = image[y:y + h, x:x + w]
    hu_moments = cv2.HuMoments(cv2.moments(roi)).flatten()
    print(f"Moments: {hu_moments}")
    cv2.imshow(f"ROI #{idx+1}", roi)

hu_moments = cv2.HuMoments(cv2.moments(image)).flatten()
print(f"[IMAGE] Moments: {hu_moments}")
cv2.waitKey(0)
Esempio n. 5
0
def show_external_contours(image, image_gray):
    clone = image.copy()
    contours = get_contours(image_gray, cv2.RETR_EXTERNAL)
    cv2.drawContours(clone, contours, -1, (0, 255, 0), 2)
    cv2.imshow("External contours", clone)
    wait_and_destroy_all_windows()
Esempio n. 6
0
        cv2.imshow(f"Contour {index}", clone)
    wait_and_destroy_all_windows()


def show_external_contours(image, image_gray):
    clone = image.copy()
    contours = get_contours(image_gray, cv2.RETR_EXTERNAL)
    cv2.drawContours(clone, contours, -1, (0, 255, 0), 2)
    cv2.imshow("External contours", clone)
    wait_and_destroy_all_windows()


def show_contour_masking(image, image_gray):
    contours = get_contours(image_gray, cv2.RETR_EXTERNAL)
    for idx, contour in enumerate(contours):
        mask = np.zeros(image_gray.shape, dtype="uint8")
        cv2.drawContours(mask, [contour], -1, (255), -1)
        cv2.imshow("Original", image)
        cv2.imshow("Mask", mask)
        cv2.imshow(f"Masked contours: {idx}",
                   cv2.bitwise_and(image, image, mask=mask))
        wait_and_destroy_all_windows()


image, image_gray = image_with_gray_arg()
contours = get_contours(image_gray, cv2.RETR_LIST)
show_all_contours(image, contours)
show_each_contour(image, contours)
show_external_contours(image, image_gray)
show_contour_masking(image, image_gray)
Esempio n. 7
0
import cv2
from imageutils import image_with_gray_arg
from imageutils import Color
from contourutils import get_contours


image, image_gray = image_with_gray_arg()
contours = get_contours(image_gray, cv2.RETR_EXTERNAL)
for contour in contours:
    perimeter = cv2.arcLength(contour, True)
    approx = cv2.approxPolyDP(contour, 0.01 * perimeter, True)
    if len(approx) == 4: # If there are 4 vertices approximated, then its a rect
        cv2.drawContours(image, [contour], -1, Color.GREEN.value, 2)
        x, y, w, h = cv2.boundingRect(approx)
        cv2.putText(
            image, "Rectangle", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
            Color.GREEN.value, 2)
cv2.imshow("Rectangles", image)
cv2.waitKey(0)
Esempio n. 8
0
import cv2
from imageutils import Color
from contourutils import get_contours


def get_biggest_4pt_contour_approximation(contours):
    contours = sorted(contours, key=cv2.contourArea, reverse=True)[:7]
    for contour in contours:
        perimeter = cv2.arcLength(contour, True)
        approx = cv2.approxPolyDP(contour, 0.01 * perimeter, True)
        print(f"Original: {len(contour)}, approx: {len(approx)}")
        if len(approx) == 4:
            return approx


image = cv2.imread("../../assets/contours_receipt_original.jpg")
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(image_gray, (5, 5), 0)
edge_map = cv2.Canny(blurred, 75, 200)
contours = get_contours(edge_map.copy(), cv2.RETR_EXTERNAL)
contour = get_biggest_4pt_contour_approximation(contours)
contour_pts = contour.reshape(4, 2)
print(f"contour: {contour} contour_pts: {contour_pts}")
cv2.drawContours(image, [contour], -1, Color.GREEN.value, 2)
cv2.imshow("Edge map", edge_map)
cv2.imshow("Output", image)
cv2.waitKey(0)
Esempio n. 9
0
    c_x = int(moments["m10"] / moments["m00"])
    c_y = int(moments["m01"] / moments["m00"])
    cv2.putText(image, f"#{i + 1}", (c_x - 20, c_y), cv2.FONT_HERSHEY_SIMPLEX,
                1.0, (255, 255, 255), 2)
    return image


def get_edges(image):
    accum_edged = np.zeros(image.shape[:2], dtype="uint8")
    for channel in cv2.split(image):
        channel = cv2.medianBlur(channel, 11)
        edged = cv2.Canny(channel, 50, 200)
        accum_edged = cv2.bitwise_or(accum_edged, edged)
    return accum_edged


args = args(argparse.ArgumentParser())
image = cv2.imread(args["image"])
edge_map = get_edges(image)
contours = get_contours(edge_map, cv2.RETR_EXTERNAL)
contours = sorted(contours, key=cv2.contourArea, reverse=True)[:5]
unsorted_contours = image.copy()
for (i, contour) in enumerate(contours):
    unsorted_contours = draw_contour(unsorted_contours, contour, i)
contours, bboxes = sort_contours(contours, method=args["method"])
for (i, contour) in enumerate(contours):
    draw_contour(image, contour, i)
cv2.imshow("Edge map", edge_map)
cv2.imshow("Unsorted contours", unsorted_contours)
cv2.imshow("Sorted contours", image)
cv2.waitKey(0)