如何从蒙版中绘制正确形状的多边形?
How can I draw a polygon in the right shape from a mask?
- 我有一个带有不同标记区域的面具,如附图所示。
- 用活动轮廓函数分割后,我想在活动轮廓点的合成点周围画一个多边形,但是多边形是逐行读取点,所以多边形绘制不正确,如图所示附图,多边形应该看起来更像一个圆形区域。
请问如何解决?
图片
- 面具
- 多边形
import numpy as np
from skimage.io import imread, imsave
import skimage.filters as filters
from skimage.segmentation import active_contour
from scipy.sparse import csr_matrix
import matplotlib.pyplot as plt
from skimage.draw import polygon
#---------------------------------
img = imread('image.jpg')
mask = imread('label.png')
def activeContour(img, mask, alpha=0.015, beta=10, gamma=0.001, iterations=500, max_step=0.5):
lb_list = np.unique(mask)
Result = np.zeros_like(img)
for lb in lb_list:
if(lb != 0):
s = np.where(mask == lb)
r = s[0].astype(np.uint8)
c = s[1].astype(np.uint8)
init = np.array([r, c]).T
#-------PreProcessing----------------------------
img2 = filters.gaussian(img, 3)
#------- Active Contour ------------------------
snake = active_contour(img2, init, alpha=alpha, beta=beta, gamma=gamma, coordinates='rc',
max_px_move=max_step, max_iterations=iterations)
#------------------------------------------------
vals = lb*np.ones_like(snake[:, 0],dtype=np.uint8)
label = csr_matrix((vals, (snake[:, 1].astype(np.uint8), snake[:, 0].astype(np.uint8))), shape=(img.shape[0], img.shape[1])).toarray()
Result = Result + label
return Result
res = activeContour(img, mask, alpha=0, beta= 10, gamma= 0.0000001)
labels = np.unique(res)
ctr = 25
for label in labels:
if label != 0:
img = np.zeros_like(img)
s = np.where(res == label)
r = s[0].astype(np.uint8)
c = s[1].astype(np.uint8)
rr, cc = polygon(r, c)
img[rr, cc] = 2*ctr
plt.imshow(img, cmap='gray')
plt.title('%d'%label)
plt.axis('off')
plt.show()
ctr+=1
感谢Juan for his help, the theoritical part of this answer is here
并且实施失败。
import numpy as np
def sortVertices(array):
s = np.where(array != 0)
yr = s[0].astype(np.uint8)
xc = s[1].astype(np.uint8)
center_xc = np.sum(xc)/xc.shape
center_yr = np.sum(yr)/yr.shape
theta = np.arctan2(yr-center_yr, xc-center_xc) * 180 / np.pi
indices = np.argsort(theta)
x = xc[indices]
y = yr[indices]
return x, y
- 我有一个带有不同标记区域的面具,如附图所示。
- 用活动轮廓函数分割后,我想在活动轮廓点的合成点周围画一个多边形,但是多边形是逐行读取点,所以多边形绘制不正确,如图所示附图,多边形应该看起来更像一个圆形区域。
请问如何解决?
图片
- 面具
- 多边形
import numpy as np
from skimage.io import imread, imsave
import skimage.filters as filters
from skimage.segmentation import active_contour
from scipy.sparse import csr_matrix
import matplotlib.pyplot as plt
from skimage.draw import polygon
#---------------------------------
img = imread('image.jpg')
mask = imread('label.png')
def activeContour(img, mask, alpha=0.015, beta=10, gamma=0.001, iterations=500, max_step=0.5):
lb_list = np.unique(mask)
Result = np.zeros_like(img)
for lb in lb_list:
if(lb != 0):
s = np.where(mask == lb)
r = s[0].astype(np.uint8)
c = s[1].astype(np.uint8)
init = np.array([r, c]).T
#-------PreProcessing----------------------------
img2 = filters.gaussian(img, 3)
#------- Active Contour ------------------------
snake = active_contour(img2, init, alpha=alpha, beta=beta, gamma=gamma, coordinates='rc',
max_px_move=max_step, max_iterations=iterations)
#------------------------------------------------
vals = lb*np.ones_like(snake[:, 0],dtype=np.uint8)
label = csr_matrix((vals, (snake[:, 1].astype(np.uint8), snake[:, 0].astype(np.uint8))), shape=(img.shape[0], img.shape[1])).toarray()
Result = Result + label
return Result
res = activeContour(img, mask, alpha=0, beta= 10, gamma= 0.0000001)
labels = np.unique(res)
ctr = 25
for label in labels:
if label != 0:
img = np.zeros_like(img)
s = np.where(res == label)
r = s[0].astype(np.uint8)
c = s[1].astype(np.uint8)
rr, cc = polygon(r, c)
img[rr, cc] = 2*ctr
plt.imshow(img, cmap='gray')
plt.title('%d'%label)
plt.axis('off')
plt.show()
ctr+=1
感谢Juan for his help, the theoritical part of this answer is here 并且实施失败。
import numpy as np
def sortVertices(array):
s = np.where(array != 0)
yr = s[0].astype(np.uint8)
xc = s[1].astype(np.uint8)
center_xc = np.sum(xc)/xc.shape
center_yr = np.sum(yr)/yr.shape
theta = np.arctan2(yr-center_yr, xc-center_xc) * 180 / np.pi
indices = np.argsort(theta)
x = xc[indices]
y = yr[indices]
return x, y