Beispiel #1
0
def main(argv):

    if (len(argv) < 1):
        print("not enough parameters\n")
        print("usage PS1-1.py <path to image>\n")
        return -1

    img = cv.imread(argv[0], cv.IMREAD_COLOR)
    if img is None:
        print("Error opening image\n")
        return -1

    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    gray_smooth = cv.GaussianBlur(gray, (5, 5), 4, 4)

    edges = cv.Canny(gray_smooth, 80, 180)
    ps.display_image("edges", edges)
    ps.save_image("ps1-5-a", edges)
    x = extract_edges(edges)
    rmin = int(input("enter the minimum raidus\n"))
    rmax = int(input("enter the max radius\n"))
    circles_in_image = hough_space_circle(x, rmin, rmax)
    #centers = detecting_circle_using_chord(x)
    for j in circles_in_image:
        if (j[2] >= rmin and j[2] < rmax):
            cv.circle(img, (j[0], j[1]), j[2], (0, 255, 0))
    ps.display_image("cirlces in image", img)
    ps.save_image("ps1-5-b", img)
    return 0
Beispiel #2
0
def main(argv):
    if (len(argv) < 1):
        print("not enough parameters\n")
        print("usage PS1-1.py <path to image>\n")
        return -1

    img = cv.imread(argv[0], cv.IMREAD_COLOR)

    if img is None:
        print("Error opening image\n")
        return -1

    ps.display_image("Image", img)
    monochrome_image = img[:, :, 1]
    ps.display_image("monochrome image", monochrome_image)

    monochrome_smooth_image = cv.GaussianBlur(monochrome_image, (5, 5), 4, 4)
    ps.display_image("smoothened  monochrome image", monochrome_smooth_image)
    ps.save_image("ps1-4-a", monochrome_smooth_image)

    edges_smooth_monochrome = cv.Canny(monochrome_smooth_image, 100, 210)
    ps.display_image("edges in the smoothed monochrome image",
                     edges_smooth_monochrome)
    ps.save_image("ps1-4-b", edges_smooth_monochrome)

    hough_space = ps.hough_transform(edges_smooth_monochrome,
                                     monochrome_smooth_image)
    ps.plot_3d_graphs(hough_space, "hough_space_ps1-4-c")
    #measuring time to detect lines in the image

    img1 = ps.detect_line_in_image(hough_space, monochrome_smooth_image, 0.5)

    ps.display_image("Lines in the image are", img1)

    ps.save_image("ps1-4-c-1", img1)
def make_template(y, x, kernel, left_image):
    template = np.zeros((kernel, kernel))
    kernel_half = int(kernel / 2)

    for i in range(-kernel_half, kernel_half):
        for j in range(-kernel_half, kernel_half):
            template[y + i, x + j] = left_image[y + i, x + j]
    ps.display_image("Template", template)
