def find_circles(edge_img, radius_range=[1, 2], threshold=100, nhood_size=10):
    n = radius_range[1] - radius_range[0]
    H_size = (n, ) + edge_img.shape
    H = np.zeros(H_size, dtype=np.uint)
    centers = ()
    radii = np.arange(radius_range[0], radius_range[1])
    valid_radii = np.array([], dtype=np.uint)
    num_circles = 0
    for i in range(len(radii)):
        H[i] = hough_circles_acc(edge_img, radii[i])
        peaks = hough_peaks(H[i],
                            numpeaks=10,
                            threshold=threshold,
                            nhood_size=nhood_size)
        if peaks.shape[0]:
            valid_radii = np.append(valid_radii, radii[i])
            centers = centers + (peaks, )
            for peak in peaks:
                cv2.circle(edge_img, tuple(peak[::-1]), radii[i] + 1,
                           (0, 0, 0), -1)
        #  cv2.imshow('image', edge_img); cv2.waitKey(0); cv2.destroyAllWindows()
        num_circles += peaks.shape[0]
        print('Progress: %d%% - Circles: %d\033[F\r' %
              (100 * i / len(radii), num_circles))
    print('Circles detected: %d          ' % (num_circles))
    centers = np.array(centers)
    return centers, valid_radii.astype(np.uint)
Example #2
0
def hough_peaks_circle(img, radius, threshold, nhood_size=5):
    acc = hough_circles_acc(img, radius)
    peaks = hough_peaks(acc,
                        numpeaks=10,
                        threshold=threshold,
                        nhood_size=nhood_size)
    print("半径:", radius, ",peaks:", peaks)
    peaks = peaks.tolist()  # 将NumPy形式转换为list
    for each in peaks:  # 为每一个圆心坐标加上半径
        each.append(radius)
    return peaks
Example #3
0
def ps1_3():
    start_time = time.time()
    #  3a: smooth the noisy image using gaussian blurring
    noise_img = cv2.imread("../input/ps1-input0-noise.png", 0)
    gaussian_noise_img = cv2.GaussianBlur(noise_img, (13, 11), 0, 0)
    plt.subplot(231)
    plt.imshow(gaussian_noise_img, cmap="Greys_r")
    cv2.imwrite("../output/ps1-3-a-1.png", gaussian_noise_img)
    plt.title("3a1")

    #  3b: perform edge detection on both images using Canny
    edge_noise_img = cv2.Canny(noise_img, 150, 150)
    edge_gaussian_noise_img = cv2.Canny(gaussian_noise_img, 70, 70)
    plt.subplot(232)
    plt.imshow(edge_noise_img, cmap="Greys_r")
    cv2.imwrite("../output/ps1-3-b-1.png", edge_noise_img)
    plt.title("3b1")
    plt.subplot(233)
    plt.imshow(edge_gaussian_noise_img, cmap="Greys_r")
    cv2.imwrite("../output/ps1-3-b-2.png", edge_gaussian_noise_img)
    plt.title("3b2")

    #  3c: apply hough line detection to the smoothed image
    plt.subplot(234)
    H, thetas, rhos = hough_lines_acc.hough_lines_acc(edge_gaussian_noise_img)
    peaks = hough_peaks(H, 10, threshold=75)
    print("计算出的peaks值", peaks)
    peaks_x, peaks_y = H.shape[0], H.shape[1]
    peaks_mask = np.zeros((peaks_x, peaks_y))
    for i in range(len(peaks)):
        y, x = peaks[i]
        peaks_mask = cv2.circle(peaks_mask, (x, y), 5, 125, 5)
    plt.imshow(peaks_mask, cmap="Greys_r")
    cv2.imwrite("../output/ps1-3-c-1.png", peaks_mask)
    plt.title("3c1")
    # c)原图像画线
    plt.subplot(235)
    # 原始图像的BGR图像
    ori_image_BGR = cv2.imread("../input/ps1-input0.png", 1)
    green_lined = hough_lines_draw(ori_image_BGR, peaks=peaks)
    plt.imshow(green_lined, cmap="Greys_r")
    cv2.imwrite("../output/ps1-3-c-2.png", green_lined)
    plt.title("3c2")
    plt.subplot(236)
    # 在mask上画线
    mask = np.zeros((1000, 1000, 3))
    green_lined_mask = hough_lines_draw(mask, peaks=peaks)
    plt.imshow(green_lined_mask, cmap="Greys_r")
    plt.title("green_lined_mask")
    # save the produced images
    print('3) Time elapsed: %.3f s' % (time.time() - start_time))
    plt.savefig("../output/myOutPut/ps1-3.png")
    plt.show()
