在python中定义孔填充函数(根据数学定义)的问题



我是python的新手,所以请考虑:(我试图在应用";关闭";不使用任何内置函数。我想在我的头部Ct照片上做这样的事情(下面(。具有结构元素(+(的连续侵蚀(类似于上面的方程(所以我写了这段代码,但我对最初的观点仍然有问题。

def holefiling(img,B):
initial= np.zeros_like(img)
imginv = cv.bitwise_not(img)
i=np.random.choice(img.shape[0])
j=np.random.choice(img.shape[1])
while img[i,j]!=np.max(img):#FINDING A WHITE HOLE WITH THE brightest intensities
i=np.random.choice(img.shape[0])
j=np.random.choice(img.shape[1])

initial[i,j]=imginv[i,j]
#initial=cv.bitwise_not(initial)#?
for k in (0,10000):#10000 times of doing this algoryithm might solve all of it
erosion = cv.erode(initial,B,iterations = 1) #X_k erosion with structuring element B
X_k=initial & erosion
initial=X_k
Output=X_k | img
return Output
kernel=cv.getStructuringElement(cv.MORPH_CROSS,(3,3))# defining a 3*3 cross(+) structure
Holefilled=holefiling(closing,kernel)
#showing the result
plt.figure()
plt.suptitle('fingerprint.tif')
plt.subplot(131)
plt.title(r'${Original_{img}}$')
plt.imshow(img, cmap='gray', vmin=0, vmax=np.max(img))
plt.axis(False)
plt.subplot(132)
plt.title(r'${closing}$')
plt.imshow(closing, cmap='gray', vmin=0, vmax=np.max(closing))
plt.axis(False)
plt.subplot(133)
plt.title(r'${Holefilled}$')
plt.imshow(Holefilled, cmap='gray', vmin=0, vmax=np.max(Holefilled))
plt.axis(False)

结果没有显示图片有任何变化,我对为什么会发生这种情况感到困惑。(事实上,我认为初始点必须在最亮的区域,而且,在没有发生任何事情的情况下,必须通过增加迭代来进行更改。(在这里,您可以看到结果:放大结果

我认为问题可能与其中一个有关。1.起点一定在两个洞之间,但我不知道如何:|2.替换代码中X_k的过程有一点问题3.结构元素(cross+(应该是其他元素(这个选项的可能性太低:D(所以,请所有能帮我做这件事的人,提前非常感谢。

Holfilling过程的初始点

我已经找到了我的问题的答案,但对那些人来说,这可能也是他们的问题。我不会删除我的问题,而是想添加我的答案。我解决了查找首字母的问题您可能想知道如何解决?所以我决定找到一个具有零值的空穴点,而它周围4条线的点的总和具有类似于填充线的值。(如果你想填充黑色区域内的白色空洞,你应该找到一个值为1的点,而周围四条线的点之和为零(这是我的代码:

def holefiling(img,B):
img1 = np.array(img)  
#imginv
thresh = 0/5
binarr = np.where(img1>thresh, 0, 1)# if i wrote: np.where(img>thresh, 1,0) the color would be reversed
# Covert numpy array back to image 
binimg = np.asmatrix(binarr)
#binimg1  = cv.threshold(img, thresh, 255, cv.THRESH_BINARY)
imginv=binimg.astype('uint8')  
hf=13 #depends on situation of the hole
#finding an initial point:
#1.defining a black initial
initial= np.zeros_like(img)
#finding a white pixel inside a black area:
#separating each pixel of the image but not around the edges: [hf:img.shape[0]-hf]
for i in range(0+hf,img.shape[0]-hf):
for j in range(0+hf,img.shape[1]-hf):
#print(np.sum(img[i-hf:i+hf,j+hf]))
#print(img[78,236])
#checking if the i,j pixel is black while around it has white lines according to the equations of these lines below and vice-versa
if np.sum(img[i-hf:i+hf,j-hf])>=hf+hf/2 and np.sum(img[i-hf:i+hf,j+hf])>=hf+hf/2 and np.sum(img[i-hf,j-hf:j+hf])>=1/2 and np.sum(img[i+hf,j-hf:j+hf])>=1/2 and img[i,j]==0:
#finally the initial point for the holefiling process
""" print(img[i,j])
print(img[j,i]) """
i_init=i
j_init=j
print(i_init,j_init)
initial[i_init,j_init]=imginv[i_init,j_init]
#print(i_init,j_init)
""" elif np.sum(img[i-hf:i+hf,j-hf])>=hf and np.sum(img[i-hf:i+hf,j+hf])>=hf and np.sum(img[i-hf,j-hf:j+hf])>=hf and np.sum(img[i+hf,j-hf:j+hf])>=hf and img[i,j]==0:
print(i,j)
i_init=i
j_init=j
initial[i_init,j_init]=img[i_init,j_init] """
print(img[i_init,j_init])
#initial[i_init,j_init]=imginv[i_init,j_init]
plt.figure()
plt.title(r'${InitialPoints}$')
plt.imshow(initial, cmap='gray', vmin=0, vmax=np.max(initial))
plt.axis(False)
plt.show()        
#initial[i_init,j_init]=img[i_init,j_init]
for k in range(100):#ten times of doing this algorithym might solve all of it
dialation=cv.dilate(initial,B,iterations=1)
X_k=imginv & dialation #img | dialation

#if you want to check the code and wanna know how it works in each iteration, simply run this part down below
""" plt.figure
plt.subplot(144)
plt.imshow(X_k, cmap='gray', vmin=0, vmax=np.max(initial))
plt.axis(False)
plt.subplot(141)
plt.imshow(initial, cmap='gray', vmin=0, vmax=np.max(initial))
plt.axis(False)
plt.subplot(142)
plt.imshow(dialation, cmap='gray', vmin=0, vmax=np.max(dialation))
plt.axis(False)
plt.subplot(143)
plt.imshow(imginv, cmap='gray', vmin=0, vmax=np.max(initial))
plt.axis(False)
plt.show() """
initial=X_k
#complementing X_k in order to have both images in complent mode
""" binarrinv = np.where(X_k>0, 0, 1)# if i wrote: np.where(img>thresh, 1,0) the color would be reversed
# Covert numpy array back to image 
binimginv = np.asmatrix(binarrinv)
imgOut=binimginv.astype('uint8')#imgOut """
Output=(X_k | img)# since the img and imgout are the complement of what we had in the slides so if we use intersection between two complements is equal to the union of two original images
return Output`

因此,为了检查它是如何工作的,我运行以下代码:

img= cv.imread('HeadCT.tif',0)
img = np.array(img)  
# Put threshold to 100 according to question to make it binary
thresh = 100
binarr = np.where(img>thresh, 1, 0)
# Covert numpy array back to image 
binimg = np.asmatrix(binarr)
binimg1=binimg.astype('uint8')
kernel=cv.getStructuringElement(cv.MORPH_ELLIPSE,(20,20))# defining a 20*20 ELLIPSE structure
closing = cv.morphologyEx(binimg1, cv.MORPH_CLOSE, kernel)# closing with Kernel
kernel1=cv.getStructuringElement(cv.MORPH_ELLIPSE,(5,5))# defining a 3*3 ellipse structure
#testing my function
Holefilled1=holefiling(closing,kernel1)#for one of the holes
plt.figure()
plt.suptitle('HeadCT.tif')
plt.subplot(131)
plt.title(r'${Original_{img}}$')
plt.imshow(binimg1, cmap='gray', vmin=0, vmax=np.max(binimg1))
plt.axis(False)
plt.subplot(132)
plt.title(r'${closing_{on_{original img}}}$')
plt.imshow(closing, cmap='gray', vmin=0, vmax=np.max(closing))
plt.axis(False)
plt.subplot(133)
plt.title(r'${Holefilled_{after_{closing}}}$')
plt.imshow(Holefilled1, cmap='gray', vmin=0, vmax=np.max(Holefilled1))
plt.axis(False)
plt.show()

这是输入imageHeadCt.tif这是it的输出关闭后的孔填充结果您也可以使用此结果来查找对象的边界。我希望你和我一样喜欢

最新更新