In [1]:
%matplotlib inline
import numpy as np
import os, string
from matplotlib import pyplot as plt
import scipy as sp
import cv2
import tensorflow as tf
import sys,time,tifffile
img = tifffile.imread('30.tif')
#img = cv2.imread('30.jpg')
#img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
print(img.shape)
plt.imshow(img[18,:,:],cmap='gray')
Out[1]:
In [2]:
#if sess:
# sess.close()
dia = 9
with tf.device('/cpu:0'):
O=tf.placeholder('float', shape=[1,img.shape[1],img.shape[2],1])
A = tf.placeholder('float', shape=[dia,dia,1,1])
B=O
for i in range(20):
B = tf.nn.conv2d(B,A,strides=[1,1,1,1],padding='SAME')
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
convImg = img.copy().reshape(img.shape[0],img.shape[1],img.shape[2],1)
convImg = np.minimum(convImg*4,400)
blurKernel = np.ones((dia,dia))
blurKernel /= np.sum(blurKernel)
data = np.zeros((1,img.shape[1],img.shape[2],1))
for i in range(30):
#print(i)
sys.stdout.write('\r %d'%i)
time.sleep(0.1)
data[0,:,:,:] = convImg[i,:,:,:]
t = sess.run(B,feed_dict={O:data,A:blurKernel.reshape([dia,dia,1,1])})
print(t[0,:,:,0].shape)
convImg[i,:,:,0] = t[0,:,:,0].reshape((img.shape[1],img.shape[2]))
binImg = img > 1. +8.*np.sqrt(convImg.reshape(img.shape))
sess.close()
#print(convImg.shape)
# dia = 9
# blurKernel = np.ones((dia,dia))
# blurKernel /= np.sum(blurKernel)
# convImg1 = np.minimum(img*4,400)
# for i in range(20):
# #print(i)
# sys.stdout.write('\r %d'%i)
# time.sleep(0.1)
# convImg1 = cv2.blur(convImg1, (9,9))
# binImg1 = img > 1. + 8.*np.sqrt(convImg1.reshape(img.shape))
# resImg1 = binImg1.copy().reshape(img.shape)
# plt.imshow(resImg1,cmap='gray')
In [3]:
resImg = binImg.copy().reshape(img.shape)
resImg = resImg[:,5:-5,5:-5]
plt.imshow(resImg[18,:,:],cmap='gray')
Out[3]:
In [4]:
tpos = np.mat(np.where(np.int8(resImg) == 1))
tpos = tpos.T
print(tpos.shape)
cluster = np.arange(tpos.shape[0])
for i in range(tpos.shape[0]):
#if i > 1:
# break
#print(tpos[i,:])
sel0 = np.where(np.abs((tpos[:,0] -tpos[i,0])) < 2)
#print(sel0[0])
sel1 = np.where(np.abs((tpos[sel0[0],1] -tpos[i,1])) < 2)
#print(sel1[0])
sel2 = np.where(np.abs((tpos[sel0[0][sel1[0]],2] -tpos[i,2])) < 2)
#print(sel2[0])
ind = sel0[0][sel1[0][sel2[0]]]
if ind.shape[0] > 1:
#print(np.min(np.min(value),cluster[i]))
fore = cluster[ind]
fore = np.unique(np.sort(fore))
a= np.min(fore)
minValue = min(cluster[i], a)
fillVal = []
fillVal.extend(fore)
fillVal.append(i)
#print(fillVal)
for i in fillVal:
cluster[cluster==i]=minValue
#print(dist.shape)
#break
In [5]:
sz = []
for i in range(cluster.shape[0]):
n = np.sum(cluster == i)
if n > 10:
sz.append((i,n))
print(sz)
In [6]:
#print(np.where(cluster == 651))
test = np.zeros_like(resImg)
pt = tpos[np.where(cluster == 1)[0],:]
test[pt[:,0],pt[:,1],pt[:,2]] = 255
#test[tpos[:,0],tpos[:,1]]=255
plt.imshow(test[18,:,:],cmap='gray')
Out[6]:
In [75]:
print(test.shape)
#tifffile.imsave('hehe.tif',np.int8(test)*255)
In [7]:
with tf.device('/cpu:0'):
O=tf.placeholder('float', shape=[None,test.shape[0],test.shape[1],test.shape[2],1])
M=tf.nn.max_pool3d(O,ksize=(1,3,3,3,1),padding='SAME',strides=(1,1,1,1,1))
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
maskImg = np.multiply(img[:,5:-5,5:-5],test)
maxImg = sess.run(M,feed_dict={O:maskImg.reshape([1,test.shape[0],test.shape[1],test.shape[2],1])})
plt.imshow(maxImg[0,18,:,:,0],cmap='gray')
Out[7]:
In [8]:
dotImg = np.zeros_like(maskImg)
dotImg[(test >0)& (maskImg==maxImg[0,:,:,:,0])]=255 #
fig = plt.figure()
#ax = fig.add_subplot(111)
plt.imshow(dotImg[18,:,:],cmap='gray')
fig = plt.figure()
#ax = fig.add_subplot(111)
plt.imshow(maskImg[18,:,:],cmap='gray')
Out[8]:
In [97]:
ptImg = np.zeros_like(maskImg)
for ij in range(2,maskImg.shape[0]-2):
for i in range(2,maskImg.shape[1]-2):
for j in range(2,maskImg.shape[2]-2):
if maskImg[ij,i,j] == 0:
continue
if np.max(maskImg[ij-1:ij+1,i-1:i+1,j-1:j+1]) == maskImg[ij,i,j]:
ptImg[ij,i,j]=255
plt.imshow(ptImg[18,:,:],cmap='gray')
Out[97]:
In [93]:
help(np.max)
In [ ]: