Ejemplo n.º 1
0
def cleanImage(bin_img, name, plot=False, path=None):
    print("Cleaning image...")
    n = 3
    n_2 = 3
    iter = 2  #3
    iter_2 = 2
    kernel = (n, n)
    kernel2 = (n_2, n_2)
    cleaned = bin_img.copy()

    # cleaned = cv2.dilate(cleaned, kernel= kernel2, iterations= iter_2)
    cleaned = cv2.morphologyEx(cleaned,
                               cv2.MORPH_CLOSE,
                               kernel=kernel,
                               iterations=iter)
    cleaned = cv2.morphologyEx(cleaned,
                               cv2.MORPH_OPEN,
                               kernel=kernel,
                               iterations=iter)
    cleaned = cv2.erode(cleaned, kernel=kernel2, iterations=iter_2)

    if path is not None:
        new_path = altsep.join((path, "Cleaned"))
        if not exists(new_path):
            tl.makeDir(new_path)
        dst = altsep.join((new_path, (name + ".png")))
        cv2.imwrite(dst, cleaned)

    if plot:
        cv2.imshow("Cleaned '{}'".format(name), cleaned)
        cv2.waitKey(2000)
        cv2.destroyAllWindows()

    return cleaned
Ejemplo n.º 2
0
def blurrImage(nameImage, name, plot=False, path=None):
    print("Blurring image...")
    image = cv2.imread(nameImage)
    if image.shape[2] == 3:
        # print("COLOR")
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    else:
        gray = image
    kernel = (7, 7)
    cleaned = cv2.GaussianBlur(gray, kernel, 0)

    if path is not None:
        new_path = altsep.join((path, "Blurred"))
        if not exists(new_path):
            tl.makeDir(new_path)
        dst = altsep.join((new_path, (name + ".png")))
        img_color = cv2.cvtColor(cleaned, cv2.COLOR_GRAY2BGR)
        cv2.imwrite(dst, img_color)

    if plot:
        cv2.imshow("Original '{}'".format(name), image)
        cv2.waitKey(2000)
        cv2.destroyAllWindows()

        cv2.imshow("Cleaned '{}'".format(name), cleaned)
        cv2.waitKey(2000)
        cv2.destroyAllWindows()
    return cleaned
Ejemplo n.º 3
0
def zhangSuen(image, name, plot=False, path=None):
    "the Zhang-Suen Thinning Algorithm"
    Image_Thinned = image.copy()  # deepcopy to protect the original image
    changing1 = changing2 = 1  #  the points to be removed (set as 0)
    while changing1 or changing2:  #  iterates until no further changes occur in the image
        # Step 1
        changing1 = []
        rows, columns = Image_Thinned.shape  # x for rows, y for columns
        for x in range(1, rows - 1):  # No. of  rows
            for y in range(1, columns - 1):  # No. of columns
                P2, P3, P4, P5, P6, P7, P8, P9 = n = neighbours(
                    x, y, Image_Thinned)
                if (Image_Thinned[x][y] == 1
                        and  # Condition 0: Point P1 in the object regions
                        2 <= sum(n) <= 6 and  # Condition 1: 2<= N(P1) <= 6
                        transitions(n) == 1 and  # Condition 2: S(P1)=1
                        P2 * P4 * P6 == 0 and  # Condition 3
                        P4 * P6 * P8 == 0):  # Condition 4
                    changing1.append((x, y))
        for x, y in changing1:
            Image_Thinned[x][y] = 0
        # Step 2
        changing2 = []
        for x in range(1, rows - 1):
            for y in range(1, columns - 1):
                P2, P3, P4, P5, P6, P7, P8, P9 = n = neighbours(
                    x, y, Image_Thinned)
                if (Image_Thinned[x][y] == 1 and  # Condition 0
                        2 <= sum(n) <= 6 and  # Condition 1
                        transitions(n) == 1 and  # Condition 2
                        P2 * P4 * P8 == 0 and  # Condition 3
                        P2 * P6 * P8 == 0):  # Condition 4
                    changing2.append((x, y))
        for x, y in changing2:
            Image_Thinned[x][y] = 0

    if path is not None:
        new_path = altsep.join((path, "ZhangSuen"))
        if not exists(new_path):
            tl.makeDir(new_path)
        dst = altsep.join((new_path, (name + ".png")))
        cv2.imwrite(dst, Image_Thinned)

    if plot:
        cv2.imshow("Thinning '{}'".format(name), Image_Thinned)
        cv2.waitKey(1000)
        cv2.destroyAllWindows()
    return Image_Thinned