Example #4
0
def ps1_6():
    start_time = time.time()
    img = cv2.imread('input/ps1-input2.png', cv2.IMREAD_COLOR)
    gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    smoothed_img = cv2.GaussianBlur(gray_img, (7, 7), 5)
    edge_img = cv2.Canny(smoothed_img, 50, 100)
    H, thetas, rhos = hough_lines_acc(edge_img)
    peaks = hough_peaks(H, numpeaks=10, threshold=120, nhood_size=50)
    hl_img = cv2.cvtColor(smoothed_img, cv2.COLOR_GRAY2BGR)
    hough_lines_draw(hl_img, 'output/ps1-6-a-1.png', peaks, rhos, thetas)
    peaks = filter_lines(peaks, thetas, rhos, 5, 50)
    hl_img2 = cv2.cvtColor(smoothed_img, cv2.COLOR_GRAY2BGR)
    hough_lines_draw(hl_img2, 'output/ps1-6-c-1.png', peaks, rhos, thetas)
    print('6) Time elapsed: %.2f s' % (time.time() - start_time))
Example #5
0
def find_circles(img_edges, radii=[20, 30]):
    rad = np.arange(radii[0], radii[1] + 1, 1)

    peaks = 10
    centers = np.zeros((len(rad) * peaks, 2))
    radius = np.zeros((len(rad) * peaks))
    #print(centers)
    for i in range(len(rad)):
        H = hough_circles_acc(img_edges, rad[i])
        center_temp = hough_peaks(H, peaks)

        #print(center_temp.shape)
        centers[i * peaks:(i + 1) * peaks, :] = center_temp
        radius[i * peaks:(i + 1) * peaks] = np.full(peaks, rad[i])
    return centers, radius
def run():
    img = cv2.imread('../input/ps1-input2.png', cv2.IMREAD_GRAYSCALE)
    img_smoothed = cv2.GaussianBlur(img, (31, 31), 3)
    edges_smoothed = cv2.Canny(img_smoothed, 0, 80)

    #task с - acuumulator array for hough transform
    accumulator, rhos, thetas = hough_lines_acc(edges_smoothed)
    peaks = np.int16(hough_peaks(accumulator, 10, 150))
    H_peaks = cv2.cvtColor(np.uint8(accumulator), cv2.COLOR_GRAY2BGR)
    for peak in peaks:
        cv2.circle(H_peaks, (peak[1], peak[0]), 2, (0, 0, 255))

    peaks = filter_lines(peaks, rhos, thetas, 50, 5)
    #draw lines
    hough_lines_draw(img, '../output/ps1-6-a-1.png', peaks, rhos, thetas)
def run():
    img = cv2.imread('../input/ps1-input0.png', cv2.IMREAD_GRAYSCALE)
    edges = cv2.Canny(img, 100, 200)
    #task a - acuumulator array for hough transform
    accumulator, rhos, thetas = hough_lines_acc(edges)

    cv2.imwrite('../output/ps1-2-a-1.png', accumulator)
    #task b - peaks detection
    peaks = np.uint16(hough_peaks(accumulator, 10))
    print(*peaks, sep='\n')
    H_peaks = cv2.cvtColor(np.uint8(accumulator), cv2.COLOR_GRAY2BGR)
    for peak in peaks:
        cv2.circle(H_peaks, (peak[1], peak[0]), 2, (0, 0, 255))
    cv2.imwrite('../output/ps1-2-b-1.png', H_peaks)
    #task 3 - draw lines
    hough_lines_draw(img, '../output/ps1-2-c-1.png', peaks, rhos, thetas)