Beispiel #4
0
def draw_line(description, angle, corners, img):
    for i in range(0, len(corners)):
        x1 = int(corners[i][1])
        y1 = int(corners[i][0])

        x2 = int(x1 + 10 * np.cos(angle[y1, x1]))
        y2 = int(y1 + 10 * np.sin(angle[y1, x1]))
        cv.line(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
    ps.display_image(description, img)
def ransac(corner1, corner2, matching, img1, img2):
    img3 = np.concatenate((img1, img2), axis=1)
    best = 0
    best_offset_x = -1
    best_offset_y = -1
    gray1 = cv.cvtColor(img1, cv.COLOR_BGR2GRAY)
    gray2 = cv.cvtColor(img2, cv.COLOR_BGR2GRAY)

    # no of iterations is 1000
    temp_img1 = np.asarray(gray1)
    temp_img2 = np.asarray(gray2)
    h, w = temp_img1.shape[0:2]
    for i in range(200):
        print("iteration {}".format(i))
        inliers = 0
        index = np.random.randint(0, len(matching))
        x1 = int(corner1[index][1])
        y1 = int(corner1[index][0])
        k1 = int(matching[index])
        x2 = int(corner2[k1][1])
        y2 = int(corner2[k1][0])
        offset_x = x2 - x1
        offset_y = y2 - y1

        for j in range(h):
            for k in range(w):
                m = j + offset_y
                n = k + offset_x
                if (m < h and m >= 0 and n >= 0 and n < w):
                    ssd = (temp_img1[j][k] - temp_img2[m][n])**2
                    if (ssd < 25):
                        inliers += 1

        if (inliers > best):
            best = inliers
            best_offset_x = offset_x
            best_offset_y = offset_y
    print(best, best_offset_x, best_offset_y)
    for i in range(30):
        x1 = int(corner1[i][1])
        y1 = int(corner1[i][0])
        x2 = int(x1 + best_offset_x)
        y2 = int(y1 + best_offset_y)
        if (x2 < w and y2 < h and y2 >= 0 and x2 >= 0):
            color1 = (list(np.random.choice(range(256), size=3)))
            color = [int(color1[0]), int(color1[1]), int(color1[2])]
            cv.circle(img3, (x1, y1), 2, color, 2)
            cv.circle(img3, (x2 + w, y2), 2, color, 2)
            cv.line(img3, (x1, y1), (x2 + w, y2), color, 1)

    ps.display_image("ransac image", img3)
def draw_keypoints(corner1, corner2, matching, img3, w1):

    for i in range(35):
        index = np.random.randint(0, len(corner1))
        x1 = int(corner1[index][1])
        y1 = int(corner1[index][0])
        k = int(matching[index])
        x2 = int(corner2[k][1] + w1)
        y2 = int(corner2[k][0])
        #breakpoint()
        color1 = (list(np.random.choice(range(256), size=3)))
        color = [int(color1[0]), int(color1[1]), int(color1[2])]
        cv.circle(img3, (x1, y1), 2, color, 2)
        cv.circle(img3, (x2, y2), 2, color, 2)
        cv.line(img3, (x1, y1), (x2, y2), color, 2)
    ps.display_image("matched", img3)
def main(argv):

    if (len(argv) < 1):
        print("not enough parameters\n")
        print("usage PS1-1.py <path to image>\n")
        return -1

    img = cv.imread(argv[0], cv.IMREAD_COLOR)
    if img is None:
        print("Error opening image\n")
        return -1

    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    gray_smooth = cv.GaussianBlur(gray, (5, 5), 4, 4)

    edges = cv.Canny(gray, 100, 200)
    ps.display_image("edges", edges)
    ps.save_image("ps1-1", edges)
def matching_descriptors1(kp1, des1, kp2, des2):
    bf = cv.BFMatcher()
    matches = bf.knnMatch(des1, des2, k=2)
    # Apply ratio test
    good = []
    for m, n in matches:
        if m.distance < 0.8 * n.distance:
            good.append([m])

    # cv.drawMatchesKnn expects list of lists as matches.
    #breakpoint()
    img3 = cv.drawMatchesKnn(img1,
                             kp1,
                             img2,
                             kp2,
                             good,
                             None,
                             flags=cv.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
    ps.display_image("matching", img3)
def main(argv):

    if (len(argv) < 1):
        print("not enough parameters\n")
        print("usage PS1-1.py <path to image>\n")
        return -1

    img = cv.imread(argv[0], cv.IMREAD_COLOR)
    if img is None:
        print("Error opening image\n")
        return -1
    corners = harris_Corners(img, 5)
    key_points = non_maximum_supression(corners, img)
    for m in key_points:
        i = int(m[0])
        j = int(m[1])

        cv.circle(img, (j, i), 2, [0, 0, 255], 2)
    ps.display_image("after nms ", img)
Beispiel #10
0
def main(argv):

    if (len(argv) < 1):
        print("not enough parameters\n")
        print("usage PS1-1.py <path to image>\n")
        return -1

    img = cv.imread(argv[0], cv.IMREAD_COLOR)
    if img is None:
        print("Error opening image\n")
        return -1
    smooth = cv.GaussianBlur(img, (3, 3), 0)
    gray = cv.cvtColor(smooth, cv.COLOR_BGR2GRAY)
    grad_x = cv.Sobel(gray, -1, 1, 0, ksize=3)

    grad_y = cv.Sobel(gray, -1, 0, 1, ksize=3)
    img3 = np.concatenate((grad_x, grad_y), axis=1)
    img3[img3 > 0] = 255
    img3[img3 == 0] = 128
    img3[img3 < 0] = 0
    ps.display_image("concatenated image", img3)
Beispiel #11
0
def main(argv):

    if (len(argv) < 1):
        print("not enough parameters\n")
        print("usage PS1-1.py <path to image>\n")
        return -1

    img = cv.imread(argv[0], cv.IMREAD_COLOR)
    if img is None:
        print("Error opening image\n")
        return -1

    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    gray_smooth = cv.GaussianBlur(gray, (5, 5), 4, 4)
    edges_original = cv.Canny(gray, 140, 210)
    ps.display_image("edges in the original image", edges_original)
    ps.save_image("ps1-3-a", gray_smooth)
    ps.save_image("ps1-3-b-1", edges_original)

    edges_smoothed = cv.Canny(gray_smooth, 140, 210)
    ps.display_image("edges in the smoothened image", edges_smoothed)
    ps.save_image("ps1-3-b-2", edges_smoothed)

    hough_space = ps.hough_transform(edges_smoothed, gray_smooth)

    #display_image("Displaying 3D plot of the hough space",hough_space)
    ps.plot_3d_graphs(hough_space, "hough_space_ps1-3")
    smooth = ps.detect_line_in_image(hough_space, img, 0.5)
    ps.display_image("PS3", img)
    ps.save_image("ps1-3-c", img)
def main(argv):
    if(len(argv) < 1):
        print("not enough parameters\n")
        print("usage PS1-1.py <path to image>\n")
        return -1


    img = cv.imread(argv[0],cv.IMREAD_COLOR)

    if img is None:
        print("Error opening image\n")
        return -1
    ps.display_image("Image", img)
    print(type(img))
    print(img.size,img.ndim, img.shape)
    smooth_image = cv.GaussianBlur(img,(5,5),4,4)
    gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
    ps.display_image("gray image ",gray)
    gray_smooth_image = cv.GaussianBlur(gray,(5,5),4,4)
    ps.display_image(" gray smoothened  image", gray_smooth_image)


    lower_threshold = int(input("enter the lower threshold value for canny\n"))
    upper_threshold = int(input("enter the upper threshold value for canny\n"))
    edges_smooth = cv.Canny(gray_smooth_image,lower_threshold,upper_threshold)
    ps.display_image("edges in the smoothed monochrome image", edges_smooth)
    e1 = cv.getTickCount()
    hough_space = ps.hough_transform(edges_smooth, gray_smooth_image)
    #ps.plot_3d_graphs(hough_space)
    #measuring time to detect lines in the image
    threshold = float(input("enter the hough accululator threshold value\n"))

    #img1 = ps.detect_line_in_image(edges_smooth,hough_space,smooth_image,threshold)
    img1 = ps.detect_line_in_image(hough_space,smooth_image,threshold)
    e2 = cv.getTickCount()
    time_taken = (e2 -e1)/cv.getTickFrequency()
    print("The time taken to calculate the lines in the image is {} seconds\n".format(time_taken))
    ps.display_image("Lines in the image are",img1)
    ps.save_image("ps1-6-a", img1)
Beispiel #13
0
def main(argv):

    if (len(argv) < 1):
        print("not enough parameters\n")
        print("usage PS1-1.py <path to image>\n")
        return -1

    img = cv.imread(argv[0], cv.IMREAD_COLOR)
    if img is None:
        print("Error opening image\n")
        return -1

    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    gray_smooth = cv.GaussianBlur(gray, (5, 5), 4, 4)

    edges = cv.Canny(gray, 100, 200)
    ps.display_image("edges", edges)
    hough_space = ps.hough_transform(edges, gray)
    ps.plot_3d_graphs(hough_space)
    img = ps.detect_line_in_image(hough_space, img, 0.6)

    ps.display_image("lines in the image are", img)
    ps.save_image("ps1-2", img)
Beispiel #14
0
                                                                  r - rmin]
    hough_space = threshold_image_circle(hough_accumulator,
                                         maximum_accumulator_value)
    result = detect_circles_in_hough_space(hough_space, 0.5, offset_x,
                                           offset_y, rmin)
    return result


def extract_edges(edge_image):
    return [(x, y) for x in range(edge_image.shape[0])
            for y in range(edge_image.shape[1]) if edge_image[x, y] == 255]


img = cv.imread('../image/ps1-input1.jpg', cv.IMREAD_COLOR)
monochrome_image = img[:, :, 1]
ps.display_image("original image", img)
ps.display_image("monochrome image", monochrome_image)

monochrome_smooth_image = cv.GaussianBlur(monochrome_image, (5, 5), 4, 4)
ps.display_image("smoothened  monochrome image", monochrome_smooth_image)

edges_smooth_monochrome = cv.Canny(monochrome_smooth_image, 100, 210)
ps.display_image("edges in the smoothed monochrome image",
                 edges_smooth_monochrome)
x = extract_edges(edges_smooth_monochrome)
circles_in_image = hough_space_circle(x, 1, 10)
#centers = detecting_circle_using_chord(x)
for j in circles_in_image:
    cv.circle(img, (j[0], j[1]), j[2], (0, 255, 0))
ps.display_image("cirlces in image", img)
Beispiel #15
0
def ransac_affine(corner1, corner2, matching, img1, img2):
    breakpoint()
    best = 0
    img3 = np.concatenate((img1, img2), axis=1)
    gray1 = cv.cvtColor(img1, cv.COLOR_BGR2GRAY)
    gray2 = cv.cvtColor(img2, cv.COLOR_BGR2GRAY)
    # no of iterations is 1000
    temp_img1 = np.asarray(gray1)
    temp_img2 = np.asarray(gray2)
    h, w = temp_img1.shape
    for i in range(50):
        print("iteration {}".format(i))
        inliers = 0
        index1 = np.random.randint(0, len(matching))
        index2 = np.random.randint(0, len(matching))
        if (index1 == index2):
            continue
        A = np.zeros((4, 4))
        B = np.zeros((4, 1))
        u1 = corner1[index1][1]
        v1 = corner1[index1][0]
        u2 = corner1[index2][1]
        v2 = corner1[index2][0]
        n1 = int(matching[index1])
        n2 = int(matching[index2])
        x1 = corner2[n1][1]
        y1 = corner2[n1][0]
        x2 = corner2[n2][1]
        y2 = corner2[n2][0]

        A[0] = [u1, -v1, 1, 0]
        A[1] = [v1, u1, 0, 1]
        A[2] = [u2, -v2, 1, 0]
        A[3] = [v2, u2, 0, 1]
        C = np.array([x1, y1, x2, y2])
        B = np.transpose(C)
        X = np.linalg.solve(A, B)
        H = np.ones((3, 2))
        H = np.array([[X[0], -X[1], X[2]], [X[1], X[0], X[3]]])

        for j in range(h):
            for k in range(w):
                p1 = [k, j, 1]
                p1 = np.transpose(p1)
                p2 = np.matmul(H, p1)
                m = int(p2[1])
                n = int(p2[0])
                if (m >= 0 and m < h and n >= 0 and n < w):
                    ssd = (temp_img1[p1[1], p1[0]] - temp_img2[m, n])**2
                    if (ssd < 25):
                        inliers += 1

        if (inliers > best):
            best = inliers
            best_homography = np.asarray(H)
    print(best_homography)
    for i in range(50):
        x1 = int(corner1[i][1])
        y1 = int(corner1[i][0])
        p1 = [x1, y1, 1]
        p2 = np.matmul(best_homography, np.transpose(p1))
        x2 = int(p2[0])
        y2 = int(p2[1])
        if (x2 < w and y2 < h and y2 >= 0 and x2 >= 0):
            color1 = (list(np.random.choice(range(256), size=3)))
            color = [int(color1[0]), int(color1[1]), int(color1[2])]
            cv.circle(img3, (x1, y1), 2, color, 2)
            cv.circle(img3, (x2 + w, y2), 2, color, 2)
            cv.line(img3, (x1, y1), (x2 + w, y2), color, 1)

    ps.display_image("ransac image", img3)