1 year ago
#388008
kukuki lili
Here is a picture of a complex cell adhesion. I tried to segment it using the watershed algorithm, but it didn't work,can someone help me?
Here is a picture of a complex cell adhesion. I tried to segment it using the watershed algorithm, but it didn't work. Can someone give me some pointers?My sincerest thanks!
Or, what other algorithms can solve such complex glued pictures?
from skimage.feature import peak_local_max
from skimage.morphology import watershed
from scipy import ndimage
import numpy as np
import cv2
input_img1 = cv2.imread('F:\\twf\\code\\Image enhancement\\image1\\24.png')
input_img = cv2.resize(input_img1, (0,0),fx=0.25,fy=1/3,interpolation=cv2.INTER_CUBIC)
cv2.imshow("input_img",input_img)
input_imgsp=input_img.copy()
gray = cv2.cvtColor(input_img, cv2.COLOR_BGR2GRAY)
adaptivethresh = cv2.adaptiveThreshold(gray , 255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV,25,5)
cv2.imshow("adaptivethresh ",adaptivethresh)
#Binarization
black = np.ones([512, 512, 3], np.uint8)*255
blackb=np.ones([512, 512, 3], np.uint8)*255
__,conts, hier = cv2.findContours(adaptivethresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
l = 14.00
a = 10.00
for i, contour in enumerate(conts):
cnt = contour
cntArea = cv2.contourArea(cnt)
len = cv2.arcLength(cnt, True)
if cntArea <a and len < l:
cv2.drawContours(black, conts, i, (0, 0, 255), 1)
cv2.imshow('black',black)
blackg = cv2.cvtColor(black, cv2.COLOR_BGR2GRAY)
ret2, threshblack = cv2.threshold(blackg,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
kernel = np.ones((2, 2), np.uint8)
result2=adaptivethresh-threshblack
cv2.imshow('result2',result2)
#remove noise
sure_bg = cv2.dilate(result2,kernel,iterations=1)
D = ndimage.distance_transform_edt(result2)
localMax = peak_local_max(D, indices=False, min_distance=5,labels=result2)
markers = ndimage.label(localMax, structure=np.ones((3, 3)))[0]
labels = watershed(-D, markers, mask=result2)
output = np.zeros((input_imgsp.shape[0],input_imgsp.shape[1], 3), np.uint8)
mask = np.zeros(gray.shape, dtype="uint8")
n=0
mask = np.zeros(gray.shape, dtype="uint8")
for label in np.unique(labels):
if label == 0:
continue
mask[labels == label] = 255
cv2.imshow("mask", mask)
#watershed segmentation of adherent spores
num_labels, lab, stats, centroids = cv2.connectedComponentsWithStats(mask , connectivity=8)
output = np.zeros((input_imgsp.shape[0],input_imgsp.shape[1], 3), np.uint8)
for i in range(1, num_labels):
mask = lab == i
output[:, :, 0][mask] = np.random.randint(0, 255)
output[:, :, 1][mask] = np.random.randint(0, 255)
output[:, :, 2][mask] = np.random.randint(0, 255)
cv2.namedWindow("Output", 0)
cv2.imshow('Output', output)
#Assign different colors to different connected domains
#But the result failed, and the segmentation was not successful
cv2.waitKey()
python
count
cell
0 Answers
Your Answer