diff --git a/README.md b/README.md index 992cc2c..9f89c18 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,8 @@ This filter allows you to blur the image based on a depth map. You can use this | focal_depth | `1.0` | Float | The focal depth of the blur. `1.0` is the closest, `0.0` is the farthest. | | focus_spread | `1.0` | Float | The spread of the area of focus. A larger value makes more of the image sharp. | | steps | `5` | Integer | The number of steps to use when blurring the image. Higher numbers are slower. | - +| focal_range | `0.0` | Float | `1.0` means all areas clear, `0.0` means only focal point is clear. | +| mask_blur | `1` | Integer | Mask blur strength (1 to 127).`1` means no blurring. | ### Apply LUT diff --git a/nodes.py b/nodes.py index c66c6a3..816b54d 100644 --- a/nodes.py +++ b/nodes.py @@ -337,35 +337,63 @@ class ProPostDepthMapBlur: "min": 1, "max": 32, }), + "focal_range": ("FLOAT", { + "default": 0.0, + "min": 0.0, + "max": 1.0, + "step": 0.01 + }), + "mask_blur": ("INT", { + "default": 1, + "min": 1, + "max": 127, + "step": 2 + }), }, } - RETURN_TYPES = ("IMAGE",) + RETURN_TYPES = ("IMAGE","MASK") RETURN_NAMES = () FUNCTION = "depthblur_image" - + DESCRIPTION = """ + blur_strength: Represents the blur strength. This parameter controls the overall intensity of the blur effect; the higher the value, the more blurred the image becomes. + + focal_depth: Represents the focal depth. This parameter is used to determine which depth level in the image should remain sharp, while other levels are blurred based on depth differences. + + focus_spread: Represents the focus spread range. This parameter controls the size of the blur transition area near the focal depth; the larger the value, the wider the transition area, and the smoother the blur effect spreads around the focus. + + steps: Represents the number of steps in the blur process. This parameter determines the calculation precision of the blur effect; the more steps, the finer the blur effect, but this also increases the computational load. + + focal_range: Represents the focal range. This parameter is used to adjust the depth range within the focal depth that remains sharp; the larger the value, the wider the area around the focal depth that remains sharp. + + mask_blur: Represents the mask blur strength for blurring the depth map. This parameter controls the intensity of the depth map's blur treatment, used for preprocessing the depth map before calculating the final blur effect, to achieve a more natural blur transition. + """ #OUTPUT_NODE = False CATEGORY = "Pro Post/Blur Effects" - def depthblur_image(self, image: torch.Tensor, depth_map: torch.Tensor, blur_strength: float, focal_depth: float, focus_spread:float, steps: int): + def depthblur_image(self, image: torch.Tensor, depth_map: torch.Tensor, blur_strength: float, focal_depth: float, focus_spread:float, steps: int, focal_range: float, mask_blur: int): batch_size, height, width, _ = image.shape - result = torch.zeros_like(image) + image_result = torch.zeros_like(image) + mask_result = torch.zeros((batch_size, height, width), dtype=torch.float32) for b in range(batch_size): tensor_image = image[b].numpy() tensor_image_depth = depth_map[b].numpy() # Apply blur - blur_image = self.apply_depthblur(tensor_image, tensor_image_depth, blur_strength, focal_depth, focus_spread, steps) + blur_image,depth_mask = self.apply_depthblur(tensor_image, tensor_image_depth, blur_strength, focal_depth, focus_spread, steps, focal_range, mask_blur) - tensor = torch.from_numpy(blur_image).unsqueeze(0) - result[b] = tensor + tensor_image = torch.from_numpy(blur_image).unsqueeze(0) + tensor_mask = torch.from_numpy(depth_mask).unsqueeze(0) - return (result,) + image_result[b] = tensor_image + mask_result[b] = tensor_mask - def apply_depthblur(self, image, depth_map, blur_strength, focal_depth, focus_spread, steps): + return (image_result,mask_result) + + def apply_depthblur(self, image, depth_map, blur_strength, focal_depth, focus_spread, steps, focal_range, mask_blur): # Normalize the input image if needed needs_normalization = image.max() > 1 if needs_normalization: @@ -383,6 +411,13 @@ class ProPostDepthMapBlur: depth_mask = np.abs(depth_map_resized - focal_depth) depth_mask = np.clip(depth_mask / np.max(depth_mask), 0, 1) + # Process the depth_mask + depth_mask[depth_mask < focal_range] = 0 + depth_mask[depth_mask >= focal_range] = (depth_mask[depth_mask >= focal_range] - focal_range) / (1 - focal_range) + + # Apply mask blur + depth_mask = cv2.GaussianBlur(depth_mask, (mask_blur, mask_blur), 0) + # Generate blurred versions of the image blurred_images = processing_utils.generate_blurred_images(image, blur_strength, steps, focus_spread) @@ -393,7 +428,7 @@ class ProPostDepthMapBlur: if needs_normalization: final_image = np.clip(final_image * 255, 0, 255).astype(np.uint8) - return final_image + return final_image, depth_mask class ProPostApplyLUT: