Mahotas – 图像中零的分数
在本文中,我们将了解如何在 mahotas 中获取图像中零的分数。零的分数是统计数据量为零的百分比。它与大量对象为零值的统计模型相关。
在本教程中,我们将使用“luispedro”图像,下面是加载它的命令。
mahotas.demos.load('luispedro')
下面是 luispedro 图像
为了做到这一点,我们将使用 np.mean 方法
Syntax : np.mean(img==0)
Argument : It takes image object as argument
Return : It returns numpy.float64
注意:输入图像应被过滤或应加载为灰色
为了过滤图像,我们将获取图像对象 numpy.ndarray 并在索引的帮助下对其进行过滤,下面是执行此操作的命令
image = image[:, :, 0]
示例 1:
Python3
# importing various libraries
import numpy as np
import mahotas
import mahotas.demos
from mahotas.thresholding import soft_threshold
from pylab import imshow, show
from os import path
# loading image
f = mahotas.demos.load('luispedro', as_grey = True)
# showing image
print("Image")
# getting fraction of zeros in image
fraction = np.mean(f == 0)
print("Fraction of zeros in image: {0}".format(fraction))
imshow(f)
show()
# Transform using D8 Wavelet to obtain transformed image t
t = mahotas.daubechies(f, 'D8')
# Discard low-order bits:
t /= 8
t = t.astype(np.int8)
# getting fraction of zeros in image
fraction = np.mean(t == 0)
print("Fraction of zeros in transform (after division by 8): {0}".format(fraction))
# showing transformed image
print("Transformed Image")
imshow(t)
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')
# filtering image
img = img[:, :, 0]
# getting fraction of zeros in image
fraction = np.mean(img == 0)
print("Fraction of zeros in image: {0}".format(fraction))
imshow(img)
show()
# Transform using D8 Wavelet to obtain transformed image t
t = mahotas.daubechies(img, 'D8')
# Discard low-order bits:
t /= 8
t = t.astype(np.int8)
# getting fraction of zeros in image
fraction = np.mean(t == 0)
print("Fraction of zeros in transform (after division by 8): {0}".format(fraction))
# showing transformed image
print("Transformed Image")
imshow(t)
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')
# filtering image
img = img[:, :, 0]
# getting fraction of zeros in image
fraction = np.mean(img == 0)
print("Fraction of zeros in image: {0}".format(fraction))
imshow(img)
show()
# Transform using D8 Wavelet to obtain transformed image t
t = mahotas.daubechies(img, 'D8')
# Discard low-order bits:
t /= 8
t = t.astype(np.int8)
# getting fraction of zeros in image
fraction = np.mean(t == 0)
print("Fraction of zeros in transform (after division by 8): {0}".format(fraction))
# showing transformed image
print("Transformed Image")
imshow(t)
show()
输出 :