Example #8
0
def ps1_2():
    start_time = time.time()
    # load input image and perform edge detection
    img = cv2.imread('input/ps1-input0.png', cv2.IMREAD_GRAYSCALE)
    edge_img = auto_canny(img, 0.5)
    # 2-a: Calculate the hough space of the edge image for line detection
    H, thetas, rhos = hough_lines_acc(edge_img)
    cv2.imwrite('output/ps1-2-a-1.png', H)
    # 2-b: Detect peaks on the hough space of the edge image and highlight them
    peaks = hough_peaks(H, numpeaks=10, threshold=100, nhood_size=50)
    H_peaks = H.copy()
    for peak in peaks:
        cv2.circle(H_peaks, tuple(peak[::-1]), 5, (255, 255, 255), -1)
    # 2-c: Draw the detected lines on the image
    color_img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
    hough_lines_draw(color_img, 'output/ps1-2-c-1.png', peaks, rhos, thetas)
    print('2) Time elapsed: %.2f s' % (time.time() - start_time))
Example #9
0
def find_circles(edges, radius_range, threshold=100, nhood_size=5):
    all_rads = np.arange(radius_range[0], radius_range[1])
    good_rads = []
    centers = []
    npeaks = 0
    for i in range(len(all_rads)):
        H = hough_circles_acc(edges, all_rads[i])
        peaks = hough_peaks(H,
                            npeaks=10,
                            threshold=threshold,
                            nhood_size=nhood_size)
        for peak in peaks:
            centers.append(peak)
            good_rads.append(all_rads[i])
            npeaks += 1
            print('Progress:', i / len(all_rads) * 100, '%')
    print('peaks detected:', npeaks)
    return np.array(centers), np.array(good_rads)
Example #10
0
def ps1_4():
    start_time = time.time()
    #  4a: Load the coin image, smooth it (gaussian blur) and save it
    img = cv2.imread('input/ps1-input1.png', cv2.IMREAD_GRAYSCALE)
    smoothed_img = cv2.GaussianBlur(img, (11,11), 3)
    cv2.imwrite('output/ps1-4-a-1.png', smoothed_img)
    #  4b: apply edge detection using Canny
    edge_img = auto_canny(smoothed_img, 0.8)
    cv2.imwrite('output/ps1-4-b-1.png', edge_img)
    #  4c: apply hough line detection to the smoothed image
    H, thetas, rhos = hough_lines_acc(edge_img)
    peaks = hough_peaks(H, numpeaks=10, threshold=150, nhood_size=20)
    for peak in peaks:
        cv2.circle(H, tuple(peak[::-1]), 15, (255,255,255), -1)
    cv2.imwrite('output/ps1-4-c-1.png', H)
    color_img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
    hough_lines_draw(color_img, 'output/ps1-4-c-2.png', peaks, rhos, thetas)
    print('4) Time elapsed: %.2f s'%(time.time()-start_time))
def find_circles(edge_img, radius_range=None, threshold=140, nhood_size=10):
    if radius_range is None:
        radius_range = [1, 2]
    n = radius_range[1] - radius_range[0]
    H_size = (n, ) + edge_img.shape
    H = np.zeros(H_size, dtype=np.uint)
    centers = ()
    radius = np.arange(radius_range[0], radius_range[1])
    valid_radius = np.array([], dtype=np.uint)
    for i in range(len(radius)):
        H = hough_circles_acc(edge_img, radius[i])
        peaks = hough_peaks(H,
                            num_peaks=10,
                            threshold=threshold,
                            nhood_size=nhood_size)
        if peaks.size:
            valid_radius = np.append(valid_radius, radius[i])
            centers = centers + (peaks, )
    centers = np.array(centers)
    return centers, valid_radius.astype(np.uint)
