本文整理匯總了Python中skimage.draw.polygon方法的典型用法代碼示例。如果您正苦於以下問題:Python draw.polygon方法的具體用法?Python draw.polygon怎麽用?Python draw.polygon使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類skimage.draw
的用法示例。
在下文中一共展示了draw.polygon方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_labels
# 需要導入模塊: from skimage import draw [as 別名]
# 或者: from skimage.draw import polygon [as 別名]
def get_labels(contours, shape, slices):
z = [np.around(s.ImagePositionPatient[2], 1) for s in slices]
pos_r = slices[0].ImagePositionPatient[1]
spacing_r = slices[0].PixelSpacing[1]
pos_c = slices[0].ImagePositionPatient[0]
spacing_c = slices[0].PixelSpacing[0]
label_map = np.zeros(shape, dtype=np.float32)
for con in contours:
num = ROI_ORDER.index(con['name']) + 1
for c in con['contours']:
nodes = np.array(c).reshape((-1, 3))
assert np.amax(np.abs(np.diff(nodes[:, 2]))) == 0
z_index = z.index(np.around(nodes[0, 2], 1))
r = (nodes[:, 1] - pos_r) / spacing_r
c = (nodes[:, 0] - pos_c) / spacing_c
rr, cc = polygon(r, c)
label_map[z_index, rr, cc] = num
return label_map
示例2: annToRLE
# 需要導入模塊: from skimage import draw [as 別名]
# 或者: from skimage.draw import polygon [as 別名]
def annToRLE(self, ann):
"""
Convert annotation which can be polygons, uncompressed RLE to RLE.
:return: binary mask (numpy 2D array)
"""
t = self.imgs[ann['image_id']]
h, w = t['height'], t['width']
segm = ann['segmentation']
if type(segm) == list:
# polygon -- a single object might consist of multiple parts
# we merge all parts into one mask rle code
rles = mask.frPyObjects(segm, h, w)
rle = mask.merge(rles)
elif type(segm['counts']) == list:
# uncompressed RLE
rle = mask.frPyObjects(segm, h, w)
else:
# rle
rle = ann['segmentation']
return rle
示例3: rectangle
# 需要導入模塊: from skimage import draw [as 別名]
# 或者: from skimage.draw import polygon [as 別名]
def rectangle(size = (4,2), layer = 0):
"""Generate rectangle geometry.
Parameters
----------
size : tuple
Width and height of rectangle.
layer : int, array-like[2], or set
Specific layer(s) to put polygon geometry on.
Returns
-------
A Device with a single rectangle in it
"""
D = Device(name = 'rectangle')
points = [[size[0], size[1]], [size[0], 0], [0, 0], [0, size[1]]]
D.add_polygon(points, layer = layer)
return D
示例4: bbox
# 需要導入模塊: from skimage import draw [as 別名]
# 或者: from skimage.draw import polygon [as 別名]
def bbox(bbox = [(-1,-1),(3,4)], layer = 0):
""" Creates a bounding box rectangle from coordinates, to allow
creation of a rectangle bounding box directly form another shape.
Parameters
----------
bbox : list of tuples
Coordinates of the box [(x1,y1),(x2,y2)].
layer : int, array-like[2], or set
Specific layer(s) to put polygon geometry on.
Returns
-------
A Device with a single rectangle in it
Examples
--------
>>> D = pg.bbox(anothershape.bbox)
"""
D = Device(name = 'bbox')
(a,b),(c,d) = bbox
points = ((a,b), (c,b), (c,d), (a,d))
D.add_polygon(points, layer = layer)
return D
示例5: cross
# 需要導入模塊: from skimage import draw [as 別名]
# 或者: from skimage.draw import polygon [as 別名]
def cross(length = 10, width = 3, layer = 0):
"""Generates a right-angle cross (+ shape, symmetric) from two
rectangles of specified length and width.
Parameters
----------
length : float
Length of the cross from one end to the other.
width : float
Width of the arms of the cross.
layer : int, array-like[2], or set
Specific layer(s) to put polygon geometry on.
Returns
-------
A Device with a cross in it
"""
D = Device(name = 'cross')
R = rectangle(size = (width, length), layer = layer)
r1 = D.add_ref(R).rotate(90)
r2 = D.add_ref(R)
r1.center = (0,0)
r2.center = (0,0)
return D
示例6: circle
# 需要導入模塊: from skimage import draw [as 別名]
# 或者: from skimage.draw import polygon [as 別名]
def circle(radius = 10, angle_resolution = 2.5, layer = 0):
"""Generate a circle geometry.
Parameters
----------
radius : float
Radius of the circle.
angle_resolution : float
Resolution of the curve of the ring (# of degrees per point).
layer : int, array-like[2], or set
Specific layer(s) to put polygon geometry on.
Returns
-------
A Device with an circle polygon in it
"""
D = Device(name = 'circle')
t = np.linspace(0, 360, int(np.ceil(360/angle_resolution) + 1))*pi/180
xpts = (radius*cos(t)).tolist()
ypts = (radius*sin(t)).tolist()
D.add_polygon(points = (xpts,ypts), layer = layer)
return D
示例7: straight
# 需要導入模塊: from skimage import draw [as 別名]
# 或者: from skimage.draw import polygon [as 別名]
def straight(size = (4,2), layer = 0):
"""Generates a rectangular wire geometry with ports on the length edges.
Parameters
----------
size : tuple
The length and width of the rectangle.
layer : int, array-like[2], or set
Specific layer(s) to put polygon geometry on.
Returns
-------
A Device with an rectangle polygon in it, and two ports (`1` and `2`) on either end
Notes
-----
Ports are included on both sides of the length edge (i.e. size[0]) of the geometry.
"""
D = Device(name = 'wire')
points = [[size[0], size[1]], [size[0], 0], [0, 0], [0, size[1]]]
D.add_polygon(points, layer = layer)
D.add_port(name = 1, midpoint = (size[0]/2, size[1]), width = size[0], orientation = 90)
D.add_port(name = 2, midpoint = (size[0]/2, 0), width = size[0], orientation = -90)
return D
示例8: cutout
# 需要導入模塊: from skimage import draw [as 別名]
# 或者: from skimage.draw import polygon [as 別名]
def cutout(pageimg, coordstring, scale=1, rect=False):
coords = [p.split(",") for p in coordstring.split()]
coords = np.array([(int(scale * int(c[1])), int(scale * int(c[0])))
for c in coords])
if rect:
return pageimg[min(c[0] for c in coords):max(c[0] for c in coords),
min(c[1] for c in coords):max(c[1] for c in coords)]
rr, cc = polygon(coords[:, 0], coords[:, 1], pageimg.shape)
offset = (min([x[0] for x in coords]), min([x[1] for x in coords]))
box = np.ones(
(max([x[0] for x in coords]) - offset[0],
max([x[1] for x in coords]) - offset[1],
) + ((pageimg.shape[-1],) if len(pageimg.shape) == 3 else ()),
dtype=pageimg.dtype) * 255
box[rr - offset[0], cc - offset[1]] = pageimg[rr, cc]
return box
示例9: __init__
# 需要導入模塊: from skimage import draw [as 別名]
# 或者: from skimage.draw import polygon [as 別名]
def __init__(self, path, size=(226, 226), transform=lambda x: x):
self.transform = transform
with Image.open(path + ".tif") as stack:
frames = []
for img in ImageSequence.Iterator(stack):
frame = torch.tensor(np.array(img).astype(float))
frames.append(frame.unsqueeze(0))
self.raw_image = torch.cat(frames, dim=0)
rois = read_roi_zip(path + ".roi.zip")
self.rois = [
torch.tensor(zip(*polygon(
roi[1]["x"], roi[1]["y"],
shape=(self.raw_image.size(1),
self.raw_image.size(2))
)), dtype=torch.long)
for roi in rois
]
示例10: area_of_intersection
# 需要導入模塊: from skimage import draw [as 別名]
# 或者: from skimage.draw import polygon [as 別名]
def area_of_intersection(det_x, det_y, gt_x, gt_y):
"""
This helper calculates the area of intersection.
"""
if approx_area_of_intersection(det_x, det_y, gt_x, gt_y) > 1: #only proceed if it passes the approximation test
ymax = np.maximum(np.max(det_y), np.max(gt_y)) + 1
xmax = np.maximum(np.max(det_x), np.max(gt_x)) + 1
bin_mask = np.zeros((ymax, xmax))
det_bin_mask = np.zeros_like(bin_mask)
gt_bin_mask = np.zeros_like(bin_mask)
rr, cc = polygon(det_y, det_x)
det_bin_mask[rr, cc] = 1
rr, cc = polygon(gt_y, gt_x)
gt_bin_mask[rr, cc] = 1
final_bin_mask = det_bin_mask + gt_bin_mask
inter_map = np.where(final_bin_mask == 2, 1, 0)
inter = np.sum(inter_map)
return inter
# return np.round(inter, 2)
else:
return 0
示例11: coord2_img
# 需要導入模塊: from skimage import draw [as 別名]
# 或者: from skimage.draw import polygon [as 別名]
def coord2_img(mean_x, mean_y, length, aspect_ratio, rotate, img_size):
W2 = np.array([[np.cos(rotate), np.sin(rotate)], [-np.sin(rotate), np.cos(rotate)]])
height = length
width = height / aspect_ratio
c = np.array([[-height/2, -width/2], [height/2, -width/2], [height/2, width/2], [-height/2, width/2]])
c = (W2 @ c.T).T + np.array([mean_y, mean_x])
img = np.zeros((img_size, img_size), dtype=np.uint8)
rr, cc = polygon(c[:, 0], c[:, 1])
index = (rr >= 0) * (rr < img_size) * (cc >= 0) * (cc < img_size)
img[rr[index], cc[index]] = 255
return img
示例12: iou
# 需要導入模塊: from skimage import draw [as 別名]
# 或者: from skimage.draw import polygon [as 別名]
def iou(self, bb, angle_threshold=np.pi/6):
if abs(self.angle - bb.angle) % np.pi > angle_threshold:
return 0
rr1, cc1 = self.polygon_coords()
rr2, cc2 = polygon(bb.points[:, 0], bb.points[:, 1])
try:
r_max = max(rr1.max(), rr2.max()) + 1
c_max = max(cc1.max(), cc2.max()) + 1
except:
return 0
canvas = np.zeros((r_max, c_max))
canvas[rr1, cc1] += 1
canvas[rr2, cc2] += 1
union = np.sum(canvas > 0)
if union == 0:
return 0
intersection = np.sum(canvas == 2)
return intersection/union
示例13: scale_regions
# 需要導入模塊: from skimage import draw [as 別名]
# 或者: from skimage.draw import polygon [as 別名]
def scale_regions(regions: Sequence[Tuple[List, List]],
scale: Union[float, Tuple[float, float]]) -> Sequence[Tuple[List, List]]:
"""
Scales baselines/polygon coordinates by a certain factor.
Args:
lines (Sequence): List of tuples containing the baseline and it's
polygonization.
scale (float or tuple of floats): Scaling factor
"""
if isinstance(scale, float):
scale = (scale, scale)
scaled_regions = []
for region in regions:
scaled_regions.append((np.array(region) * scale).astype('uint').tolist())
return scaled_regions
示例14: scale_polygonal_lines
# 需要導入模塊: from skimage import draw [as 別名]
# 或者: from skimage.draw import polygon [as 別名]
def scale_polygonal_lines(lines: Sequence[Tuple[List, List]], scale: Union[float, Tuple[float, float]]) -> Sequence[Tuple[List, List]]:
"""
Scales baselines/polygon coordinates by a certain factor.
Args:
lines (Sequence): List of tuples containing the baseline and it's
polygonization.
scale (float or tuple of floats): Scaling factor
"""
if isinstance(scale, float):
scale = (scale, scale)
scaled_lines = []
for line in lines:
bl, pl = line
scaled_lines.append(((np.array(bl) * scale).astype('int').tolist(),
(np.array(pl) * scale).astype('int').tolist()))
return scaled_lines
示例15: _test_intersect
# 需要導入模塊: from skimage import draw [as 別名]
# 或者: from skimage.draw import polygon [as 別名]
def _test_intersect(bp, uv, bs):
"""
Returns the intersection points of a ray with direction `uv` from
`bp` with a polygon `bs`.
"""
u = bp - np.roll(bs, 2)
v = bs - np.roll(bs, 2)
points = []
for dir in ((1,-1), (-1,1)):
w = (uv * dir * (1,-1))[::-1]
z = np.dot(v, w)
t1 = np.cross(v, u) / z
t2 = np.dot(u, w) / z
t1 = t1[np.logical_and(t2 >= 0.0, t2 <= 1.0)]
points.extend(bp + (t1[np.where(t1 >= 0)[0].min()] * (uv * dir)))
return np.array(points)