连通域计算与形态学特征
三维
import numpy as np
import skimage.measure as msr
# Creates a 32x32x32 array with two regions
a = np.zeros((32,32,32),np.int32)
a[5:10,5:10,5:10] = 1
a[20:30,20:30,20:30] = 1
# Assign each region to a unique integer
# then calculate the property of each region
a_regions, num_rgn = msr.label(a,return_num=True)
a_properties = msr.regionprops(a_regions)
for prop in a_properties:
xmin,ymin,zmin,xmax,ymax,zmax = prop.bbox
print(xmin,ymin,zmin,xmax,ymax,zmax)
二维
import cv2
import numpy as np
img=cv2.imread("labelTmp6.jpg")
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
contours,hierarchy= cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
print(len(contours))
for i in range(len(contours)):
x,y,w,h = cv2.boundingRect(contours[i])
cv2.rectangle(img,(x-2,y-2),(x+w+2,y+h+2),(0,255,0),2)
print(x,y,w,h)
cv2.imwrite('contours.png', img)
骨架提取
Bone = morphology.skeletonize(img)