1. 程式人生 > >opencv學習(十三):影象直方圖

opencv學習(十三):影象直方圖

一、安裝matplotlib

在cmd環境下,按照自己安裝的python位置進入Scripts目錄下,輸入命令:pip install matplotlib

二、 繪出圖片的直方圖

程式碼如下:

#匯入cv模組
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

def plot_demo(image):
    plt.hist(image.ravel(),256,[0,256])
    plt.show()

#畫出影象的直方圖
def image_hist(image):
    color = ("blue", "green", "red")
    for i, color in enumerate(color):
        hist = cv.calcHist([image], [i], None, [256], [0, 256])
        plt.plot(hist, color=color)
        plt.xlim([0, 256])
    plt.show()

print("------------Hi,Python!-------------")
# 讀取影象,支援 bmp、jpg、png、tiff 等常用格式
src = cv.imread("F:/Projects/images/3.jpg")
#建立視窗並顯示影象
cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)
cv.imshow("input image",src)   #顯示原圖
plot_demo(src)
image_hist(src)
cv.waitKey(0)
#釋放視窗
cv.destroyAllWindows()

效果如下:

三、直方圖的應用

(1)直方圖的均衡化

         直方圖均衡化:提升對比度的兩種方法:預設、自定義

         程式碼如下:

#匯入cv模組
import cv2 as cv
import numpy as np

#直方圖均衡化是基於灰度圖的,實現對比度增前
def equalHist_demo(image):
    gray=cv.cvtColor(image,cv.COLOR_BGR2GRAY)#提升對比度(預設提升),只能是灰度影象
    dst=cv.equalizeHist(gray)
    cv.imshow("equalHist_demo",dst)

#自定義直方圖均值化
def clahe_demo(image):
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)  # 提升對比度(預設提升),只能是灰度影象
    clahe = cv.createCLAHE(clipLimit=5.0,tileGridSize=(8,8))#clipLimit是對比度的大小,tileGridSize是每次處理塊的大小
    dst=clahe.apply(gray)
    cv.imshow("clahe_demo",dst)


print("------------Hi,Python!-------------")
# 讀取影象,支援 bmp、jpg、png、tiff 等常用格式
src = cv.imread("F:/Projects/images/rise.png")
#建立視窗並顯示影象
cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)
cv.imshow("input image",src)   #顯示原圖
equalHist_demo(src)
clahe_demo(src)
cv.waitKey(0)
#釋放視窗
cv.destroyAllWindows()

   效果如下:

(2)直方圖比較

程式碼如下:

# 匯入cv模組
import cv2 as cv
import numpy as np


# 直方圖均衡化是基於灰度圖的,實現對比度增前
def equalHist_demo(image):
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)  # 提升對比度(預設提升),只能是灰度影象
    dst = cv.equalizeHist(gray)
    cv.imshow("equalHist_demo", dst)


# 自定義直方圖均值化
def clahe_demo(image):
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)  # 提升對比度(預設提升),只能是灰度影象
    clahe = cv.createCLAHE(clipLimit=5.0, tileGridSize=(8, 8))  # clipLimit是對比度的大小,tileGridSize是每次處理塊的大小
    dst = clahe.apply(gray)
    cv.imshow("clahe_demo", dst)


# 直方圖比較

def create_rgb_hist(image):#建立直方圖
    h, w, c = image.shape
    rgbhist = np.zeros([16 * 16 * 16, 1], np.float32)
    bsize = 256 / 16
    for row in range(h):
        for col in range(w):
            b = image[row, col, 0]
            g = image[row, col, 1]
            r = image[row, col, 2]
            index = np.int(b / bsize) * 16 * 16 + np.int(g / bsize) * 16 + np.int(r / bsize)
            rgbhist[np.int(index), 0] = rgbhist[np.int(index), 0] + 1

def hist_compare(image1,image2):
    hist1=create_rgb_hist(image1)
    hist2=create_rgb_hist(image2)
    match1=cv.compareHist(hist1,hist2,cv.HISTCMP_BHATTACHARYYA)#巴氏距離比較
    match2=cv.compareHist(hist1,hist2,cv.HISTCMP_CORREL)#相關性比較
    match3 = cv.compareHist(hist1, hist2, cv.HISTCMP_CHISQR)  #卡方相關性比較
    print("巴氏距離:%s,相關性:%s,卡方:%s")%(match1,match2,match3)



print("------------Hi,Python!-------------")
# 讀取影象,支援 bmp、jpg、png、tiff 等常用格式
src = cv.imread("F:/Projects/images/rise.png")
# 建立視窗並顯示影象
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
# cv.imshow("input image", src)  # 顯示原圖

image1= cv.imread("F:/Projects//images/image1.jpg")
image2= cv.imread("F:/Projects/images/image2.jpg")
cv.imshow("image1",image1)
cv.imshow("image2",image2)

#equalHist_demo(src)
#clahe_demo(src)
hist_compare(image1,image2)

cv.waitKey(0)
# 釋放視窗
cv.destroyAllWindows()

效果如下: