import cv2
import numpy as np
from openslide import OpenSlide
grid_size = 224
#img = cv2.imread('/Users/xiaoying/Downloads/0066YOYqgy1fue9dkgygrj30i20pn0wk.jpg')
slide = OpenSlide('/Users/xiaoying/QQ下载/中南医院_酒精肝/2018.07.13/image1.ndpi')
img = np.array(slide.read_region((0, 0), 3, (slide.level_dimensions[3][0], slide.level_dimensions[3][1])))
histogram = np.zeros(256)
histogram.dtype = int
for i in range(img.shape[0]):
for j in range(img.shape[1]):
gray = img[i, j, 0]
histogram[gray] += 1
def calc_var(histogram, threshold):
back = histogram[:threshold]
fore = histogram[threshold:]
weight_b = back.sum() / histogram.sum()
weight_f = fore.sum() / histogram.sum()
#if back.sum() != 0 and fore.sum() != 0:
# sum_b = 0
# for i in range(back.shape[0]):
# sum_b += i * back[i]
#sum_f = 0
#for i in range(fore.shape[0]):
# sum_f += (i + threshold) * fore[i]
#mean_b = sum_b / back.sum()
#mean_f = sum_f / fore.sum()
#var_b = 0
#for i in range(back.shape[0]):
# var_b += (i - mean_b) ** 2 * back[i]
#var_b = var_b / back.sum()
#var_f = 0
#for i in range(fore.shape[0]):
# var_f += (i + threshold - mean_f) ** 2 * fore[i]
#var_f = var_f / fore.sum()
#if back.sum() == 0:
# var_b = 0
#if fore.sum() == 0:
# var_f = 0
#var = weight_b * var_b + weight_f * var_f
if back.sum() == 0:
var_b = 0
else:
sum_b = 0
for i in range(back.shape[0]):
sum_b += i * back[i]
mean_b = sum_b / back.sum()
var_b = 0
for i in range(back.shape[0]):
var_b += (i - mean_b) ** 2 * back[i]
var_b = var_b / back.sum()
if fore.sum() == 0:
var_f = 0
else:
sum_f = 0
for i in range(fore.shape[0]):
sum_f += (i + threshold) * fore[i]
mean_f = sum_f / fore.sum()
var_f = 0
for i in range(fore.shape[0]):
var_f += (i + threshold - mean_f) ** 2 * fore[i]
var_f = var_f / fore.sum()
var = weight_b * var_b + weight_f * var_f
return var
var = []
for i in range(256):
if i != 0:
var.append(calc_var(histogram, i))
minvar = min(var)
thre = var.index(minvar)
print(minvar)
print(thre)
#for i in range(img.shape[0]):
# for j in range(img.shape[0]):
# if img[i, j, 0] < thre:
# img[i, j] = (0, 0, 0, 255)
#cv2.imwrite('image1.png', img)
def slide_tiling(thre, image):
grid = []
width = image.shape[0]
height = image.shape[0]
grid_num_x = width // grid_size
grid_num_y = height // grid_size
for i in range(grid_num_y):
for j in range(grid_num_x):
flag = False
tissue_pix = 0
for k in range(224):
for l in range(224):
if image[i * 224 + l, j * 224 + k, 0] < thre:
tissue_pix += 1
if tissue_pix / (224 * 224) > 0.005:
flag = True
if flag == True:
grid.append((j * 224, i * 224))
return grid
grids = slide_tiling(thre, img)
print(grids)
for grid in grids:
img[grid[1]:grid[1] + 224, grid[0]] = (0, 0, 0, 255)
img[grid[1]:grid[1]+224, grid[0]+224] = (0, 0, 0, 255)
img[grid[1], grid[0]:grid[0]+224] = (0, 0, 0, 255)
img[grid[1]+224, grid[0]:grid[0]+224] = (0, 0, 0, 255)
cv2.imwrite('img.tiff', img)
将whole slide切割成很多tiles并去除背景
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 每年到这个时候,身边就会有很多人开始咳嗽、咳痰、流鼻涕、打喷嚏的,有些是感冒、有些是哮喘、有些是气管炎,有些的鼻炎...
- 我要看美剧学英文,中文字幕没意思;没有字幕听不懂;英文字幕又难找,怎么办? 难道自己手动删除中文字幕? 下面,我们...
- 最近 Android-CleanArchitecture 闹得是沸沸扬扬,然而笔者也不甘寂寞,一直在研究这个东西,...