SciTech-Mathmatics-Statistics-Descriptive Statistics-"Pandas + NumPy" + "Best Ways to Grayscale/"Color Channels Split" Images with Python Using OpenCV+Pandas+NumPy

问题:怎么解释

答案:percentile函数是统计学用于计算数据集的特定百分位数

percentile百分位数 与 percentile()函数

# 示原理代码
img = cv.imread('downloads/Signature.jpg')
# create a Pandas Series from the flatten BLUE channel array of the image.
ss = pd.Series(img[:, :, 0].flatten())
# calculating the **index** of quarter from the total number ss.count()
index_of_quarter = round(ss.count()/4)
# calculated MUST equal with ss.describe()["25%"]
sorted(ss) [index_of_quarter] == ss.describe()["25%"]

  • percentile百分位数: 数据集(先升序排序)上某个特定百分比位置(index序号)数据值.

    • 必须先将数据集由小到大顺序排列, 某个特定百分比数据点值.
    • 例如, 第75个百分位数 表示 有75%的数据点** 低于或等于 它(百分位数)的值.
  • percentile()函数: 统计学用于计算数据集的特定百分位数

    • percentile函数的计算方法依赖于选择的算法。常用的有线性插值法最近邻法等。
      线性插值法是最常用的: 在两个已知数值之间进行线性插值, 来估算百分位数的位置。
    • 统计分析时, percentile函数非常有用:
      通过它可以了解数据分布的特征,如数据的中心趋势和离散程度
      此外它在处理异常值时也表现稳健不会受到极值的影响

Pandas Descriptive statistics

Pandas is more powerful than NumPy for Number and Statistics Processing.
https://pandas.pydata.org/pandas-docs/stable/user_guide/basics.html#descriptive-statistics

import cv2 as cv
import numpy as np
import pandas as pd

# Load the image first
img_path = 'downloads/Signature.jpg'
img = cv.imread(img_path)
assert img is not None, "file could not be read, check with os.path.exists('%s')" % img_path

# Color Channels Splitting and merging
b, g, r =img[:, :, 0], img[:, :, 1], img[:, :, 2]
# img = cv.merge((b, g, r))

# Using Pandas for Number Analysis and Statistics
arr = b
ss, df = pd.Series(arr.flatten()), pd.DataFrame(arr)

# Summarizing data: describe
# There is a convenient describe() function which computes a variety of summary statistics about a Series,
# or the columns of a DataFrame (excluding NAs of course).
ss.describe()

Out[1]:
count    371712.000000
mean        135.706345
std          24.186046
min           3.000000
25%         140.000000
50%         143.000000
75%         145.000000
max         161.000000
dtype: float64

# Pandas: **select specific percentiles**:
#    By default, the median is always included
percentiles = [0.05, 0.25, 0.75, 0.95]
ss.describe(percentiles=percentiles)
Out[1]:
count    371712.000000
mean        135.706345
std          24.186046
min           3.000000
5%           63.000000
25%         140.000000
50%         143.000000
75%         145.000000
95%         149.000000
max         161.000000
dtype: float64

In [2]: sorted(ss) [round(371712/20)]
Out[2]: 63


Here is a quick reference summary table of common functions.
Each also takes an optional level parameter which applies only if the object has a hierarchical index.

Statistics From NumPy Official Docs.

https://numpy.org/doc/stable/reference/routines.statistics.html

  • Order statistics
    numpy.percentile
numpy.percentile(a, q, axis=None, out=None, overwrite_input=False, method='linear', keepdims=False, *, weights=None, interpolation=None)
  • Averages and variances
  • Correlating
  • Histograms

Best Ways to Grayscale Images with Python Using OpenCV

March 11, 2024 by Emily Rosemary Collins

  • Problem Formulation
    In image processing, grayscaling is a procedure of converting color images into shades of gray, indicating that each pixel now represents the intensity of light only, not color. Programmers often need to perform this task to simplify or prepare images for further processing, such as edge detection or thresholding. A common input would be a colored image (JPEG, PNG, etc.), and the desired output is its grayscale version.
  1. cv2
# Load the image
image = cv2.imread('path_to_image.jpg')
# Convert the image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Save the grayscale image
cv2.imwrite('gray_image.jpg', gray_image)

  1. cv2
import cv2

# Load the image directly in grayscale
gray_image = cv2.imread('path_to_image.jpg', cv2.IMREAD_GRAYSCALE)

# Save the grayscale image
cv2.imwrite('gray_image.jpg', gray_image)
  1. cv2
import cv2
import numpy as np

# Load the image
image = cv2.imread('path_to_image.jpg')

# Manually convert to grayscale using channel mixing
weights = [0.1140, 0.5870, 0.2989] # BGR weights
gray_image = np.dot(image[...,:3], weights).astype(np.uint8)

# Save the grayscale image
cv2.imwrite('gray_image.jpg', gray_image)
  1. cv2
import cv2

# Load the image
image = cv2.imread('path_to_image.jpg')

# Split the image into its individual color channels
b, g, r = cv2.split(image)

# Use the red channel for grayscale effect
gray_image = cv2.merge([r, r, r])

# Save the grayscale image
cv2.imwrite('red_channel_gray_image.jpg', gray_image)
  1. cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

# Load the image
image = mpimg.imread('path_to_image.jpg')

# Display the image in grayscale
plt.imshow(image, cmap='gray')
plt.axis('off') # Hide axis
plt.show()
posted @   abaelhe  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
历史上的今天:
2023-07-18 "Management" by Stephen P. Robbins and Mary Coulter现代管理科学理论
点击右上角即可分享
微信分享提示