def update_data(): global last, a last = a while True: last = a for hit, miss in zip(hit_list, miss_list): hm = m.binary_hit_or_miss(a, hit, miss) a = np.logical_and(a, np.logical_not(hm)) try: pl.imshow(a, cmap=pl.cm.gray, interpolation="nearest") fig.canvas.draw_idle() yield except: import sys sys.exit() if np.abs(last - a).max() == 0: yield
def skeletonize(img): """Skeletonize image""" print("\nSkeletonizing image...") s = np.array([[1, 0, 0, 1], [0, 1, 1, 0], [0, 1, 1, 0], [1, 0, 0, 1]]) skl = [] for i in img: j = skeletonize_3d(i) k = morp.binary_hit_or_miss(j, structure1=s) k_ind = k.nonzero() k_ind = np.column_stack(k_ind) if len(k_ind) > 0: for el in k_ind: j[el[0], el[1]] = 0 skl.append(j) skl = np.array(skl, dtype=int, copy=False) skl[skl > 0] = 1 print("Image skeletonized.\n") return skl
def diag(imIn): strl = np.array([[[0, 1, 0], [1, 0, 0], [0, 0, 0]], [[0, 1, 0], [0, 0, 1], [0, 0, 0]], [[0, 0, 0], [1, 0, 0], [0, 1, 0]], [[0, 0, 0], [0, 0, 1], [0, 1, 0]], [[0, 1, 0], [1, 0, 0], [0, 1, 0]], [[0, 1, 0], [1, 0, 1], [0, 0, 0]], [[0, 1, 0], [0, 0, 1], [0, 1, 0]], [[0, 0, 0], [1, 0, 1], [0, 1, 0]]], dtype=np.uint8) bwIm = np.zeros(imIn.shape, dtype=int) imIn = np.array(imIn) imIn = imIn / np.max(np.max(imIn)) #normalizing to be added later for i in range(7): bwIm = bwIm + smorph.binary_hit_or_miss(imIn, strl[i, :, :]) bwIm = ((bwIm > 0) + imIn) > 0 return bwIm # out put is boolean
def prune(sklt: np.array, windows: np.array, iters: int = 1): """ Remove iteratively matching points of the skeleton image Arguments: sklt - skeleton image to be pruned. Can be acquired via skeleton() \ function windows - array of n dimentional windows to match iters - number of times to repeat the pruning procedure Return the pruned image """ pruned = np.array(sklt) for _ in itertools.repeat(None, iters): for wnd in windows: pruned[binary_hit_or_miss(pruned, wnd)] = 0 return pruned
def task3(): img = mimg.imread("hm2.bmp") img = rgb2gray(img) new_img = 1 - img new_img = binary_hit_or_miss(new_img, structure1=square(50)) new_img = np.array(new_img, dtype=np.uint8) _, contours, hierarchy = cv2.findContours(new_img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) number_of_squares = len(contours) print(number_of_squares) fig, ax = plt.subplots(1, 2) show(ax[0], img, "original") show(ax[1], new_img, "hit_or_miss") plt.show()
def getLineEnd(img): h1 = np.array([[0, 0, 0], [0, 1, 0], [0, 1, 0]]) m1 = np.array([[1, 1, 1], [1, 0, 1], [1, 0, 1]]) h2 = np.array([[0, 0, 0], [0, 1, 0], [0, 0, 1]]) m2 = np.array([[1, 1, 1], [1, 0, 1], [1, 1, 0]]) hit_list = [] miss_list = [] for k in range(4): hit_list.append(np.rot90(h1, k)) hit_list.append(np.rot90(h2, k)) miss_list.append(np.rot90(m1, k)) miss_list.append(np.rot90(m2, k)) line_end = np.zeros(img.shape) for hit, miss in zip(hit_list, miss_list): hm = m.binary_hit_or_miss(img, hit, miss) line_end = np.logical_or(line_end, hm) return line_end
def X_marksTspot(I, S_hit, S_miss): #make a color image and insert the binary image in one channel. I2 = Normalize(I) Iout = np.dstack([I2, I2, I2]) #get hits from hit or miss hits = binary_hit_or_miss( I, structure1=S_hit, structure2=S_miss) #, origin1 = (n//2, m//2), origin2 = (n//2, m//2)) loc = np.array(np.where(hits == True)) #draw x in red at locations for hits for i in range(loc.shape[1]): x, y = loc[:, i] rr, cc = line(x - 10, y - 8, x + 10, y + 8) Iout[rr, cc, :] = [1, 0, 0] rr, cc = line(x + 10, y - 8, x - 10, y + 8) Iout[rr, cc, :] = [1, 0, 0] return (Iout)
def skeletonize(img): h1 = np.array([[0, 0, 0], [0, 1, 0], [1, 1, 1]]) m1 = np.array([[1, 1, 1], [0, 0, 0], [0, 0, 0]]) h2 = np.array([[0, 0, 0], [1, 1, 0], [0, 1, 0]]) m2 = np.array([[0, 1, 1], [0, 0, 1], [0, 0, 0]]) hit_list = [] miss_list = [] for k in range(4): hit_list.append(np.rot90(h1, k)) hit_list.append(np.rot90(h2, k)) miss_list.append(np.rot90(m1, k)) miss_list.append(np.rot90(m2, k)) img = img.copy() while True: last = img for hit, miss in zip(hit_list, miss_list): hm = m.binary_hit_or_miss(img, hit, miss) img = np.logical_and(img, np.logical_not(hm)) if np.all(img == last): break return img
def getSkeletonizedImage(binary): h1 = np.array([[0, 0, 0], [0, 1, 0], [1, 1, 1]]) m1 = np.array([[1, 1, 1], [0, 0, 0], [0, 0, 0]]) h2 = np.array([[0, 0, 0], [1, 1, 0], [0, 1, 0]]) m2 = np.array([[0, 1, 1], [0, 0, 1], [0, 0, 0]]) hit_list = [] miss_list = [] for k in range(4): hit_list.append(np.rot90(h1, k)) hit_list.append(np.rot90(h2, k)) miss_list.append(np.rot90(m1, k)) miss_list.append(np.rot90(m2, k)) binary = binary.copy() while True: last = binary for hit, miss in zip(hit_list, miss_list): hm = m.binary_hit_or_miss(binary, hit, miss) binary = np.logical_and(binary, np.logical_not(hm)) if np.all(binary == last): break return binary
def skeletonize(img): h1 = np.array([[0, 0, 0],[0, 1, 0],[1, 1, 1]]) m1 = np.array([[1, 1, 1],[0, 0, 0],[0, 0, 0]]) h2 = np.array([[0, 0, 0],[1, 1, 0],[0, 1, 0]]) m2 = np.array([[0, 1, 1],[0, 0, 1],[0, 0, 0]]) hit_list = [] miss_list = [] for k in range(4): hit_list.append(np.rot90(h1, k)) hit_list.append(np.rot90(h2, k)) miss_list.append(np.rot90(m1, k)) miss_list.append(np.rot90(m2, k)) img = img.copy() while True: last = img for hit, miss in zip(hit_list, miss_list): hm = m.binary_hit_or_miss(img, hit, miss) img = np.logical_and(img, np.logical_not(hm)) if np.all(img == last): break return img
def prune(img, n): h1 = np.array([[0, 0, 0], [0, 1, 0], [0, 1, 0]]) m1 = np.array([[1, 1, 1], [1, 0, 1], [0, 0, 0]]) h2 = np.array([[0, 0, 0], [0, 1, 0], [0, 0, 1]]) m2 = np.array([[1, 1, 1], [1, 0, 1], [1, 1, 0]]) hit_list = [] miss_list = [] for k in range(4): hit_list.append(np.rot90(h1, k)) hit_list.append(np.rot90(h2, k)) miss_list.append(np.rot90(m1, k)) miss_list.append(np.rot90(m2, k)) img = img.copy() pru = np.zeros(img.shape) for k in range(n): for hit, miss in zip(hit_list, miss_list): hm = m.binary_hit_or_miss(img, hit, miss) pru = np.logical_or(pru, hm) img = np.logical_and(img, np.logical_not(hm)) img = np.array(img, dtype=np.uint8) return img, pru
def fill_gaps(image_array): shape = image_array.shape #returns tuple of row,col rows = shape[0] cols = shape[1] im = np.zeros((rows,cols)) ### arrays of filling elements f1 = np.array([[0,1,0],[0,0,0],[0,1,0]]) f2 = np.array([[0,0,0],[1,0,1],[0,0,0]]) f3 = np.array([[1,0,0],[0,0,0],[0,0,1]]) f4 = np.array([[0,0,1],[0,0,0],[1,0,0]]) fill_kern = [f1,f2,f3,f4] print 'filling gaps...' for r in range(4): #find gaps, fill zed = morphology.binary_hit_or_miss(np.where(image_array + im != 0,1,0), structure1=fill_kern[r]) im = im + zed print r return im
import numpy as np from scipy.ndimage import morphology all_objects = [] image = np.load('ps.npy.txt') first = np.ones((4, 6)) all_objects.append(first) second = first second[:2, 2:4] = 0 all_objects.append(second) third = np.flip(second) all_objects.append(third) fourth = np.transpose(second) all_objects.append(fourth) fifth = np.transpose(third) all_objects.append(fifth) count_objects = [] for i in range(5): new_objects = morphology.binary_hit_or_miss(image, all_objects[i]) count_objects.append(np.sum(new_objects)) print(f'Count of objects {i} - {count_objects[i]}') print(f'All objects - {np.sum(count_objects)}')
def main(): plt.close("all") plt.figure(figsize=(20, 12)) n_ig = 6 igs = [] count = 0 while count < n_ig: orbit = np.random.randint(6000) + 2000 file_name = ais.find_file(orbit) nsweeps = 0 try: stats = os.stat(file_name) nsweeps = stats[stat.ST_SIZE] / 400 except OSError as e: print(e) if nsweeps > (180 * 150): tmp = ais.read_ais(orbit) if len(tmp) > 150: ignumber = np.random.randint(len(tmp)) igs.append(tmp[ignumber]) count = count + 1 print("ORBIT %d, Ionogram %d" % (orbit, ignumber)) continue print("REJECTING") n = len(igs) igs = igs[0:n_ig] g = mpl.gridspec.GridSpec(6, 6, wspace=0.16, hspace=0.1, left=0.05, right=0.95, bottom=0.08, top=0.95) # plt.hot() vmin, vmax = -16, -13 thresold = -15 fp_structure = np.ones((10,1)) td_structure = np.ones((1,4)) for i, ig in enumerate(igs): ig.interpolate_frequencies() data = ig.data plt.subplot(g[i,0]) plt.imshow(np.log10(data), interpolation='nearest', aspect='auto', vmin=vmin, vmax=vmax) if i == 0: plt.title("Data") plt.subplot(g[i,1]) bdata = np.zeros(data.shape, dtype=bool) bdata[data > (10.**thresold)] = True plt.imshow(bdata, interpolation='nearest', aspect='auto') if i == 0: plt.title("Threshold @ %f" % thresold) plt.subplot(g[i,2]) dfp = morphology.binary_opening(bdata, structure=fp_structure) plt.imshow(dfp, interpolation='nearest', aspect='auto') if i == 0: plt.title("FP-Lines: v-opened") # plt.subplot(g[i,3]) # dtd = morphology.binary_opening(bdata, structure=td_structure) # plt.imshow(dtd, interpolation='nearest', aspect='auto') # if i == 0: # plt.title("CYC LINES?") plt.subplot(g[i,3]) dtd = np.logical_and(bdata, np.logical_not(dfp)) plt.imshow(dtd, interpolation='nearest', aspect='auto') if i == 0: plt.title("Residual") plt.subplot(g[i,4]) # dtd = np.logical_and(bdata, np.logical_not(dfp)) # dtd = morphology.binary_dilation(dtd, structure=morphology.generate_binary_structure(2,1)) # dtd = morphology.binary_fill_holes(dtd, structure=np.ones((1,3))) dtd = morphology.binary_opening(dtd, structure=np.ones((1,4))) structure = np.ones((2,1)) structure[1,0] = 0 dtd = morphology.binary_hit_or_miss(dtd, structure1=structure) plt.imshow(dtd, interpolation='nearest', aspect='auto') if i == 0: plt.title("Cyc. lines: residual h-opened") # plt.subplot(g[i,5]) # dtd = np.logical_and(bdata, np.logical_not(np.logical_or(dtd, dfp))) # plt.imshow(dtd, interpolation='nearest', aspect='auto') # if i == 0: # plt.title("RESIDUAL") plt.subplot(g[i,5]) dtd = morphology.binary_closing(dtd) plt.imshow(dtd, interpolation='nearest', aspect='auto') if i == 0: plt.title("Ionosphere - topside edge") # plt.subplot(g[i,4]) # d = np.logical_and(bdata, np.logical_not(dfp)) # d = morphology.binary_erosion(d, structure=td_structure) # d = morphology.binary_opening(d, structure=td_structure) # plt.imshow(d, interpolation='nearest', aspect='auto') # if i == 0: # plt.title("IONOSPHERE?") # plt.subplot(g[i,5]) # # d = np.logical_and(bdata, np.logical_not(np.logical_or(dfp, d))) # # d = morphology.binary_closing(bdata,structure=morphology.generate_binary_structure(2,2), iterations=3) # d = morphology.binary_dilation(bdata, structure=np.ones((2,1))) # plt.imshow(d, interpolation='nearest', aspect='auto') # if i == 0: # plt.title("RESIDUAL?") plt.show()
from scipy.ndimage import morphology image = np.load('ps.npy.txt') mask1 = np.array([[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 1, 1], [1, 1, 1, 1]]) mask2 = np.array([[1, 1, 1, 1], [1, 1, 1, 1], [0, 0, 1, 1], [0, 0, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]) mask3 = np.array([[1, 1, 0, 0, 1, 1], [1, 1, 0, 0, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1]]) mask4 = np.array([[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 0, 0, 1, 1], [1, 1, 0, 0, 1, 1]]) mask5 = np.array([[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1]]) masks = np.array([mask1, mask2, mask3, mask4, mask5]) summ = 0 for i in range(5): result = morphology.binary_hit_or_miss(image, masks[i]) print("Symbol %i, quantity =" % (i + 1), np.sum(result)) summ += np.sum(result) print("Total sum =", summ) plt.imshow(image) plt.show()
def hitmiss_demo(a, structure1, structure2): global b b = m.binary_hit_or_miss(a, structure1, structure2) img = expand_image(a, 100) return expand_image(b, 255, out=img)
if (other[r:r + b1.shape[0], c:c + b1.shape[1]].shape == b1.shape): other[r:r + b1.shape[0], c:c + b1.shape[1]] = b1 print(count) return other f = color.rgb2gray(io.imread('blobs_inv.png', plugin='pil')) f = (f >= 10).astype(float) se1 = np.transpose(np.array([[1, 1, 1, 1, 1]])) se2 = disk(2) se3 = np.zeros((3, 3)) se3[1:3, 0:2] = 1 print(se3) hom1 = binary_hit_or_miss(f, se1) hom2 = binary_hit_or_miss(f, se2) hom3 = binary_hit_or_miss(f, se3) hom1 = 1 - hom(1 - f, se1, 1 - se1) hom2 = 1 - hom(1 - f, se2, 1 - se2) hom3 = 1 - hom(1 - f, se3, 1 - se3) print(f) print(1 - f) print(hom3) what1 = 1 - white_tophat(1 - f, se1) what2 = 1 - white_tophat(1 - f, se2) what3 = 1 - white_tophat(1 - f, se3) bhat1 = 1 - black_tophat(1 - f, se1) bhat2 = 1 - black_tophat(1 - f, se2) bhat3 = 1 - black_tophat(1 - f, se3)
from skimage import io from skimage.color import rgb2gray from skimage.morphology import rectangle import numpy as np import matplotlib.pyplot as plt from scipy.ndimage.morphology import binary_hit_or_miss path = "images/lab5/hit_or_miss.png" path1 = "images/lab5/str.png" image = io.imread(path) str = io.imread(path1) image = rgb2gray(image) str = rgb2gray(str) print(np.shape(str)) HOMimage = binary_hit_or_miss(image, structure1=str) plt.gray() plt.subplot(1, 2, 1) plt.imshow(image) plt.axis('off') plt.subplot(1, 2, 2) plt.imshow(HOMimage) plt.axis('off') plt.show()
import numpy as np from scipy.ndimage import morphology as mrg image = np.load('ps.npy.txt') plt.figure(figsize=(8, 6), dpi=100) plt.imshow(image[100:300, 200:400]) mask1 = np.ones((4, 6)) # mask2 = np.ones((4, 6)) # ப mask2[:2, 2:4] = 0 mask3 = np.flip(mask2) # п mask4 = np.transpose(mask2) # ɔ mask5 = np.transpose(mask3) # c masks = np.array([mask1, mask2, mask3, mask4, mask5]) result = [] for i in range(5): image_new = mrg.binary_hit_or_miss(image, masks[i]) # plt.figure(figsize=(8,6), dpi=100) # plt.imshow(image2[100:300, 200:400]) result.append(np.sum(image_new)) symbols = ['', 'ப', 'п', 'ɔ', 'c'] for i in range(len(result)): print(result[i], '-', symbols[i]) print(np.sum(result), '- all')