Example #12
0
def run():
    img = cv2.imread('../input/ps1-input0-noise.png', cv2.IMREAD_GRAYSCALE)
    img_smoothed = cv2.GaussianBlur(img, (31, 31), 3)
    cv2.imwrite('../output/ps1-3-a-1.png', img_smoothed)

    edges_original = cv2.Canny(img, 0, 100)
    cv2.imwrite('../output/ps1-3-b-1.png', edges_original)
    edges_smoothed = cv2.Canny(img_smoothed, 0, 80)
    cv2.imwrite('../output/ps1-3-b-2.png', edges_smoothed)

    #task с - acuumulator array for hough transform
    accumulator, rhos, thetas = hough_lines_acc(edges_smoothed)
    peaks = np.int16(hough_peaks(accumulator, 10, 85))
    print(*peaks, sep='\n')
    H_peaks = cv2.cvtColor(np.uint8(accumulator), cv2.COLOR_GRAY2BGR)
    for peak in peaks:
        cv2.circle(H_peaks, (peak[1], peak[0]), 2, (0, 0, 255))
    cv2.imwrite('../output/ps1-3-c-1.png', H_peaks)

    #draw lines
    hough_lines_draw(img, '../output/ps1-2-c-2.png', peaks, rhos, thetas)
Example #13
0
def ps1_3():
    start_time = time.time()
    noisy_img = cv2.imread('input/ps1-input0-noise.png', cv2.IMREAD_GRAYSCALE)
    #  3a: smooth the noisy image using gaussian blurring
    smoothed_img = cv2.GaussianBlur(noisy_img, (23,)*2, 4.5)
    #  3b: perform edge detection on both images using Canny
    min_val = 20; max_val = 2 * min_val
    noisy_edge_img = cv2.Canny(noisy_img, min_val, max_val)
    edge_img = cv2.Canny(smoothed_img, min_val, max_val)
    #  3c: apply hough line detection to the smoothed image
    H, thetas, rhos = hough_lines_acc(edge_img)
    peaks = hough_peaks(H, numpeaks=20, threshold=50, nhood_size=150)
    for peak in peaks:
        cv2.circle(H, tuple(peak[::-1]), 5, (255,255,255), -1)
    color_img = cv2.cvtColor(noisy_img, cv2.COLOR_GRAY2BGR)
    hough_lines_draw(color_img, 'output/ps1-3-c-2.png', peaks, rhos, thetas)
    #  save the produced images
    cv2.imwrite('output/ps1-3-a-1.png', smoothed_img)
    cv2.imwrite('output/ps1-3-b-1.png', noisy_edge_img)
    cv2.imwrite('output/ps1-3-b-2.png', edge_img)
    cv2.imwrite('output/ps1-3-c-1.png', H)
    print('3) Time elapsed: %.2f s'%(time.time()-start_time))
Example #14
0
def ps1_8():
    start_time = time.time()
    img = cv2.imread('input/ps1-input3.png', cv2.IMREAD_COLOR)
    smoothed_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    smoothed_img = cv2.erode(smoothed_img, np.ones((3, ) * 2, np.uint8), 1)
    smoothed_img = cv2.GaussianBlur(smoothed_img, (3, ) * 2, 2)
    edge_img = cv2.Canny(smoothed_img, 40, 80)

    #  Detect lines
    H, thetas, rhos = hough_lines_acc(edge_img)
    peaks = hough_peaks(H, numpeaks=40, threshold=105, nhood_size=40)
    peaks = filter_lines(peaks, thetas, rhos, 3, 24)
    img_hl = hough_lines_draw(img, 'output/ps1-8-a-1.png', peaks, rhos, thetas)

    #  Detect circles
    centers, radii = find_circles(edge_img, [20, 40],
                                  threshold=110,
                                  nhood_size=50)
    img_circles = img.copy()
    for i in range(len(radii)):
        img_hl = hough_circles_draw(img_hl, 'output/ps1-8-a-1.png', centers[i],
                                    radii[i])
    print('\033[F\r8) Time elapsed: %.2f s' % (time.time() - start_time))
