def koponging(bw): bwr = bw.copy() # remove filler rollup, rolldown, rollleft, rollright = nph.roll_all(bwr) bwr -= bwr * rolldown * rollup * rollright * rollleft # remove zigzag rollup, rollright = nph.roll_up(bwr), nph.roll_right(bwr) rolldownleft = nph.roll_left(nph.roll_down(bwr)) bwr -= bwr * rollup * rollright * (rolldownleft == False) rollright, rolldown = nph.roll_right(bwr), nph.roll_down(bwr) rollupleft = nph.roll_left(nph.roll_up(bwr)) bwr -= bwr * rollright * rolldown * (rollupleft == False) rolldown, rollleft = nph.roll_down(bwr), nph.roll_left(bwr) rollupright = nph.roll_right(nph.roll_up(bwr)) bwr -= bwr * rolldown * rollleft * (rollupright == False) rollleft, rollup = nph.roll_left(bwr), nph.roll_up(bwr) rolldownright = nph.roll_right(nph.roll_down(bwr)) bwr -= bwr * rollleft * rollup * (rolldownright == False) return bwr
def get_identity(tulang): rollup, rolldown, rollleft, rollright = nph.roll_all(tulang) rollupleft = nph.roll_left(rollup) rollupright = nph.roll_right(rollup) rolldownleft = nph.roll_left(rolldown) rolldownright = nph.roll_right(rolldown) neighbors = tulang.astype(np.uint8)*(0+rollup+rolldown+rollleft+rollright+rollupleft+rollupright+rolldownleft+rolldownright) ujung_nonzero = (neighbors == 1).nonzero() simpangan_nonzero = (neighbors > 2).nonzero() ujung = [] for i in range(0, ujung_nonzero[0].shape[0]): ujung.append((ujung_nonzero[0][i], ujung_nonzero[1][i])) simpangan = [] for i in range(0, simpangan_nonzero[0].shape[0]): simpangan.append((simpangan_nonzero[0][i], simpangan_nonzero[1][i])) # remove redundant simpangan i = 0 while i < len(simpangan): yi, xi = simpangan[i] j = i+1 while j < len(simpangan): yj, xj = simpangan[j] if yi-1 <= yj <= yj+1 and xi-1 <= xj <= xi+1: simpangan.pop(j) j -= 1 j += 1 i += 1 return (ujung, simpangan)
def gaussian(img): # kernel = [1, 3, 5, 3, 1] kernel = [1, 4, 7, 9, 7, 4, 1] kernel_count = len(kernel) kernel_half = (kernel_count-1)/2 rolls = range(0, len(kernel)) result = np.zeros(img.shape, dtype=np.float) rolls[kernel_half] = img.astype(np.float) for i in range(1, kernel_half+1): rolls[kernel_half+i] = nph.roll_left(rolls[kernel_half+i-1], img[:,-1].copy()) rolls[kernel_half-i] = nph.roll_right(rolls[kernel_half-i+1], img[:,0].copy()) for i in range(0, kernel_count): result += rolls[i] * kernel[i] result /= sum(kernel) rolls[kernel_half] = result.copy() result = 0 for i in range(1, kernel_half+1): rolls[kernel_half+i] = nph.roll_up(rolls[kernel_half+i-1], img[-1].copy()) rolls[kernel_half-i] = nph.roll_down(rolls[kernel_half-i+1], img[0].copy()) for i in range(0, kernel_count): result += rolls[i] * kernel[i] result /= sum(kernel) return result.round().astype(np.uint8)
def create_rolls(gray, kernel): mid = (len(kernel)-1)/2 rolls = [] for y in kernel: roll = [] for x in y: roll.append(gray.copy().astype(np.float)) rolls.append(roll) for i in range(0, mid): for j in range(0, i+1): for k in range(0, len(rolls)): rolls[j][k][:] = nph.roll_down(rolls[j][k], rolls[mid][j][0].copy()) for i in range(len(rolls)-1, mid, -1): for j in range(len(rolls)-1, i-1, -1): for k in range(0, len(rolls)): rolls[j][k][:] = nph.roll_up(rolls[j][k], rolls[mid][j][-1].copy()) for i in range(0, mid): for j in range(0, i+1): for k in range(0, len(rolls)): rolls[k][j][:] = nph.roll_right(rolls[k][j], rolls[k][mid][:, 0].copy()) for i in range(len(rolls)-1, mid, -1): for j in range(len(rolls)-1, i-1, -1): for k in range(0, len(rolls)): rolls[k][j][:] = nph.roll_left(rolls[k][j], rolls[k][mid][:, -1].copy()) return rolls
def mulsum(gray, operator_baris, operator_kolom, y, x): rolled = np.array(gray, dtype=np.int32) if y == 0: rolled[:] = nph.roll_down(rolled) elif y == 2: rolled[:] = nph.roll_up(rolled) if x == 0: rolled[:] = nph.roll_right(rolled) elif x == 2: rolled[:] = nph.roll_left(rolled) return rolled * (operator_baris[y][x] + operator_kolom[y][x])