Mahotas – 软阈值
在本文中,我们将了解如何在 mahotas 中实现软阈值。软阈值也称为小波收缩,因为正和负系数的值都被“收缩”到零,这与保留或删除系数值的硬阈值相反。
在本教程中,我们将使用“luispedro”图像,下面是加载它的命令。
mahotas.demos.load('luispedro')
下面是 luispedro 图像
为此,我们将使用 mahotas.rc 方法
Syntax : mahotas.thresholding.soft_threshold(image, t_value)
Argument : It takes image object and unit8 value as argument
Return : It returns image object
注意:输入图像应被过滤或应加载为灰色
为了过滤图像,我们将获取图像对象 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()
# unit 8 value
t = np.uint8(150)
# soft threshold
photo = mahotas.thresholding.soft_threshold(photo, t)
print("Image with soft 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()
# unit 8 value
t = np.uint8(180)
# soft threshold
img = mahotas.thresholding.soft_threshold(img, t)
print("Image with soft 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()
# unit 8 value
t = np.uint8(180)
# soft threshold
img = mahotas.thresholding.soft_threshold(img, t)
print("Image with soft threshold")
# showing image
imshow(img)
show()
输出 :