Example #15
0
def ps1_5():
    start_time = time.time()
    # 5a: Load coin image, smooth, detect edges and calculate hough space
    img = cv2.imread('input/ps1-input1.png', cv2.IMREAD_COLOR)
    gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    smoothed_img = cv2.GaussianBlur(gray_img, (11, 11), 3)  # smooth image
    edge_img = auto_canny(smoothed_img, 0.8)
    cv2.imwrite('output/ps1-5-a-1.png', smoothed_img)
    cv2.imwrite('output/ps1-5-a-2.png', edge_img)
    # detect circles with radius = 20 and save image
    H_20 = hough_circles_acc(edge_img, 20)
    peaks = hough_peaks(H_20, numpeaks=10, threshold=140, nhood_size=100)
    img_circles = img.copy()
    img_circles = hough_circles_draw(img_circles, 'output/ps1-5-a-3.png',
                                     peaks, 20)
    # 5b: detect circles in the range [20 50]
    centers, radii = find_circles(edge_img, [20, 50],
                                  threshold=153,
                                  nhood_size=10)
    img_circles = img.copy()
    for i in range(len(radii)):
        img_circles = hough_circles_draw(img_circles, 'output/ps1-5-b-1.png',
                                         centers[i], radii[i])
    print('\033[F\r5) Time elapsed: %.2f s' % (time.time() - start_time))
Example #16
0
img = cv2.imread('input/ps1-input1.png', 0)

blur = cv2.GaussianBlur(img, (7, 7), 2)
cv2.imwrite('output/ps1-5-a-1.png', blur)

edge2 = cv2.Canny(blur, 80, 170)
cv2.imwrite('output/ps1-5-a-2.png', edge2)

radius = 20
H = hough_circles_acc(edge2, radius)
#print(H)

normalizedImg = np.zeros(H.shape)
H_norm = cv2.normalize(H, normalizedImg, 0, 255, cv2.NORM_MINMAX)

centers = hough_peaks(H, 10)

#print(centers)

new_img = hough_draw_circles(img, 'output/ps1-5-a-3.png', centers, radius)

x, y = find_circles(edge2, [20, 30])

print(y[0])
print(y[12])
new_img = hough_draw_circles(img, 'output/ps1-5-a-3.png', x, y)

#cv2.imshow('Hough Image', H_norm)

cv2.imshow('Drawn Image', new_img)
cv2.waitKey()
Example #17
0
#!/usr/bin/env python

import cv2
import numpy as np
from hough_lines_acc import *
from hough_peaks import *
from hough_lines_draw import *

img = cv2.imread('input/ps1-input1.png', 0)
blur = cv2.GaussianBlur(img, (7, 7), 2)

cv2.imwrite('output/ps1-4-a-1.png', blur)
edge2 = cv2.Canny(blur, 80, 170)

cv2.imwrite('output/ps1-4-b-1.png', edge2)

# cv2.imshow('image2', edge2)
# cv2.waitKey()

H, theta, rho = hough_lines_acc(edge2, res=1, thetas=np.arange(-90, 89, 0.6))
cv2.imwrite('output/ps1-4-c-1.png', H)
peaks = hough_peaks(H, 8)
hough_lines_draw(img, 'output/ps1-4-c-2.png', peaks, rho, theta)


Example #18
0
#!/usr/bin/env python3

import cv2
import numpy as np
from matplotlib import pyplot as plt
from hough_lines_acc import *
from hough_peaks import *
from hough_lines_draw import *

if __name__ == '__main__':
    img = cv2.imread('ps1_python/input/ps1-input0.png', cv2.IMREAD_GRAYSCALE)
    img_edges = cv2.Canny(img, 0, 1)
    cv2.imwrite('ps1_python/output/ps1-1-a-1.png', img_edges)

    H, thetas, rhos = hough_lines_acc(img_edges)
    cv2.imwrite('ps1_python/output/ps1-2-a-1.png', H)

    peaks = hough_peaks(H, 10)
    P = cv2.imread('ps1_python/output/ps1-2-a-1.png')

    for i in range(0, len(peaks)):
        x, y = peaks[i]
        P = cv2.rectangle(P, (y - 2, x - 2), (y + 2, x + 2), (0, 255, 0))

    cv2.imwrite('ps1_python/output/ps1-2-b-1.png', P)

    img_color = cv2.imread('ps1_python/input/ps1-input0.png', cv2.IMREAD_COLOR)
    hough_lines_draw(img_color, 'ps1-2-c-1.png', peaks, thetas, rhos)