📜  Mahotas – Bernsen 局部阈值

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

Mahotas – Bernsen 局部阈值

在本文中,我们将了解如何在 mahotas 中实现 bernsen 局部阈值。 Bernsen 方法是为图像分割而开发的一种局部自适应二值化方法。在这项研究中,实现了 Bernsen 的局部自适应二值化方法,然后针对不同的灰度图像进行了测试。

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

mahotas.demos.load('luispedro')

下面是 luispedro 图像

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

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

为了过滤图像,我们将获取图像对象 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')
 
 
# 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)
 
# showing original image
print("Image")
imshow(photo)
show()
 
# bernsen threshold
photo = mahotas.thresholding.bernsen(photo, 7, 200)
 
 
print("Image with bernsen threshold")
 
# showing image
imshow(photo)
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]
 
print("Image")
 
# showing the image
imshow(img)
show()
 
 
# bernsen threshold
img = mahotas.thresholding.bernsen(img, 5, 100)
 
 
print("Image with bernsen threshold")
 
# showing image
imshow(img)
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]
 
print("Image")
 
# showing the image
imshow(img)
show()
 
 
# bernsen threshold
img = mahotas.thresholding.bernsen(img, 5, 100)
 
 
print("Image with bernsen threshold")
 
# showing image
imshow(img)
show()

输出 :