Ejemplo n.º 4
0
def image_enhance(gray, name, plot=False, path=None):
    print("Enhancing ridges...")
    blksze = 16
    thresh = 0.1
    print(type(gray))
    normim, mask = ridge_segment(gray, blksze,
                                 thresh)  # normalise the image and find a ROI

    gradientsigma = 1
    blocksigma = 7
    orientsmoothsigma = 7
    orientim = ridge_orient(
        normim, gradientsigma, blocksigma,
        orientsmoothsigma)  # find orientation of every pixel

    blksze = 38
    windsze = 5
    minWaveLength = 5
    maxWaveLength = 15
    freq, medfreq = ridge_freq(
        normim, mask, orientim, blksze, windsze, minWaveLength,
        maxWaveLength)  #find the overall frequency of ridges

    freq = medfreq * mask
    kx = 0.65
    ky = 0.65
    newim = ridge_filter(normim, orientim, freq, kx,
                         ky)  # create gabor filter and do the actual filtering

    img_enhanced = (newim < -3).astype(float)

    if path is not None:
        new_path = altsep.join((path, "Enhanced"))
        if not exists(new_path):
            tl.makeDir(new_path)
        dst = altsep.join((new_path, (name + ".png")))
        img_color = cv2.cvtColor(img_enhanced, cv2.COLOR_GRAY2BGR)
        cv2.imwrite(dst, img_color)

    if plot:
        cv2.imshow("Enhanced '{}'".format(name), img_enhanced)
        cv2.waitKey(2000)
        cv2.destroyAllWindows()

    return img_enhanced
Ejemplo n.º 5
0
def thinImage(image, name, plot=False, path=None):
    print("Thinning image...")
    image *= 255
    image8 = np.uint8(image)
    thinned = thinning(image8.astype(np.uint8))

    if path is not None:
        new_path = altsep.join((path, "Thinned"))
        if not exists(new_path):
            tl.makeDir(new_path)
        dst = altsep.join((new_path, (name + ".png")))
        cv2.imwrite(dst, thinned)

    if plot:
        cv2.imshow("Thinning '{}'".format(name), thinned)
        cv2.waitKey(2000)
        cv2.destroyAllWindows()

    return thinned
Ejemplo n.º 6
0
    ap.add_argument("-r",
                    "--results",
                    required=False,
                    help="-r Destiny path where the results will be stored.")
    args = vars(ap.parse_args())

    # Configuration
    image_ext = '.tif'
    plot = False
    path = None
    # ratio = 0.2
    # Create folders for results
    # -r ../Data/Results/fingerprints
    if args.get("results") is not None:
        if not exists(args["results"]):
            tl.makeDir(args["results"])
        path = args["results"]
    # Extract names
    all_images = tl.natSort(tl.getSamples(args["path"], image_ext))
    # Split train and test data
    # train_data, test_data = tl.split_train_test(all_images, ratio)
    print("\nAll_images size: {}\n".format(len(all_images)))
    all_times = []
    for image in all_images:
        start = time.time()
        name = splitext(basename(image))[0]
        print("\nProcessing image '{}'".format(name))
        cleaned_img = preprocess.blurrImage(image, name, plot)
        enhanced_img = img_e.image_enhance(cleaned_img, name, plot)
        cleaned_img = preprocess.cleanImage(enhanced_img, name, plot)
        # skeleton = preprocess.zhangSuen(cleaned_img, name, plot)
Ejemplo n.º 7
0
    n_iter = 50  # 10 - 100
    lam_pond = 1  # 0.1 - 60

    ext = conf_seq[seq_idx]
    window = [5, 9, 15]
    n = 5
    k_gauss = (n, n)
    plt_step = 3

    # Get the frames of the sequence and make results folder
    seq_selected = sequences[seq_idx]
    name_sequence = basename(seq_selected)
    results_path = altsep.join((args["results"], name_sequence))

    if not exists(results_path) and save:
        tl.makeDir(results_path)

    frames = tl.natSort(tl.getSamples(seq_selected, ext))
    # print(frames)

    for win in window:
        start = time.time()

        # Compute the optical flow using all frames of the sequence and measure times
        for fr_idx in range(len(frames) - 1):
            if ext == '.gif':
                img1 = Image.open(frames[fr_idx])
                img1_rgb = img1.convert('RGB')
                img1_rgb = cv2.cvtColor(np.array(img1_rgb), cv2.COLOR_RGB2BGR)

                img2 = Image.open(frames[fr_idx + 1])
Ejemplo n.º 8
0
import argparse
import time
import pickle

if __name__ == '__main__':
    ap = argparse.ArgumentParser()
    ap.add_argument("-d", "--data",
                    help="-d Training data")
    ap.add_argument("-r", "--results", required= False,
                    help="-r Results path")
    args = vars(ap.parse_args())

    # Create results folder
    if args.get("results") is not None:
        if not exists(args["results"]):
            tl.makeDir(results_folder)
        results_folder = args["results"]
    else:
        results_folder = "."

    # Configuration
    pca_threshold = 1.5
    val_ratio = 0.2
    plot = True
    max_estimators = 201
    ini_estimators = 10
    step_estimators = 10
    max_depth = 11
    seed = 10
    models = {}
    loop = 5