def function(image, **kwargs): from intrinsic.algorithm.grosse2009 import intrinsic image = srgb_to_rgb(pil_to_numpy(image)) * 255.0 mask = np.ones((image.shape[0:2]), dtype=bool) s, r = intrinsic.color_retinex(image, mask, **kwargs) r = image / np.clip(s, 1e-3, float('inf'))[:, :, np.newaxis] return r, s
def function(image, **kwargs): from intrinsic.algorithm.bell2014.solver import IntrinsicSolver from intrinsic.algorithm.bell2014.input import IntrinsicInput solver = IntrinsicSolver( input=IntrinsicInput(image_rgb=srgb_to_rgb(pil_to_numpy(image)), ), params=parameters, ) r, s, decomposition = solver.solve() return r, s
def function(image, **kwargs): from intrinsic.algorithm.bell2014.solver import IntrinsicSolver from intrinsic.algorithm.bell2014.input import IntrinsicInput solver = IntrinsicSolver( input=IntrinsicInput( image_rgb=srgb_to_rgb(pil_to_numpy(image)), ), params=parameters, ) r, s, decomposition = solver.solve() return r, s
def sample_photo_intrinsic_points(photo, width=2048, min_separation=Decimal('0.07'), edge_window=4, side_thresh=40, avoid_existing_points=True, **kwargs): """ Populate a photograph with evenly spaced samples that avoid image edges (canny), high gradient (gaussian) regions, and very dark regions """ # detect edges and compute gradients image = pil_to_numpy(photo.open_image(width=width)) image_lab = rgb2lab(image) rows, cols, _ = image.shape #image_sobel = sobel(rgb2gray(image)) image_canny = (canny(image[:, :, 0], sigma=2.0) | canny(image[:, :, 1], sigma=2.0) | canny(image[:, :, 2], sigma=2.0)) # avoid existing points if avoid_existing_points: existing_points = [ (x * cols, y * rows) for (x, y) in IntrinsicPoint.objects.filter( photo=photo, min_separation=min_separation).values_list( 'x', 'y') ] else: existing_points = [] # min radius r = max(1, int(np.sqrt(cols**2 + rows**2) * float(min_separation))) r_sq = r**2 # filter: not near the edge, on an image edge, and not on a high gradient def point_filter(p): # not near any existing points for q in existing_points: if (q[0] - p[0])**2 + (q[1] - p[1])**2 < r_sq: return False c, r = int(p[0]), int(p[1]) # cannot be near the edge of the image if (c < side_thresh or r < side_thresh or cols - c < side_thresh or rows - r < side_thresh): return False # cannot be super bright or dark if not (0.02 < np.mean(image[r, c, :]) < 0.98): return False # cannot be on an edge r0, r1 = max(r - edge_window, 0), min(r + edge_window + 1, rows) c0, c1 = max(c - edge_window, 0), min(c + edge_window + 1, cols) if np.any(image_canny[r0:r1, c0:c1]): return False # chromaticity coefficient of variation cannot be too high # (cv = std / mean) chroma_cv = 0.5 * (variation(image_lab[r0:r1, c0:c1, 1], axis=None) + variation(image_lab[r0:r1, c0:c1, 2], axis=None)) return chroma_cv < 0.50 # poisson disk sampling samples = sample_poisson_uniform(width=cols, height=rows, r=r, k=30, n_seeds=10000, point_filter=point_filter) # store in database intrinsic_points = [] for p in samples: x = float(p[0]) / cols y = float(p[1]) / rows sRGB = '%02x%02x%02x' % photo.get_pixel(x, y, width='300') intrinsic_points.append( IntrinsicPoint( photo=photo, x=x, y=y, sRGB=sRGB, min_separation=min_separation, )) if intrinsic_points: IntrinsicPoint.objects.bulk_create(intrinsic_points)
def sample_photo_intrinsic_points( photo, width=2048, min_separation=Decimal('0.07'), edge_window=4, side_thresh=40, avoid_existing_points=True, **kwargs): """ Populate a photograph with evenly spaced samples that avoid image edges (canny), high gradient (gaussian) regions, and very dark regions """ # detect edges and compute gradients image = pil_to_numpy(photo.open_image(width=width)) image_lab = rgb2lab(image) rows, cols, _ = image.shape #image_sobel = sobel(rgb2gray(image)) image_canny = ( canny(image[:, :, 0], sigma=2.0) | canny(image[:, :, 1], sigma=2.0) | canny(image[:, :, 2], sigma=2.0) ) # avoid existing points if avoid_existing_points: existing_points = [ (x * cols, y * rows) for (x, y) in IntrinsicPoint.objects .filter(photo=photo, min_separation=min_separation) .values_list('x', 'y') ] else: existing_points = [] # min radius r = max(1, int(np.sqrt(cols ** 2 + rows ** 2) * float(min_separation))) r_sq = r ** 2 # filter: not near the edge, on an image edge, and not on a high gradient def point_filter(p): # not near any existing points for q in existing_points: if (q[0] - p[0]) ** 2 + (q[1] - p[1]) ** 2 < r_sq: return False c, r = int(p[0]), int(p[1]) # cannot be near the edge of the image if (c < side_thresh or r < side_thresh or cols - c < side_thresh or rows - r < side_thresh): return False # cannot be super bright or dark if not (0.02 < np.mean(image[r, c, :]) < 0.98): return False # cannot be on an edge r0, r1 = max(r - edge_window, 0), min(r + edge_window + 1, rows) c0, c1 = max(c - edge_window, 0), min(c + edge_window + 1, cols) if np.any(image_canny[r0:r1, c0:c1]): return False # chromaticity coefficient of variation cannot be too high # (cv = std / mean) chroma_cv = 0.5 * ( variation(image_lab[r0:r1, c0:c1, 1], axis=None) + variation(image_lab[r0:r1, c0:c1, 2], axis=None) ) return chroma_cv < 0.50 # poisson disk sampling samples = sample_poisson_uniform( width=cols, height=rows, r=r, k=30, n_seeds=10000, point_filter=point_filter) # store in database intrinsic_points = [] for p in samples: x = float(p[0]) / cols y = float(p[1]) / rows sRGB = '%02x%02x%02x' % photo.get_pixel(x, y, width='300') intrinsic_points.append(IntrinsicPoint( photo=photo, x=x, y=y, sRGB=sRGB, min_separation=min_separation, )) if intrinsic_points: IntrinsicPoint.objects.bulk_create(intrinsic_points)
def function(image, **kwargs): image = srgb_to_rgb(pil_to_numpy(image)) return image, np.ones_like(image)
def function(image, **kwargs): image = srgb_to_rgb(pil_to_numpy(image)) s = np.clip(np.sum(image, axis=-1), 1e-3, float('inf')) r = image / s[:, :, np.newaxis] return r, s