import cv2 import base as bs import numpy as np def conv_to_gray(img): return (0.0722 * img[:, :, 0] + 0.7152 * img[:, :, 1] + 0.2126 * img[:, :, 2]).astype(np.uint8) img = cv2.imread("imori.jpg").astype(np.float) img_gray = conv_to_gray(img) bs.show_img(img_gray)
H = hsv[..., 0] S = hsv[..., 1] V = hsv[..., 2] C = S H_ = H / 60. X = C * (1 - np.abs(H_ % 2 - 1)) Z = np.zeros_like(H) vals = [[Z, X, C], [Z, C, X], [X, C, Z], [C, X, Z], [C, Z, X], [X, Z, C]] for i in range(6): ind = np.where((i <= H_) & (H_ < (i + 1))) out[..., 0][ind] = (V - C)[ind] + vals[i][0][ind] out[..., 1][ind] = (V - C)[ind] + vals[i][1][ind] out[..., 2][ind] = (V - C)[ind] + vals[i][2][ind] out[np.where(max_v == min_v)] = 0 out = np.clip(out, 0, 1) out = (out * 255).astype(np.uint8) return out img = cv2.imread("imori.jpg").astype(np.float) #hsv = BGR2HSV(img) hsv = rgb2hsv(img) bs.show_img(hsv) out = HSV2BGR(img, hsv) bs.show_img(out)
out_v = np.clip(out_v, 0, 255) out_v = out_v[pad:pad + H, pad:pad + W].astype(np.uint8) out_h = np.clip(out_h, 0, 255) out_h = out_h[pad:pad + H, pad:pad + W].astype(np.uint8) return out_v, out_h def pool2d(_img, w_size=8): out = img.copy() h, w, c = _img.shape nh = int(h / w_size) nw = int(w / w_size) for y in range(nh): for x in range(nw): for c_i in range(c): out[w_size * y:w_size * (y + 1), w_size * x:w_size * (x + 1), c_i] = np.max(_img[w_size * y:w_size * (y + 1), w_size * x:w_size * (x + 1), c_i]) return out img = cv2.imread("imori.jpg") img_gray = bs.conv_to_gray(img) q_img_v, q_img_h = sobel_filter(img_gray) bs.show_img(q_img_v) bs.show_img(q_img_h)
for x in range(W): for c_i in range(C): out[pad + y, pad + x, c_i] = np.mean(tmp[y:y + K_size, x:x + K_size, c_i]) out = np.clip(out, 0, 255) out = out[pad:pad + H, pad:pad + W].astype(np.uint8) return out def pool2d(_img, w_size=8): out = img.copy() h, w, c = _img.shape nh = int(h / w_size) nw = int(w / w_size) for y in range(nh): for x in range(nw): for c_i in range(c): out[w_size * y:w_size * (y + 1), w_size * x:w_size * (x + 1), c_i] = np.max(_img[w_size * y:w_size * (y + 1), w_size * x:w_size * (x + 1), c_i]) return out img = cv2.imread("imori.jpg") q_img = mean_filter(img) bs.show_img(q_img)
def Otsu_binarize(img): img = img.astype(np.float) thres_set = range(255) argmin_thr = 0 max_sigma_b = None for thr in thres_set: c_1 = np.array(img[img <= thr]) c_2 = np.array(img[img > thr]) w_1 = len(c_1) w_2 = len(c_2) if (len(c_1) == 0 or len(c_2) == 0): continue else: tmp_sigma_b = w_1 * w_2 * np.power(np.mean(c_1) - np.mean(c_2), 2) / np.power((w_1 + w_2), 2) if (max_sigma_b is None or max_sigma_b < tmp_sigma_b): argmin_thr = thr max_sigma_b = tmp_sigma_b return binarize(img.astype(np.uint8), argmin_thr), argmin_thr img = cv2.imread("imori.jpg").astype(np.float) img_gray = conv_to_gray(img) img_bin, thr = Otsu_binarize(img_gray) print("thr:", thr) bs.show_img(img_bin)