-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplasty.py
More file actions
41 lines (33 loc) · 1.19 KB
/
plasty.py
File metadata and controls
41 lines (33 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python
import cv2 as cv
import click
import numpy as np
@click.command()
@click.option('--filename',
type=click.Path(exists=True),
required=True)
def main(filename):
img = cv.imread(filename)
cv.imshow('original', img)
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
img_gray = cv.GaussianBlur(img_gray, (5, 5), 2)
kernel = np.ones((3, 3), np.uint8)
eroded = cv.erode(img_gray, kernel, iterations=4)
cv.imshow('eroded', eroded)
_, filled = cv.threshold(eroded, 110, 255, cv.THRESH_BINARY)
_, empty = cv.threshold(eroded, 50, 255, cv.THRESH_BINARY_INV)
kernel = np.ones((7, 7), np.uint8)
filled = cv.dilate(filled, kernel, iterations=1)
empty = cv.dilate(empty, np.ones((3, 3), np.uint8), iterations=1)
cv.imshow('filled mask', filled)
cv.imshow('empty mask', empty)
cv.imshow('gray', img_gray)
img_filled = cv.bitwise_and(img, img, mask=filled)
img_empty = cv.bitwise_and(img, img, mask=empty)
cv.imshow('filled', img_filled)
cv.imwrite('filled.png', img_filled)
cv.imshow('empty', img_empty)
cv.imwrite('empty.png', img_empty)
cv.waitKey(0)
if __name__ == '__main__':
main()