📜  Mahotas – 图像的条件分水岭

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

Mahotas – 图像的条件分水岭

在本文中,我们将了解如何在 mahotas 中对图像进行条件分水岭。在图像处理的研究中,分水岭是在灰度图像上定义的变换。这个名字隐喻地指的是一个地质分水岭或排水沟,它将相邻的流域分隔开来。

在本教程中,我们将使用“Lena”图像,下面是加载它的命令。

mahotas.demos.load('lena')

下面是莉娜的图片

注意:输入图像应被过滤或加载为灰色

为了过滤图像,我们将获取图像对象 numpy.ndarray 并在索引的帮助下对其进行过滤,下面是执行此操作的命令

image = image[:, :, 0]

下面是实现

Python3
# importing required libraries
import mahotas
import mahotas.demos
from pylab import gray, imshow, show
import numpy as np
   
# loading image
img = mahotas.demos.load('lena')
 
 
   
# filtering image
img = img.max(2)
 
# otsu method
T_otsu = mahotas.otsu(img)  
   
# image values should be greater than otsu value
img = img > T_otsu
   
print("Image threshold using Otsu Method")
 
# creating a labelled image
marker, n_nucleus = mahotas.label(img)
   
# showing image
imshow(img)
show()
   
 
# watershed of image
new_img = mahotas.cwatershed(img, marker)
 
print("CWatershed Image")
 
# showing image
imshow(new_img)
show()


Python3
# importing required libraries
import mahotas
import numpy as np
from pylab import gray, imshow, show
import os
  
# loading image
img = mahotas.imread('dog_image.png')
 
 
# filtering image
img = img[:, :, 0]
   
# otsu method
T_otsu = mahotas.otsu(img)  
   
# image values should be greater than otsu value
img = img > T_otsu
   
print("Image threshold using Otsu Method")
   
# showing image
imshow(img)
show()
 
# creating a labelled image
marker, n_nucleus = mahotas.label(img)
   
# watershed of image
new_img = mahotas.cwatershed(img, marker)
 
print("CWatershed Image")
 
# showing image
imshow(new_img)
show()


输出 :

Image threshold using Otsu Method

CWatershed Image

另一个例子

Python3

# importing required libraries
import mahotas
import numpy as np
from pylab import gray, imshow, show
import os
  
# loading image
img = mahotas.imread('dog_image.png')
 
 
# filtering image
img = img[:, :, 0]
   
# otsu method
T_otsu = mahotas.otsu(img)  
   
# image values should be greater than otsu value
img = img > T_otsu
   
print("Image threshold using Otsu Method")
   
# showing image
imshow(img)
show()
 
# creating a labelled image
marker, n_nucleus = mahotas.label(img)
   
# watershed of image
new_img = mahotas.cwatershed(img, marker)
 
print("CWatershed Image")
 
# showing image
imshow(new_img)
show()

输出:

Image threshold using Otsu Method

CWatershed Image