add reverse mask

This commit is contained in:
Qing 2024-01-30 13:30:43 +08:00
parent e5f71ae535
commit cdac68a9a9
5 changed files with 34 additions and 17 deletions

View File

@ -354,25 +354,29 @@ def concat_alpha_channel(rgb_np_img, alpha_channel) -> np.ndarray:
def adjust_mask(mask: np.ndarray, kernel_size: int, operate): def adjust_mask(mask: np.ndarray, kernel_size: int, operate):
# fronted brush color "ffcc00bb"
# kernel_size = kernel_size*2+1 # kernel_size = kernel_size*2+1
mask[mask >= 127] = 255 mask[mask >= 127] = 255
mask[mask < 127] = 0 mask[mask < 127] = 0
# fronted brush color "ffcc00bb"
kernel = cv2.getStructuringElement( if operate == "reverse":
cv2.MORPH_ELLIPSE, (2 * kernel_size + 1, 2 * kernel_size + 1) mask = 255 - mask
)
if operate == "expand":
mask = cv2.dilate(
mask,
kernel,
iterations=1,
)
else: else:
mask = cv2.erode( kernel = cv2.getStructuringElement(
mask, cv2.MORPH_ELLIPSE, (2 * kernel_size + 1, 2 * kernel_size + 1)
kernel,
iterations=1,
) )
if operate == "expand":
mask = cv2.dilate(
mask,
kernel,
iterations=1,
)
else:
mask = cv2.erode(
mask,
kernel,
iterations=1,
)
res_mask = np.zeros((mask.shape[0], mask.shape[1], 4), dtype=np.uint8) res_mask = np.zeros((mask.shape[0], mask.shape[1], 4), dtype=np.uint8)
res_mask[mask > 128] = [255, 203, 0, int(255 * 0.73)] res_mask[mask > 128] = [255, 203, 0, int(255 * 0.73)]
res_mask = cv2.cvtColor(res_mask, cv2.COLOR_BGRA2RGBA) res_mask = cv2.cvtColor(res_mask, cv2.COLOR_BGRA2RGBA)

View File

@ -304,12 +304,12 @@ class SwitchModelRequest(BaseModel):
name: str name: str
AdjustMaskOperate = Literal["expand", "shrink"] AdjustMaskOperate = Literal["expand", "shrink", "reverse"]
class AdjustMaskRequest(BaseModel): class AdjustMaskRequest(BaseModel):
mask: str = Field( mask: str = Field(
..., description="base64 encoded mask. 255 means area to do inpaint" ..., description="base64 encoded mask. 255 means area to do inpaint"
) )
operate: AdjustMaskOperate = Field(..., description="expand or shrink") operate: AdjustMaskOperate = Field(..., description="expand/shrink/reverse")
kernel_size: int = Field(5, description="Kernel size for expanding mask") kernel_size: int = Field(5, description="Kernel size for expanding mask")

View File

@ -13,3 +13,5 @@ def test_adjust_mask():
cv2.imwrite(str(save_dir / "adjust_mask_expand.png"), res_mask) cv2.imwrite(str(save_dir / "adjust_mask_expand.png"), res_mask)
res_mask = adjust_mask(mask, 20, "shrink") res_mask = adjust_mask(mask, 20, "shrink")
cv2.imwrite(str(save_dir / "adjust_mask_shrink.png"), res_mask) cv2.imwrite(str(save_dir / "adjust_mask_shrink.png"), res_mask)
res_mask = adjust_mask(mask, 20, "reverse")
cv2.imwrite(str(save_dir / "adjust_mask_reverse.png"), res_mask)

View File

@ -839,6 +839,17 @@ const DiffusionOptions = () => {
Shrink Shrink
</div> </div>
</Button> </Button>
<Button
variant="outline"
className="p-1 h-8"
onClick={() => adjustMask("reverse")}
disabled={isProcessing}
>
<div className="flex items-center gap-1 select-none">
Reverse
</div>
</Button>
</div> </div>
<Button <Button

View File

@ -126,4 +126,4 @@ export enum PowerPaintTask {
outpainting = "outpainting", outpainting = "outpainting",
} }
export type AdjustMaskOperate = "expand" | "shrink" export type AdjustMaskOperate = "expand" | "shrink" | "reverse"