当前位置:网站首页>Definition, significance and characteristics of histogram

Definition, significance and characteristics of histogram

2022-07-08 02:20:00 So come on

The definition of histogram 、 Meaning and characteristics

1. Definition

In statistics , Histogram is a graphical representation of the distribution of data , It's a two-dimensional statistical chart , His two coordinates are statistical samples ( Images 、 Video frame ) And some attribute of the sample ( brightness , Pixel values , gradient , Direction , Color and so on ).

2. significance

(1) Histogram is a graphical representation of the intensity distribution of pixels in an image .

(2) The histogram counts the number of pixels of each intensity value .

3. features

(1) Histogram no longer represents any image texture information , But the statistics of image pixels .

(2) Because the same object, whether rotating or translating, has the same gray value in the image , Therefore, the histogram has translation invariance 、 Zoom invariance and other advantages .

4. Methods and parameters

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

(1)images : Integer type (uint8 and float32) Original graph (list Form shows ).

(2)channels : The index of the channel , for example :[0] Represents a grayscale image ,[0],[1],[2] Represents multi-channel .

(3)mask : Calculate the histogram of the specified area of the picture . If mask by none, Then calculate the whole picture .

4)histSize( bins ) : Each hue value ( Range : 0 ~ 255) The corresponding number of pixels / frequency .[ this 256 Each of the values is called bin, Its value is 8,16,32,64,128,256. stay OpenCV in , use histSize Express bins.]

5)range : Range of strength values ,[0, 256].

# 1  Import library 
import cv2
import matplotlib.pyplot as plt
import numpy as np

# 2  Method : display picture 
def show_image(image, title, pos):
    #   Sequential conversion :BGR TO RGB
    image_RGB = image[:, :, ::-1] # shape : (height, width, channel) -1 Reverse order 
    #  Show title 
    plt.title(title)
    plt.subplot(2, 1, pos) #  location 
    plt.imshow(image_RGB)

# 3  Method : Display the gray histogram of the picture 
def show_histogram(hist, title, pos, color):
    #  Show title 
    plt.title(title)
    plt.subplot(2, 1, pos) #  Positioning pictures 
    plt.xlabel("Bins") #  Horizontal axis information 
    plt.ylabel("Pixels") #  Vertical axis information 
    plt.xlim([0, 256]) #  Range 
    plt.plot(hist, color=color) #  Draw histogram 


# 4  The main function  main()
def main():
    # 5  Create a canvas 
    plt.figure(figsize=(15, 6)) #  Canvas size 
    plt.suptitle("Gray Image Histogram", fontsize=14, fontweight="bold") #  Set the title form 

    # 6  Loading pictures 
    img = cv2.imread("children.jpg")

    # 7  Gray scale conversion 
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # 8  Calculate the histogram of gray image 
    hist_img = cv2.calcHist([img_gray], [0], None, [256], [0, 256])# list 

    # 9  Display gray histogram 
    #  Gray scale image is converted into BGR Format picture 
    img_BGR = cv2.cvtColor(img_gray, cv2.COLOR_GRAY2BGR)
    show_image(img_BGR, "BGR image", 1)
    show_histogram(hist_img, "gray image histogram", 2, "m")

    plt.show()

if __name__ == '__main__':
    main()

原网站

版权声明
本文为[So come on]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130539412448.html