📜  Mahotas – 设定门槛

📅  最后修改于: 2022-05-13 01:54:33.984000             🧑  作者: Mango

Mahotas – 设定门槛

在本文中,我们将了解如何为 mahotas 中的图像设置阈值。为此,我们将使用来自核分割基准的荧光显微镜图像。我们可以在下面给出的命令的帮助下获取图像

mahotas.demos.nuclear_image()

图像阈值化是图像分割的一种简单形式。这是一种从灰度或全彩色图像创建二值图像的方法。这通常是为了将“对象”或前景像素与背景像素分开以帮助图像处理。
下面是nuclear_image

为了给图像设置阈值,我们将获取图像对象 numpy.ndarray 并将数组除以阈值,这里阈值是平均值,下面是执行此操作的命令

img = (img < img.mean())]

示例 1:

Python3
# importing required libraries
import mahotas as mh
import mahotas.demos
import numpy as np
from pylab import imshow, show
 
# getting nuclear image
nuclear = mh.demos.nuclear_image()
 
 
# filtering the image
nuclear = nuclear[:, :, 0]
 
print("Image with filter")
# showing the image
imshow(nuclear)
show()
 
# setting image threshold
nuclear = (nuclear < nuclear.mean())
 
print("Image with threshold")
# showing the threshold image
imshow(nuclear)
show()


Python3
# importing required libraries
import numpy as np
import mahotas
from pylab import imshow, show
  
# loading image
img = mahotas.imread('dog_image.png')
   
# filtering the image
img = img[:, :, 0]
    
print("Image with filter")
# showing the image
imshow(img)
show()
  
# setting threshold
img = (img < img.mean())
  
print("Image with Threshold")
# showing the threshold image
imshow(img)
show()


输出 :

示例 2:

Python3

# importing required libraries
import numpy as np
import mahotas
from pylab import imshow, show
  
# loading image
img = mahotas.imread('dog_image.png')
   
# filtering the image
img = img[:, :, 0]
    
print("Image with filter")
# showing the image
imshow(img)
show()
  
# setting threshold
img = (img < img.mean())
  
print("Image with Threshold")
# showing the threshold image
imshow(img)
show()

输出 :