def lesson11(): brain_h = io.read_from_xcr('brain-H_x512.bin', width=512, height=512) brain_v = io.read_from_xcr('brain-V_x256.bin', width=256, height=256) spine_h = io.read_from_xcr('spine-H_x256.bin', width=256, height=256) spine_v = io.read_from_xcr('spine-V_x512.bin', width=512, height=512) # return np.array([ # (brain_h, brain_v, histogram(brain_h), histogram(brain_v)), # (spine_h, spine_v, histogram(spine_h), histogram(spine_v)), # ]) gamma_bh = gamma(brain_h, C=1.3, gamma=1.05) gamma_bv = gamma(brain_v, C=1.3, gamma=1.05) gamma_sh = gamma(spine_h, C=1.3, gamma=1.2) gamma_sv = gamma(spine_v, C=1.3, gamma=1.3) thresh_bh = thresholding(gamma_bh, thresh=18) thresh_bv = thresholding(gamma_bv, thresh=18) thresh_sh = thresholding(gamma_sh, thresh=18) thresh_sv = thresholding(gamma_sv, thresh=18) and_bh = and_pic(gamma_bh, thresh_bh) and_bv = and_pic(gamma_bv, thresh_bv) and_sh = and_pic(gamma_sh, thresh_sh) and_sv = and_pic(gamma_sv, thresh_sv) median_bh = mean_filter(and_bh, size=3) median_bv = mean_filter(and_bv, size=3) median_sh = mean_filter(and_sh, size=3) median_sv = mean_filter(and_sv, size=3) equal_bh = Picture("", cv2.equalizeHist(median_bh.matrix)) equal_bv = Picture("", cv2.equalizeHist(median_bv.matrix)) equal_sh = Picture("", cv2.equalizeHist(median_sh.matrix)) equal_sv = Picture("", cv2.equalizeHist(median_sv.matrix)) # return np.array([ # (histogram(median_bh), cdf(median_bh), Picture("", cv2.equalizeHist(median_bh.matrix))), # (histogram(median_bv), cdf(median_bv), Picture("", cv2.equalizeHist(median_bv.matrix))), # (histogram(median_sh), cdf(median_sh), Picture("", cv2.equalizeHist(median_sh.matrix))), # (histogram(median_sv), cdf(median_sv), Picture("", cv2.equalizeHist(median_sv.matrix))), # ]) return np.array([ (brain_h, scale(equal_bh, w_target=400, h_target=400)), (brain_v, scale(equal_bv, w_target=400, h_target=400)), (spine_h, scale(equal_sh, w_target=400, h_target=400)), (spine_v, scale(equal_sv, w_target=400, h_target=400)), ]).T
def lesson10(): img = io.read_from_jpg("MODEL.jpg") thresh_img = thresholding(img, thresh=200) erode_img = erode(thresh_img) dilate_img = dilate(thresh_img) # return np.array([ # (img, thresh_img), # (erode_img, minus_pic(thresh_img, erode_img)), # (dilate_img, minus_pic(dilate_img, thresh_img)), # ]) noised = gaussian_noise(img, percent=0.15) thresh_noised = thresholding(noised, thresh=200) a1 = closing(thresh_noised, size=3) a2 = opening(a1, size=5) a3 = closing(a2, size=7) a4 = erode(a3) import cv2 return np.array([ (noised, thresh_noised, noised), (a1, a2, a3), (a4, minus_pic(a3, a4), Picture( "kernel", scale_array(cv2.getStructuringElement(cv2.MORPH_CROSS, (7, 7)), left=0, right=255))), ])
def read_img_from_dat(self, filepath, width=259, height=185): with open(INPUT_FOLDER + filepath, 'rb') as f: whole_data = f.read() format = '{:d}f'.format(len(whole_data) // 4) whole_array = np.array(struct.unpack(format, whole_data)) matrix = whole_array.reshape((height, width)) return Picture(filepath, matrix) pass
def read_from_xcr(self, filepath, width=400, height=300, normalize=True): matrix = [] with open(INPUT_FOLDER + filepath, 'rb') as f: for i in range(height): row = width * [0] # np.zeros(width) for j in range(width): row[j] = int.from_bytes(f.read(2), 'little', signed=False) matrix.append(row) nparray = np.asarray(matrix).astype('float64') if normalize: nparray = scale_array(nparray, right=255) return Picture(filepath, nparray.astype('uint8'))
def scale(picture, ratio=None, w_ratio=None, h_ratio=None, w_target=None, h_target=None, strategy="bilinear"): h, w = picture.matrix.shape if ratio is not None: w_ratio = ratio h_ratio = ratio w_target = round(w * w_ratio) h_target = round(h * h_ratio) elif w_ratio is not None: w_target = round(w * w_ratio) h_target = round(h * h_ratio) elif w_target is not None: w_ratio = w_target / w h_ratio = h_target / h scaled = np.empty((h_target, w_target)) if strategy == "nearest-neighbor": _nearest_neighbor(picture, h_target, w_target, h_ratio, w_ratio, scaled) if strategy == "bilinear": _bilinear(picture, h_target, w_target, h_ratio, w_ratio, scaled) ratio = (w_ratio + h_ratio) / 2 if ratio is None else ratio return Picture(picture.title + "(x"+str(ratio)+")", scaled)
def mean_filter(picture: Picture, size=5): matrix = __base_filter(picture.matrix, size, lambda window: np.average(window)) return Picture(picture.title, matrix)
def median_filter(picture: Picture, size=5): matrix = signal.medfilt2d(picture.matrix, kernel_size=size) # a lot faster # matrix = __base_filter(picture.matrix, window_size, lambda window: np.median(window)) return Picture(picture.title, matrix)
def and_pic(pic1: Picture, pic2: Picture): return Picture(pic1.title + " & " + pic2.title, pic1.matrix & pic2.matrix)
def or_pic(pic1: Picture, pic2: Picture): return Picture(pic1.title + " | " + pic2.title, pic1.matrix | pic2.matrix)
def minus_pic(pic1: Picture, pic2: Picture): return Picture(pic1.title + " - " + pic2.title, pic1.matrix - pic2.matrix)
def plus_pic(pic1: Picture, pic2: Picture): return Picture(pic1.title + " + " + pic2.title, pic1.matrix + pic2.matrix)
def xor_pic(pic1: Picture, pic2: Picture): return Picture(pic1.title + " ^ " + pic2.title, pic1.matrix ^ pic2.matrix)
def read_from_jpg(self, filepath): return Picture(filepath, cv2.imread(INPUT_FOLDER + filepath, cv2.IMREAD_GRAYSCALE))