Update README.md
Browse files
README.md
CHANGED
|
@@ -95,6 +95,43 @@ matte = (matte * 255).astype(np.uint8)
|
|
| 95 |
Image.fromarray(matte).save("alpha_matte.png")
|
| 96 |
|
| 97 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
|
| 100 |
---
|
|
|
|
| 95 |
Image.fromarray(matte).save("alpha_matte.png")
|
| 96 |
|
| 97 |
```
|
| 98 |
+
## Visualization
|
| 99 |
+
|
| 100 |
+
```python
|
| 101 |
+
import numpy as np
|
| 102 |
+
from PIL import Image
|
| 103 |
+
|
| 104 |
+
def combined_display(image, matte):
|
| 105 |
+
# calculate display resolution
|
| 106 |
+
w, h = image.width, image.height
|
| 107 |
+
rw, rh = 800, int(h * 800 / (3 * w))
|
| 108 |
+
|
| 109 |
+
# obtain predicted foreground
|
| 110 |
+
image = np.asarray(image)
|
| 111 |
+
if len(image.shape) == 2:
|
| 112 |
+
image = image[:, :, None]
|
| 113 |
+
if image.shape[2] == 1:
|
| 114 |
+
image = np.repeat(image, 3, axis=2)
|
| 115 |
+
elif image.shape[2] == 4:
|
| 116 |
+
image = image[:, :, 0:3]
|
| 117 |
+
matte = np.repeat(np.asarray(matte)[:, :, None], 3, axis=2) / 255
|
| 118 |
+
foreground = image * matte + np.full(image.shape, 255) * (1 - matte)
|
| 119 |
+
|
| 120 |
+
# combine image, foreground, and alpha into one line
|
| 121 |
+
combined = np.concatenate((image, foreground, matte * 255), axis=1)
|
| 122 |
+
combined = Image.fromarray(np.uint8(combined)).resize((rw, rh))
|
| 123 |
+
return combined
|
| 124 |
+
|
| 125 |
+
# visualize all images
|
| 126 |
+
image_names = os.listdir(input_folder)
|
| 127 |
+
for image_name in image_names:
|
| 128 |
+
matte_name = image_name.split('.')[0] + '.png'
|
| 129 |
+
image = Image.open(os.path.join(input_folder, image_name))
|
| 130 |
+
matte = Image.open(os.path.join(output_folder, matte_name))
|
| 131 |
+
display(combined_display(image, matte))
|
| 132 |
+
print(image_name, '\n')
|
| 133 |
+
|
| 134 |
+
```
|
| 135 |
|
| 136 |
|
| 137 |
---
|