简体   繁体   English

直方图、均值、标准差的正态分布,在数组的某个部分内

[英]Normal distribution of histogram, mean, standard deviation, within a certain part of an array

I have a lot of different images of a particular object in a folder.我在一个文件夹中有很多特定 object 的不同图像。 I want to set up histograms of these images for Hue, Saturation, and Value.我想为色调、饱和度和值设置这些图像的直方图。 It has worked so far.到目前为止它已经奏效了。

On the other hand, I want to fit a normal distribution to the histograms, get the mean and standard deviation.另一方面,我想将正态分布拟合到直方图,得到均值和标准差。 I ran into a problem with the Hue histogram.我遇到了色调直方图的问题。 The histogram shows that it has two parts up to [0-50] and [120-180].直方图显示它有两个部分,分别为 [0-50] 和 [120-180]。

My question is how could I only get the mean and standard deviation in the [120-180] range because, I don't know how to code this.我的问题是我怎么才能得到 [120-180] 范围内的平均值和标准差,因为我不知道如何编码。

在此处输入图像描述

In the total_hue_list , I tried to count all the occurrences of the different color codes.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()

Just a quick pointer before we start - Hue is cyclic, so assuming your maximal hue is 180, then the hue values 0 and 1 are as close to one another as 180 to 0. I'm not sure from your code if 180 is indeed the max or not, but just putting this fact out there in case it matters.在我们开始之前只是一个快速指针 - 色调是循环的,所以假设你的最大色调是 180,那么色调值 0 和 1 就像 180 到 0 一样接近。我不确定你的代码是否确实是 180最大值与否,但只是把这个事实放在那里以防万一。

Back on topic - if I understood correctly, you only want to consider pixels with hue values that are between 120 and 180, and ignore all the other values.回到主题 - 如果我理解正确,您只想考虑色调值在 120 到 180 之间的像素,而忽略所有其他值。 If that is indeed the case, then np.histogram has a value designed specifically for this.如果确实如此,则np.histogram具有专门为此设计的值。 Quoting the documentation :引用文档

range(float, float) , optional The lower and upper range of the bins. range(float, float) ,可选 bin 的下限和上限。 If not provided, range is simply (a.min(), a.max()).如果没有提供,范围就是(a.min(), a.max())。 Values outside the range are ignored.超出范围的值将被忽略。 The first element of the range must be less than or equal to the second.范围的第一个元素必须小于或等于第二个元素。 range affects the automatic bin computation as well. range 也会影响自动 bin 计算。 While bin width is computed to be optimal based on the actual data within range, the bin count will fill the entire range including portions containing no data.虽然根据范围内的实际数据计算 bin 宽度是最佳的,但 bin 计数将填充整个范围,包括不包含数据的部分。

However, if what you really want is just the standard deviation and mean, then you don't really need the histogram, you can just do something similar to this:但是,如果您真正想要的只是标准偏差和均值,那么您实际上并不需要直方图,您可以执行类似的操作:

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)]

And you can then compute the mean and standard deviation like so:然后您可以像这样计算平均值和标准差:

filtered_hue = filter_values(hue, v_min=120, v_max=180)
np.mean(filtered_hue), np.std(filtered_hue)

Good luck!祝你好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 生成一个 numpy random 查看从均值 1 和标准差 2 的正态分布中抽取的 100 个随机数的数组 - Generating a numpy random see array of 100 random numbers drawn from a normal distribution with mean 1 and standard deviation 2 求出特定点距cdf的正态标准偏差和分布平均值 - Find normal standard deviation from the cdf at a specific point and the distribution mean 给定均值和标准差,生成二维正态分布 - Generate two-dimensional normal distribution given a mean and standard deviation 给定均值和标准差,如何计算正态分布的概率? - How to calculate probability in a normal distribution given mean & standard deviation? 如何在Python中获取600个值的均值为零且标准偏差为σ= 2 dB的对数正态分布(即以dB为单位的正态分布)? - How to get log-normal distribution (i.e. a normal distribution in dB) with a zero mean and a standard deviation of σ = 2 dB for 600 values in python? 使用 seaborn 库从最佳拟合正态分布中获取均值和标准差 - getting mean and standard deviation from best-fit normal distribution using seaborn library 为多个元素创建具有均值和标准差的直方图 - Create histogram with mean and standard deviation for multiples elements 当在 python 中为正态分布绘制 pdf 并且标准偏差为 2 时,平均值是否仍然为零? - when plotting a pdf in python for a normal distribution and the standard deviation is 2, will the mean still be zero? 标准C或Python库,用于计算正态分布的标准偏差 - Standard C or Python libraries to compute standard deviation of normal distribution 将阵列标准化为标准正态分布 - Normalize an Array to Standard Normal Distribution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM