Yongxing notes -OpenCV-7 basic introduction histogram 1

Here Insert Picture Description

First, what is the image histogram:

Histogram (the Histogram), also known as the mass distribution, a statistical report is a diagram of a case where data series of unequal height distributed in longitudinal stripes or line segment. Generally horizontal axis indicates the data type, the vertical axis represents the distribution.
Graphic image histogram is accurate numerical representation of the data distribution.

  • Histogram Color:
    the color histogram is a color feature in many image retrieval systems are widely used. It describes that the ratio occupied by different colors in the entire image, but does not care which of the spatial position of each color, i.e., not described object in the image or object. Color histogram is particularly suitable for those described for automatic segmentation of the image difficult.
    • Histogram (we use more of):
      is simply count the number of each pixel value. For example, a grayscale value of the pixel values of the pixels in the number of 88, the number of pixel values of 66 pixels.
      Here Insert Picture Description
      Here Insert Picture Description

Second, why should use the histogram:

Since the image histogram to calculate a smaller cost of its many advantages, and an image translation, rotation, scaling invariance, widely used in various fields of image processing, in particular the threshold gray image segmentation, color-based image retrieval and image classification.

  • Why histogram to calculate a smaller price?
    For example, a 1024x1024 image, when converted into the histogram for its packet, such as the column 255 into groups, then this is superimposed 1024x124 pixels assigned to this group 255, i.e., while the processing from the value becomes large, but the fewer pixels, does not require additional work to split rgb (except hsv) tri-color, no single-step calculation, this can only be calculated by a specific algorithm group 255, so that the cost of a small of!
  • The color histogram have the following meanings:
      ● the color histogram is a graphic expression of pixels in the image intensity distribution.
      ● It counts the number of pixels each having an intensity value.
  • Common applications of image histogram direction:
    • 图像分割:
      图像分割是图像识别的基础,对图像进行图像分割,将目标从背景区域中分离出,可以避免图像识别时在图像上进行盲目的搜索,大大提高图像识别的效率以及识别准确率。基于灰度直方图的阈值分割计算简单,适用于目标与背景分布于不同灰度范围的灰度图像,特别是遥感图像。
    • 图像检索:
      图像检索是指快速有效地从大规模图像数据库中检索出所需的图像,是目前一个非常重要又富有的挑战性的研究课题。颜色特征由于其直观性、计算代价较小等优点,在图像检索中扮演着重要角色,早期的图像检索算法也主要利用颜色特征,特别是颜色直方图。
    • 图像分类:
      图像分类任务主要是对一组图进行一系列自动处理,最终确定图形所属的类别。图像分类具有广泛的应用前景,是计算机视觉的难点问题。针对图像分类的算法众多,其中以基于bag-words模型的方法最为经典有效。该方法首先利用提取的颜色、形状等特征构建视觉词典,然后在图像上统计视觉词的直方图,最后利用视觉词直方图作为特征运用分类器进行分类决策。

三、绘制直方图:

1、Python图像可视化简单介绍:

#安装 matplotlib 
pip install matplotlib
#导入 
import matplotlib.pyplot as plt 
plt.plot(*args, scalex=True, scaley=True, data=None, **kwargs)

点击打开:介绍教程

plt.title(label, fontdict=None, loc='center', pad=None, **kwargs)
  • label : 图像名称

2、绘制直方图:
hist = cv2.calcHist(images, channels, mask, histSize, ranges, hist=None, accumulate=None)

  • images : 绘制直方图的图像
  • channels : 要计算的通道数,灰度图写[0]就行,彩色图B/G/R分别传入[0]/[1]/[2]。
  • mask : 图像掩膜 , 如果需要计算整个区域 则 填写 None
  • histSize : 子区段数目
  • ranges : 需要计算的像素范围
  • hist :传入的 hist 直方图
  • accumulate: Boolean value that indicates whether incoming hist cleared. Not clear if multiple images can be cumulative histogram.

return value:

  • hist: histogram data
import cv2
import matplotlib.pyplot as plt
img = cv2.imread("first.jpg",0) #以灰度模式读取图像
hist = cv2.calcHist([img],[0],None,[256],[0,256])
plt.plot(hist),plt.title("hist")
plt.show()
cv2.imshow("img",img)
cv2.waitKey()
cv2.destroyAllWindows()

Here Insert Picture Description
Here Insert Picture Description

hist , bins = np.histogram(a, bins=10, range=None, normed=None, weights=None,density=None)
参数:

  • a: an array of image pixels
    effect Ravel () is the dimensionality reduction matrix or multidimensional array of one-dimensional
  • The number of subsections: bins
  • range: range of pixel values ​​to be calculated, typically [0,256]

return value:

  • hist: histogram data
  • bins: Returns an array of values ​​of a sub-segment boundary point
import numpy as np
import cv2
import matplotlib.pyplot as plt
img = cv2.imread("first.jpg",0) # 以灰度模式读取图像
hist , bins = np.histogram(img.ravel(),256,[0,256])
print(bins)
plt.plot(hist)
plt.title("hist")
plt.show()
cv2.imshow("img",img)

Here Insert Picture Description
Here Insert Picture Description

Exercise 7-1
draw your own favorite color histogram of the image.

Comment out your answer

Published 45 original articles · won praise 28 · views 10000 +

Guess you like

Origin blog.csdn.net/m0_43505377/article/details/103805865