Ejemplo n.º 1
0
from ImageUtils import MultiplePlot
from ImageUtils.hough_transform import hough_line, extract_hough_lines
import numpy as np
import matplotlib.pyplot as plt

original = np.zeros((200, 100))
original[30:170, 25:26] = original[30:170, 74:75] = 255
original[30:31, 26:74] = original[169:170, 26:74] = 255

drawer = MultiplePlot([10, 10], [1, 2])
drawer.add(original, "Original")

accumulator, thetas, rhos = hough_line(original)
hough_lines = extract_hough_lines(accumulator, thetas, rhos, 4, 5)

out = np.zeros_like(original)

for i in hough_lines:
    i["rho"] = abs(int(i["rho"]))
    if np.rad2deg(i["theta"]) == -90:
        i["rho"] += 1
        for x_idx in range(out.shape[1]):
            out[i["rho"], x_idx] += 1
    elif np.rad2deg(i["theta"]) == 0:
        for y_idx in range(out.shape[0]):
            out[y_idx, i["rho"]] += 1

corners = np.where(out == 2)

# corners = [(a, b) for (a, b) in zip(corners_temp[0], corners_temp[1])]
y_idx = corners[0]
Ejemplo n.º 2
0
from ImageUtils import MultiplePlot
from ImageUtils.kernels import *
from ImageUtils.utils import convolve
from ImageUtils.utils import uniform_noise

original = np.zeros((200, 200))
original[50:150, 50:150] = 255

drawer = MultiplePlot([20, 10], [1, 3])
drawer.add(original, "Original")

noised = uniform_noise(original, 5)
drawer.add(noised, "Uniform noise")

cleaned = convolve(noised, kernel_noise_remove)
averaged = convolve(cleaned, kernel_average)

out = convolve(original, kernel_sobel)
out2 = abs(out)
drawer.add(out2, "After filter")
drawer.show()
Ejemplo n.º 3
0
import cv2 as cv
import numpy as np
from ImageUtils import MultiplePlot

drawer = MultiplePlot([10, 10], [1, 2])

kernel_d = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]], np.uint8)

origin = cv.imread("rise_hollow.jpg", 0)
origin[origin > 150] = 255
origin[origin < 150] = 0
invert = np.invert(origin)

filler = np.zeros_like(origin)
filler[0, 0] = 255

last = np.zeros_like(filler)

while not np.array_equal(last, filler):
    last = filler
    dilated = cv.dilate(filler, kernel_d, iterations=1)
    filler = np.bitwise_and(dilated, invert)

filler = np.invert(filler)
filled = origin + filler
drawer.add(origin, "Origin")
drawer.add(filled, "Filled")

drawer.show()
Ejemplo n.º 4
0
import cv2 as cv
import numpy as np
from ImageUtils import MultiplePlot

kernel_erosion = np.ones((3, 3))
kernel_dilation = np.ones((5, 5))
drawer = MultiplePlot([10, 10], [1, 2])

original = cv.imread('sudoku.png', 0)

drawer.add(original, "Original")

erosion = cv.erode(original, kernel_erosion, iterations=1)
dilation = cv.dilate(original, kernel_dilation, iterations=1)

drawer.add(dilation, "After Dilation")
drawer.show()
Ejemplo n.º 5
0
def main():
    for file in IMAGES:
        print("Image: {0}".format(file))
        drawer = MultiplePlot([20, 10], [1, 2])
        image = cv2.imread('ImageInputs/{0}.tif'.format(file))
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

        ret, thresh = cv2.threshold(gray, 80, 255, cv2.THRESH_BINARY)
        opened = thresh
        for i in range(35):
            opened = cv2.morphologyEx(opened, cv2.MORPH_OPEN, np.ones((3, 3)))

        filled = fill_blob(opened)
        blurred = cv2.GaussianBlur(filled, (13, 13), 0)
        edges = cv2.Canny(blurred, 100, 255, 7)

        hough_line = cv2.HoughLines(edges, 1, np.pi / 180, 120)

        y_size, x_size = np.shape(filled)

        sorted_lines = sort_lines(hough_line, x_size, y_size)

        print("Top:")
        print(sorted_lines[TOP])
        print('Bottom:')
        print(sorted_lines[BOTTOM])
        print('Left:')
        print(sorted_lines[LEFT])
        print('Right:')
        print(sorted_lines[RIGHT])

        if sorted_lines[TOP][0][RHO] > TOP_THRESH:
            sorted_lines[TOP][0][RHO] = TOP_THRESH
        sorted_lines[BOTTOM] = sorted_lines[BOTTOM][:1]

        final_lines = [
            sorted_lines[TOP][0], sorted_lines[BOTTOM][0],
            sorted_lines[RIGHT][0]
        ]
        if len(sorted_lines[LEFT]) > 1 and \
                abs(sorted_lines[LEFT][0][RHO] - sorted_lines[LEFT][1][RHO]) > INTERNAL_THRESH:
            final_lines.append(sorted_lines[LEFT][0])
        else:
            final_lines.append(sorted_lines[LEFT][-1])

        points = get_line_points(final_lines, x_size, y_size)

        pts_src = np.array(
            [points[0][0], points[0][1], points[1][0], points[1][1]])
        x_len = abs(points[0][0][0] - points[0][1][0])
        y_len = abs(points[0][0][1] - points[1][0][1])
        pts_dst = np.array([[0.0, 0.0], [x_len, 0.0], [0.0, y_len],
                            [x_len, y_len]])

        im_dst = np.zeros((y_len, x_len, 3), np.uint8)
        h, status = cv2.findHomography(pts_src, pts_dst)
        im_out = cv2.warpPerspective(image, h,
                                     (im_dst.shape[1], im_dst.shape[0]))

        drawer.add(image, 'Image {0}: Original'.format(file))
        drawer.add(im_out, 'Image {0}: Aligned'.format(file))
        drawer.show()
        print('\n\n')
Ejemplo n.º 6
0
from ImageUtils import MultiplePlot
from ImageUtils.kernels import *
from ImageUtils.utils import convolve

original = np.zeros((200, 200))
original[50:150, 50:150] = 255

drawer = MultiplePlot([10, 10], [1, 2])
drawer.add(original, "Original")
out = convolve(original, kernel_sobel)
out2 = abs(out)
drawer.add(out2, "After filter")
drawer.show()
Ejemplo n.º 7
0
import cv2 as cv
import numpy as np
from ImageUtils import MultiplePlot
from ImageUtils.utils import threshold

drawer = MultiplePlot([10, 10], [1, 2])

original = cv.imread('rise.jpg', 0)

thresholded = threshold(original, 115)

opened = cv.morphologyEx(thresholded, cv.MORPH_OPEN, np.ones((7, 7)))
erosion = cv.erode(opened, np.ones((5, 5)), iterations=1)
res = opened - erosion
res[res == 1] = 200

drawer.add(original, "Boundary")

drawer.add(res, "After")

cv.imwrite("rise_hollow.jpg", res)

drawer.show()