一、圖像直方圖顯示(python)

  1. 圖像處理中繪製圖像直方圖往往是觀察和處理圖像的利器之一。

  2. 直方圖的觀察方面的基本知識:

    • 橫座標代表着灰度級、縱座標是該灰度值在圖像中出現的概率或者次數。
    • 直方圖的型態爲斜態和峯態,斜態指的是直方圖的不對稱的程度,峯態表示的是直方圖的分佈在均值周圍的集中程度。
    • 直方圖可以基本上反映出圖像對比度的基本情況。
  3. 直方圖的基本性質

    • 直方圖沒有位置信息。
    • 直方圖反映了總體灰度分佈。
    • 直方圖具有可疊加性。
    • 直方圖具有統計性。

不多BB,上代碼:

實現一:

from PIL import Image
from pylab import *
import cv2
from tqdm import tqdm
def Rgb2gray(image):
    h = image.shape[0]
    w = image.shape[1]
    grayimage  = np.zeros((h,w),np.uint8)
    for i in tqdm(range(h)):
        for j in range(w):
            grayimage [i,j] = 0.144*image[i,j,0]+0.587*image[i,j,1]+0.299*image[i,j,1]
    return grayimage
# 讀取圖像到數組中,並灰度化
image = cv2.imread("peng.png")

im = array(Image.open('peng.png').convert('L'))
# 直方圖圖像
# flatten可將二維數組轉化爲一維
hist(image.flatten(), 128)
# 顯示
show()

顯示效果圖如下:

在這裏插入圖片描述
實現方法二:

import cv2
import matplotlib.pyplot as plt
import numpy as np
import math
import os
import pandas as pd
from tqdm import tqdm

# 繪製直方圖函數
def grayHist(img):
    h, w = img.shape[:2]
    pixelSequence = img.reshape([h * w, ])
    numberBins = 256
    histogram, bins, patch = plt.hist(pixelSequence, numberBins,
                                      facecolor='black', histtype='bar')
    plt.xlabel("gray label")
    plt.ylabel("number of pixels")
    plt.axis([0, 255, 0, np.max(histogram)])
    plt.show()

image = cv2.imread("peng.png",0)
grayHist(image)

顯示效果如下:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章