OpenCV note finishing [histogram and mask]

1. Histogram:

The histogram counts the number of occurrences of each gray level in the image, the abscissa is the gray level of each pixel, and the ordinate is the number of pixels with the gray level.
insert image description here
We can imagine the nine-square grid above as an image, and the numbers in it represent the gray level of the current pixel.

Displayed in a chart: the X-axis represents the gray level, and the Y-axis represents the number of statistics.
insert image description here
In actual processing, the X axis represents 256 gray levels [0-255] of 8 bits, and the Y axis represents the number of pixels with corresponding gray levels.
Example: Count the histogram of this black 8 image on a pure white background
insert image description here
insert image description here

2. Draw a histogram:

The module matplotlib.pyplot provides a drawing framework similar to MATLAB, where the hist() function can be used to draw histograms.

hist( X,BINS)

  • X is the data source, which must be one-dimensional. The two-dimensional image needs to be converted into a one-dimensional array by the ravel() function and then passed to the hist() function.
  • BINS is the grouping of gray levels, which is equivalent to the previous Y.

Above code:

import cv2
import matplotlib.pyplot as plt

o=cv2.imread("image\\8.bmp")
cv2.imshow("original",o)
# 显示直方图
plt.hist(o.ravel(),256) # 将o转换为一维数组,然后显示256个等级【0-255】

cv2.waitKey()
cv2.destroyAllWindows()

3. Draw a histogram in OpenCV:

hist = cv2.calcHist( images, channels, mask, histSize, ranges, accumulate )

  • hist returns a one-dimensional array, the number of pixels in each gray level in the array.
  • images are original images, pay attention to brackets with [].
  • Channels are channels, the single channel is [0], and the color channels use [0], [1], [2] to represent B, G, and R respectively.
  • mask is the mask, if there is no mask, pass None.
  • histSize represents a subset of BINS, pay attention to brackets with [].
  • ranges represent the pixel range, such as single channel [0,255].
  • accumulate is temporarily empty.

Above code:

import cv2
import numpy as np

img=cv2.imread("image\\hist.png")
# 计算直方图
hist = cv2.calcHist([img],[0],None,[2],[0,256]) # 这里像素范围[0,255]只能返回0灰
# 度等级的像素数,至于原因求高手分析???

print(hist)

cv2.waitKey()
cv2.destroyAllWindows()

run:
insert image description here

Use the plot channel to extract the histogram code:

import cv2
import matplotlib.pyplot as plt

o=cv2.imread("image\\girl.bmp")
cv2.imshow("original",o)
histb = cv2.calcHist([o],[0],None,[256],[0,255]) # channels中[0][1][2]分别代表BGR通道
histg = cv2.calcHist([o],[1],None,[256],[0,255])
histr = cv2.calcHist([o],[2],None,[256],[0,255])

plt.plot(histb,color='b') 
plt.plot(histg,color='g')
plt.plot(histr,color='r')
plt.show()

run:
insert image description here

4. Plot the histogram after the mask:

AND operation:
When M=1: O AND M = original image O
When N=0: O AND M = 0 (zero)
insert image description here
to construct a mask image

import cv2
import numpy as np

mask=np.zeros([600,600],np.uint8) #构造一个像素为0的二维数组
mask[200:400,200:400]=255 # 将X200-X400、Y200-Y400之间的数组值改为255

cv2.imshow('mask',mask)
cv2.waitKey()
cv2.destroyAllWindows()

Run:
insert image description here
Plot a histogram of the masked image:

import cv2
import numpy as np
import matplotlib.pyplot as plt

image=cv2.imread("image\\girl.bmp",cv2.IMREAD_GRAYSCALE)

# 创建一个原图一个大小的二维数组,元素0黑色
mask=np.zeros(image.shape,np.uint8)
# 掩模,改变200-400区域为白色
mask[200:400,200:400]=255

# 将原图和掩模图进行与运算,黑色区域被消除
m=cv2.bitwise_and(image,mask)

# 计算原图和掩模后图像的直方图
histImage=cv2.calcHist([image],[0],None,[256],[0,255])
histMI=cv2.calcHist([image],[0],mask,[256],[0,255])
plt.plot(histImage,color='r')
plt.plot(histMI,color='g')

cv2.imshow("o",image)
cv2.imshow("mask",mask)
cv2.imshow("maskImg",m)

cv2.waitKey()
cv2.destroyAllWindows()

Run:
insert image description here
insert image description here
Over. . . . . . . . . . . . . . .

Guess you like

Origin blog.csdn.net/qq_34699535/article/details/123294016