-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplot_contour_mask.py
More file actions
68 lines (58 loc) · 1.79 KB
/
plot_contour_mask.py
File metadata and controls
68 lines (58 loc) · 1.79 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import numpy as np
import matplotlib.pyplot as plt
import sys
import os
import time
import imageio
import cv2
import scipy.misc
from PIL.Image import fromarray as toimage
import argparse
sys.path.append(os.path.join("./"))
from base import plot2d
import logging
logging.getLogger('matplotlib').setLevel(logging.ERROR)
if __name__ == '__main__':
argvs = sys.argv
parser = argparse.ArgumentParser()
parser.add_argument("--dir", dest="dir", default="./")
parser.add_argument("--pxyz", dest="point",
default=[0.0, 0.0, 0.0], type=float, nargs=3)
opt = parser.parse_args()
print(opt, argvs)
px = np.linspace(-1, 1, 100) * 200 + 50
py = np.linspace(-1, 1, 200) * 150 - 50
mesh = np.meshgrid(py, px)
surf = mesh[0]**2 / 1000 + mesh[1]**2 / 400
data = np.array(toimage(surf))
pts = [
[-100, -50],
[-250, +275],
[+150, +200],
[+50, -100]
]
pxy = [np.array(v) for v in pts]
pxy.append(pxy[0])
pxy = np.array(pxy)
obj = plot2d()
obj.contourf_div(mesh, surf)
obj.axs.plot(pxy[:, 0], pxy[:, 1])
obj.SavePng()
nxy = []
for xy in pxy:
nxy.append(obj.sxy_to_nxy(mesh, xy))
print(nxy[-1])
original_frame = surf
(x, y, w, h) = cv2.boundingRect(np.array(nxy))
print(x, y, w, h)
pts = np.array(nxy) - np.array(nxy).min(axis=0)
mask = np.zeros(original_frame.shape, np.uint8)
cv2.drawContours(mask, [pts], -1, (255, 255, 255), -1, cv2.LINE_AA)
mask = cv2.fillConvexPoly(original_frame, np.array(
pts, "int32"), color=(255, 255, 255))
result = original_frame * mask
res_data = np.asanyarray(result)
print(res_data)
cv2.imwrite(obj.tempname + "-xxx.png", result)
obj.new_2Dfig()
obj.contourf_div(mesh, mask, pngname=obj.tempname)