forked from mehrshadg/acf_paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_arrows.py
39 lines (35 loc) · 1.6 KB
/
make_arrows.py
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
import numpy as np
from skimage.measure import label, regionprops
from skimage.transform import rotate
from skimage import img_as_ubyte
import cv2
import math
import glob
import imageio
path = "/Users/Mehrshad/Desktop/final_figs"
mask_img = imageio.imread(f"{path}/mask.png")
mask = np.where(mask_img == 0)
label_img = label(mask_img)
regions = regionprops(label_img)
files = glob.glob(f"{path}/map.topo.*.png")
files.sort()
for file in files:
image = imageio.imread(file)
image[mask[0], mask[1], :] = 0
for region in regions:
min_row, min_col, max_row, max_col = region.bbox
rotated = img_as_ubyte(rotate(image[min_row: max_row, min_col: max_col, :],
angle=90-math.degrees(region.orientation), order=0))
_, thresh = cv2.threshold(cv2.cvtColor(rotated, cv2.COLOR_BGR2GRAY), 1, 255, cv2.THRESH_BINARY)
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
x, y, w, h = cv2.boundingRect(contours[-1])
crop = rotated[y:y + h, x:x + w, :]
cv2.imwrite(file.replace(".png", f".reg{region.label}.png").replace("map.topo", "arrow."), crop)
mask_img = cv2.imread(f"{path}/mask.png")
_, thresh = cv2.threshold(cv2.cvtColor(mask_img, cv2.COLOR_BGR2GRAY), 1, 255, cv2.THRESH_BINARY)
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
image = cv2.imread(f"{path}/nets.sh7.png")
for cnt in contours:
approx = cv2.approxPolyDP(cnt, 0.009 * cv2.arcLength(cnt, True), True)
cv2.drawContours(image, [approx], 0, (0, 0, 0), 5)
cv2.imwrite(f"{path}/arrow.nets.sh7.png", image)