我有一张细胞图像,我对它进行了阈值处理,并检测到了这些细胞(使用 cv2)。
我想创建一个值为 True 或 False 的数组,以显示每个组件是否接触到图像的边界 (True) 或不接触 (False)。
import cv2 as cv
# Read the image you want connected components of,IN BLACK AND WHITE
img = cv.imread('../images/37983_ERSyto/cellpaintingfollowup-reimage_a01_s1_w26ae36209-938b-45ef-b166-3aba3af125df.tif',cv.IMREAD_GRAYSCALE)
seed_pt = (100,800) #point in the background
fill_color = 0
mask = np.zeros_like(img)
kernel = cv.getStructuringElement(cv.MORPH_RECT,(6,5))
for th in range(7,70):
#creates a copy of the mask:
prev_mask = mask.copy()
#thresholded image:
mask = cv.threshold(img,th,22331,cv.THRESH_BINARY)[1]
#FloodFill: fill a connected component starting from the seed point with the specified color.
mask = cv.floodFill(mask,None,seed_pt,fill_color)[1]
#cv.bitwise: calculates the per-element bit-wise disjunction of two arrays or an array and a scalar. Superposition of thresholded images
mask = cv.bitwise_or(mask,prev_mask)
#clean speckles
mask = cv.morphologyEx(mask,cv.MORPH_OPEN,kernel)
#compute the connected components labeled image of boolean image and also produce a statistics output for each label
connectivity = 8 #You need to choose 4 or 8 for connectivity type.
#OBTAIN FEATURE OF THE AREA IN PIXELS OF THE CELLS
stats = cv.connectedComponentsWithStats(mask,connectivity,cv.CV_32S)[2]
label_area = stats[1:,cv.CC_STAT_AREA] #we dont include the first element because it represents the area of the background
#OBTAIN FEATURES OF THE CENTROID POSITION
centroids = cv.connectedComponentsWithStats(mask,cv.CV_32S)[3]
label_centroids_x = centroids[1:,0] #dont include the first element because it represents the background
label_centroids_y = centroids[1:,1]
#HIGHT: The vertical size of the bounding box.
label_hight = stats[1:,cv.CC_STAT_HEIGHT]
#WIDTH: The horizontal size of the bounding box.
label_width = stats[1:,cv.CC_STAT_WIDTH]
#TOUCHING IMAGE BOUNDARIES: is the component touching the boundaries of the matrix/image?--> True/False
label_bounary = #boolean array
我首先想到的是搜索每个组件的轮廓并定义一些限制,但是我无法理解每个组件的标签是如何存储的,因此我无法选择所需的组件。
非常感谢您。