[英]Generating a numpy random see array of 100 random numbers drawn from a normal distribution with mean 1 and standard deviation 2
[英]Normal distribution of histogram, mean, standard deviation, within a certain part of an array
我在一个文件夹中有很多特定 object 的不同图像。 我想为色调、饱和度和值设置这些图像的直方图。 到目前为止它已经奏效了。
另一方面,我想将正态分布拟合到直方图,得到均值和标准差。 我遇到了色调直方图的问题。 直方图显示它有两个部分,分别为 [0-50] 和 [120-180]。
我的问题是我怎么才能得到 [120-180] 范围内的平均值和标准差,因为我不知道如何编码。
在total_hue_list
中,我尝试计算所有不同颜色代码的出现次数。
import matplotlib.pyplot as plt
import numpy as np
import cv2
import os
from imutils import paths
directory = os.getcwd() + "\Demos/SotetLila"
total_hue_hist = np.zeros((180,))
total_sat_hist = np.zeros((256,))
total_val_hist = np.zeros((256,))
for imagePath in paths.list_images(directory):
img = cv2.imread(imagePath)
kernel = np.ones((5, 5), np.uint8)
blur = cv2.GaussianBlur(img, (5, 5), 0)
hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV)
hsv_image = hsv
hue, sat, val = hsv_image[:, :, 0], hsv_image[:, :, 1], hsv_image[:, :, 2]
hue_hist, bin_hue = np.histogram(hue, bins=range(181))
total_hue_hist += hue_hist
sat_hist, bin_sat = np.histogram(sat, bins=range(257))
total_sat_hist += sat_hist
val_hist, bin_val = np.histogram(val, bins=range(257))
total_val_hist += val_hist
plt.bar(list(range(180)), total_hue_hist)
#plt.bar(list(range(256)), total_sat_hist)
#plt.bar(list(range(256)), total_val_hist)
plt.show()
在我们开始之前只是一个快速指针 - 色调是循环的,所以假设你的最大色调是 180,那么色调值 0 和 1 就像 180 到 0 一样接近。我不确定你的代码是否确实是 180最大值与否,但只是把这个事实放在那里以防万一。
回到主题 - 如果我理解正确,您只想考虑色调值在 120 到 180 之间的像素,而忽略所有其他值。 如果确实如此,则np.histogram
具有专门为此设计的值。 引用文档:
range(float, float) ,可选 bin 的下限和上限。 如果没有提供,范围就是(a.min(), a.max())。 超出范围的值将被忽略。 范围的第一个元素必须小于或等于第二个元素。 range 也会影响自动 bin 计算。 虽然根据范围内的实际数据计算 bin 宽度是最佳的,但 bin 计数将填充整个范围,包括不包含数据的部分。
但是,如果您真正想要的只是标准偏差和均值,那么您实际上并不需要直方图,您可以执行类似的操作:
def filter_values(values, v_min, v_max):
# Flatten the array to a single 1D vector
# See https://numpy.org/doc/stable/reference/generated/numpy.ndarray.flatten.html
flat = np.asanyarray(values).flatten()
# Now index the values using "boolean indexing"
# See https://numpy.org/devdocs/reference/arrays.indexing.html#boolean-array-indexing
return flat[np.logical_and(flat >= v_min,
flat < v_max)]
然后您可以像这样计算平均值和标准差:
filtered_hue = filter_values(hue, v_min=120, v_max=180)
np.mean(filtered_hue), np.std(filtered_hue)
祝你好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.