提取细胞特征
持续更新ing……
import os,time
import cv2
import numpy as np
import matplotlib.pyplot as plt
import copy
def get_fit_img(img):
dst = cv2.fastNlMeansDenoising(img,None,15,7,21)
return dst
def get_mean(temp, long_=10):
temp_1 = []
for m in range(0+long_, 255-long_):
temp_1.append(temp[m-long_:m+long_].mean())
temp_2 = np.array(temp_1)
temp_out = np.zeros(255)
temp_out[0+long_: 255-long_] = temp_2
return temp_out
def grien_value(temp):
temp_1 = np.zeros(255)
temp_1[1:255] = temp[0:254]
temp_out = temp - temp_1
temp_out = get_mean(temp_out, 10)
return temp_out
def get_last2value(temp):
sign1 = 0
sign2 = 0
cnt = 0
value = 0
temp_4 = temp[::-1]
for k in range(0, len(temp_4)-1):
if temp_4[k+1] > temp_4[k]:
sign2 = sign1
sign1 = 1
if sign1 != sign2:
cnt = cnt + 1
else:
sign2 = sign1
sign1 = 0
if sign1 != sign2:
cnt = cnt + 1
if cnt == 3:
value = k
break #!!!!!!!!!
return 255-value
def get_2value(img, long_ = 10):
temp_2 = cv2.calcHist([img],[0],None,[256],[0,256])
temp_4 = get_mean(temp_2, long_)
temp_4 = get_mean(temp_4, 5)
temp_4 = grien_value(temp_4) #使用二阶导数更容易检测到细胞核像素阈值
# plt.figure("Image")
# plt.imshow(img)
# plt.figure("Image_value")
# plt.plot(temp_4)
# plt.figure("Image_value_gre")
# plt.plot(grien_value(temp_4))
# plt.figure("Image_value_gre2")
# plt.plot(grien_value(grien_value(temp_4)))
# plt.show()
sign1 = 0
sign2 = 0
cnt = 0
value_1 = 0
value_2 = 0
for k in range(0, len(temp_4)-1):
if temp_4[k+1] > temp_4[k]:
sign2 = sign1
sign1 = 1
if sign1 != sign2:
cnt = cnt + 1
else:
sign2 = sign1
sign1 = 0
if sign1 != sign2:
cnt = cnt + 1
if cnt == 3:
value_1 = k
break #!!!!!!!!!
#print(temp_4[k])
value_2 = get_last2value(temp_4)
return value_1,value_2
def get_img_open(img,fit):
img_open = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel_1)
return img_open
def get_img_close(img,fit):
img_close = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel_1)
return img_close
def get_cell_nuclei_mask(img, value):
pass
def get_cell_cytoplasm_mask(img, nuclei_mask, value):
pass
def get_cell_nuclei_value(img, img_mask): #计算深染程度
pass
def get_cell_cytoplasm_value(img, img_mask): #细胞质计算内容暂定
pass
if __name__ == "__main__":
dstroot = 'cells'
listcells = os.listdir(dstroot)
cellsinfo = []
for n in listcells:
cellinfo = {}
cellpath = os.path.join(dstroot, n)
img = cv2.imread(cellpath)
img_gray = cv2.imread(cellpath, 0)
img_gray = get_fit_img(img_gray)
value_1,value_2 = get_2value(img_gray)
cell_nuclei_mask, nuclei_cnt, nuclei_area, nuclei_circ, nuclei_rule = get_cell_nuclei_mask(img_gray, value_1) #获取细胞核掩码、个数、面积、周长、核形规则度
cell_cytoplasm_mask, cytoplasm_area, cytoplasm_rule = get_cell_cytoplasm_mask(img_gray, cell_nuclei_mask, value_2) #获取细胞质掩码、面积、细胞规则度
cell_nuclei_value = get_cell_nuclei_value(img, cell_nuclei_mask) #获取细胞核深染程度
cell_cytoplasm_value = get_cell_cytoplasm_value(img, cell_cytoplasm_mask) #获取细胞质情况
cell_N_C = nuclei_area/cytoplasm_area #计算核质比
cellinfo_keys = ['cellpath','nuclei_cnt','nuclei_area','nuclei_circ','nuclei_rule','cell_nuclei_value','cytoplasm_area','cytoplasm_rule','cell_cytoplasm_value','cell_N_C']
cellinfo_values = [cellpath,nuclei_cnt,nuclei_area,nuclei_circ,nuclei_rule,cell_nuclei_value,cytoplasm_area,cytoplasm_rule,cell_cytoplasm_value,cell_N_C]
cellinfo = dict(zip(cellinfo_keys, cellinfo_values))
cellsinfo.append(cellinfo)
提取细胞团特征
if __name__ == "__main__":
dstroot = 'clusters'
listcluster = os.listdir(dstroot)
clustersinfo = []
for n in listcluster:
clusterinfo = {}
clusterpath = os.path.join(dstroot, n)
img = cv2.imread(clusterpath)
img_gray = cv2.imread(clusterpath, 0)
#获取阈值
cells_mask, cells_cnt, cells_area, cells_circ, cells_rule = get_cluster_info_1(img_gray) #获取细胞团细胞核掩码、细胞核数、细胞面积,细胞周长、细胞规则度
cytoplasm_mask = get_cluster_mask(img_gray) #获取细胞质掩码
cluster_nuclei_value = get_cluster_nuclei_value(img, cells_mask) #获取细胞核深染程度
cluster_cytoplasm_value = get_cluster_cytoplasm_value(img, cytoplasm_mask) #获取细胞质信息
来源:https://www.cnblogs.com/niulang/p/12546816.html