def process(self): width = self.img.shape[1] height = self.img.shape[0] luminance = np.zeros((height, width), dtype='float32') pixel_count = 0 sigma = 0.0 for y, x in product(range(height), range(width)): if self.mask[y, x] == PIXEL_MASKED: c = self.img[y, x, :] luminance[y, x] = c[0] * 0.2126 + c[1] * 0.7152 + c[2] * 0.0722 sigma += math.log(luminance[y, x] + EPS) pixel_count += 1 sigma = math.exp(sigma / pixel_count) for y, x in product(range(height), range(width)): l = luminance[y, x] luminance[y, x] = math.pow(l, N_POW) / (math.pow(l, N_POW) + math.pow(sigma, N_POW)) # Apply bilateral filter for it in range(10): luminance = imfilter.bilateral_filter(luminance, 5.0, 1.0, 15) # Invert sigmoidal compression in Eq.11 of [Khan et al. 2006] self.depth = np.zeros((height, width)) dscale = (width + height) / 2.0 for y, x in product(range(height), range(width)): if self.mask[y, x] == PIXEL_MASKED: d = luminance[y, x] b = - math.pow(sigma, N_POW) * d / min(d - 1.0, -EPS) dp = math.pow(b, 1.0 / N_POW) self.depth[y, x] = dp * dscale # Solve for depth self._solve_depth2() sp.misc.imsave('result/depth_map.png', self.depth)
def estimate_zenith(self, image, mask): width = image.shape[1] height = image.shape[0] lum = np.zeros((height, width), dtype='float32') for y, x in product(range(height), range(width)): col = image[y,x,:] lum[y,x] = hdr.luminance(col[0], col[1], col[2]) for it in range(10): lum = imfilter.bilateral_filter(lum, 5.0, 1.0, 15) clip_img, clip_mask, clip_rect = self.clip_masked_region(lum, mask) num_rot = 64 clip_width = clip_img.shape[1] clip_height = clip_img.shape[0] rot_imgs = [None] * num_rot rot_masks = [None] * num_rot for i in range(num_rot): cx = clip_width / 2 cy = clip_height / 2 theta = - 360.0 * i / num_rot rot_imgs[i] = sp.ndimage.interpolation.rotate(clip_img, theta, reshape=False) rot_masks[i] = sp.ndimage.interpolation.rotate(clip_mask, theta, reshape=False) num_light = len(self.lights) num_cont = len(self.silhouette) for j in range(num_light): theta_ij = [] L_ij = [] for i in range(num_cont): l = self.lights[j] cx = clip_rect.x + clip_rect.width // 2 cy = clip_rect.y + clip_rect.height // 2 sx = self.silhouette[i].x - cx sy = self.silhouette[i].y - cy rot = math.atan2(sy, sx) rx = int(sx * math.cos(rot) - sy * math.sin(rot) + cx - clip_rect.x) ry = int(sx * math.sin(rot) + sy * math.cos(rot) + cy - clip_rect.y) rx = max(0, min(rx, clip_width - 1)) ry = max(0, min(ry, clip_height - 1)) rot_idx = int(l.phi * num_rot / (2.0 * math.pi)) while rot_idx < 0: rot_idx += num_rot rot_idx %= num_rot left_x = clip_width - 1 right_x = 0 for x in range(0, clip_width): if rot_masks[rot_idx][ry,x] > 0.5: left_x = min(x, left_x) right_x = max(x, right_x) if left_x > right_x: continue ext_x = left_x f1 = rot_imgs[rot_idx][ry,left_x-1] f2 = rot_imgs[rot_idx][ry,left_x+1] grad = (f2 - f1) * 0.5 for x in range(left_x, right_x): f1 = rot_imgs[rot_idx][ry,x-1] f2 = rot_imgs[rot_idx][ry,x+1] gg = (f2 - f1) * 0.5 if gg * grad < 0.0: ext_x = x break numer_mls = 0.0 denom_mls = 0.0 mid_x = (left_x + right_x) / 2.0 radius = (right_x - left_x) / 2.0 for x in range(left_x, right_x+1): if rot_masks[rot_idx][ry,x] > 0.5: dx = x - mid_x a = math.sqrt(radius * radius - dx * dx) l = lum[ry,x] numer_mls += a * l denom_mls += a * a ratio = numer_mls / (denom_mls + EPS) dx = abs(ext_x - mid_x) th = math.atan(math.sqrt(radius * radius - dx * dx) / (dx * ratio + EPS)) th = th * (1.0 if grad > 0.0 else -1.0) theta_ij.append(th) L_ij.append(rot_imgs[rot_idx][ry, ext_x]) numer = 0.0 denom = 0.0 for i in range(len(theta_ij)): numer += theta_ij[i] * L_ij[i] denom += L_ij[i] self.lights[j].theta = numer / (denom + EPS)
def estimate_zenith(self, image, mask): width = image.shape[1] height = image.shape[0] lum = np.zeros((height, width), dtype='float32') for y, x in product(range(height), range(width)): col = image[y, x, :] lum[y, x] = hdr.luminance(col[0], col[1], col[2]) for it in range(10): lum = imfilter.bilateral_filter(lum, 5.0, 1.0, 15) clip_img, clip_mask, clip_rect = self.clip_masked_region(lum, mask) num_rot = 64 clip_width = clip_img.shape[1] clip_height = clip_img.shape[0] rot_imgs = [None] * num_rot rot_masks = [None] * num_rot for i in range(num_rot): cx = clip_width / 2 cy = clip_height / 2 theta = -360.0 * i / num_rot rot_imgs[i] = sp.ndimage.interpolation.rotate(clip_img, theta, reshape=False) rot_masks[i] = sp.ndimage.interpolation.rotate(clip_mask, theta, reshape=False) num_light = len(self.lights) num_cont = len(self.silhouette) for j in range(num_light): theta_ij = [] L_ij = [] for i in range(num_cont): l = self.lights[j] cx = clip_rect.x + clip_rect.width // 2 cy = clip_rect.y + clip_rect.height // 2 sx = self.silhouette[i].x - cx sy = self.silhouette[i].y - cy rot = math.atan2(sy, sx) rx = int(sx * math.cos(rot) - sy * math.sin(rot) + cx - clip_rect.x) ry = int(sx * math.sin(rot) + sy * math.cos(rot) + cy - clip_rect.y) rx = max(0, min(rx, clip_width - 1)) ry = max(0, min(ry, clip_height - 1)) rot_idx = int(l.phi * num_rot / (2.0 * math.pi)) while rot_idx < 0: rot_idx += num_rot rot_idx %= num_rot left_x = clip_width - 1 right_x = 0 for x in range(0, clip_width): if rot_masks[rot_idx][ry, x] > 0.5: left_x = min(x, left_x) right_x = max(x, right_x) if left_x > right_x: continue ext_x = left_x f1 = rot_imgs[rot_idx][ry, left_x - 1] f2 = rot_imgs[rot_idx][ry, left_x + 1] grad = (f2 - f1) * 0.5 for x in range(left_x, right_x): f1 = rot_imgs[rot_idx][ry, x - 1] f2 = rot_imgs[rot_idx][ry, x + 1] gg = (f2 - f1) * 0.5 if gg * grad < 0.0: ext_x = x break numer_mls = 0.0 denom_mls = 0.0 mid_x = (left_x + right_x) / 2.0 radius = (right_x - left_x) / 2.0 for x in range(left_x, right_x + 1): if rot_masks[rot_idx][ry, x] > 0.5: dx = x - mid_x a = math.sqrt(radius * radius - dx * dx) l = lum[ry, x] numer_mls += a * l denom_mls += a * a ratio = numer_mls / (denom_mls + EPS) dx = abs(ext_x - mid_x) th = math.atan( math.sqrt(radius * radius - dx * dx) / (dx * ratio + EPS)) th = th * (1.0 if grad > 0.0 else -1.0) theta_ij.append(th) L_ij.append(rot_imgs[rot_idx][ry, ext_x]) numer = 0.0 denom = 0.0 for i in range(len(theta_ij)): numer += theta_ij[i] * L_ij[i] denom += L_ij[i] self.lights[j].theta = numer / (denom + EPS)