📜  Mahotas – Otsu 的方法

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

Mahotas – Otsu 的方法

在本文中,我们将了解如何在 mahotas 中实现 otsu 的方法。在计算机视觉和图像处理中,以 Nobuyuki Otsu 命名的 Otsu 方法用于执行自动图像阈值处理。在最简单的形式中,该算法返回一个单一的强度阈值,将像素分为前景和背景两类。

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

mahotas.demos.load('luispedro')

下面是 luispedro 图像

为此,我们将使用 mahotas.otsu 方法

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

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

image = image[:, :, 0]

示例 1:

Python3
# importing required libraries
import mahotas
import mahotas.demos
import numpy as np
from pylab import imshow, gray, show
from os import path
 
# loading the image
photo = mahotas.demos.load('luispedro')
 
# showing original image
print("Original Image")
imshow(photo)
show()
 
# loading image as grey
photo = mahotas.demos.load('luispedro', as_grey = True)
 
# converting image type to unit8
# because as_grey returns floating values
photo = photo.astype(np.uint8)
 
# otsu method
T_otsu = mahotas.otsu(photo)
 
# printing otsu value
print("Otsu Method value : " + str(T_otsu))
 
print("Image threshold using Otsu Method")
# showing image
# image values should be greater than otsu value
imshow(photo > T_otsu)
show()


Python3
# importing required libraries
import mahotas
import numpy as np
from pylab import imshow, show
import os
 
 
# loading image
img = mahotas.imread('dog_image.png')
     
 
# setting filter to the image
img = img[:, :, 0]
 
imshow(img)
show()
 
 
# otsu method
T_otsu = mahotas.otsu(img)
 
# printing otsu value
print("Otsu Method value : " + str(T_otsu))
 
print("Image threshold using Otsu Method")
# showing image
# image values should be greater than otsu value
imshow(img > T_otsu)
show()


输出 :

示例 2:

Python3

# importing required libraries
import mahotas
import numpy as np
from pylab import imshow, show
import os
 
 
# loading image
img = mahotas.imread('dog_image.png')
     
 
# setting filter to the image
img = img[:, :, 0]
 
imshow(img)
show()
 
 
# otsu method
T_otsu = mahotas.otsu(img)
 
# printing otsu value
print("Otsu Method value : " + str(T_otsu))
 
print("Image threshold using Otsu Method")
# showing image
# image values should be greater than otsu value
imshow(img > T_otsu)
show()

输出 :