如何在 OpenCV Python 中規範化影像?


我們使用函式 cv2.normalize()OpenCV 中規範化影像。此函式接受引數 - src、dst、alpha、beta、norm_type、dtypemask。srcdst 是輸入影像和與輸入大小相同的輸出,alpha 是範圍規範化的較低範數值,beta 是範圍規範化的較高範數值,norm_type 是規範化型別,dtype 是輸出的資料型別,mask 是可選的操作掩碼。

步驟

要規範化影像,我們可以按照以下步驟操作 -

  • 匯入所需的庫。在以下所有示例中,所需的 Python 庫是 OpenCV。確保您已安裝它。

  • 使用 cv2.imread() 方法將輸入影像讀取為灰度影像。使用影像型別(即 png 或 jpg)指定影像的完整路徑。

  • 對輸入影像 img 應用 cv2.normalize() 函式。傳遞引數 src、dst、alpha、beta、norm_type、dtypemask

img_normalized = cv2.normalize(img, None, 0, 1.0, cv2.NORM_MINMAX, dtype=cv2.CV_32F)
  • 顯示規範化的輸出影像。

  • 列印規範化前後影像資料。嘗試找出這兩個影像資料之間的差異。

讓我們藉助一些 Python 示例來理解這個問題。

我們將在以下示例中使用此影像作為輸入檔案 -

示例

在此 Python 程式中,我們使用最小-最大範數規範化彩色輸入影像。影像畫素值被規範化到 [0,1] 的範圍。

# import required library import cv2 # read the input image in grayscale img = cv2.imread('jeep.jpg',0) print("Image data before Normalize:\n", img) # Normalize the image img_normalized = cv2.normalize(img, None, 0, 1.0, cv2.NORM_MINMAX, dtype=cv2.CV_32F) # visualize the normalized image cv2.imshow('Normalized Image', img_normalized) cv2.waitKey(0) cv2.destroyAllWindows() print("Image data after Normalize:\n", img_normalized)

輸出

執行上述程式時,將產生以下輸出 -

Image data before Normalize:
[[ 37 37 37 ... 55 55 55]
[ 39 39 39 ... 57 56 56]
[ 39 39 39 ... 56 56 56]
...
[243 244 244 ... 82 85 86]
[242 245 245 ... 83 91 91]
[242 245 245 ... 86 94 93]]
Image data after Normalize:
[[0.14509805 0.14509805 0.14509805 ... 0.21568629 0.21568629 0.21568629]
[0.15294118 0.15294118 0.15294118 ... 0.22352943 0.21960786 0.21960786]
[0.15294118 0.15294118 0.15294118 ... 0.21960786 0.21960786 0.21960786]
...
[0.95294124 0.9568628 0.9568628 ... 0.32156864 0.33333334 0.3372549 ]
[0.9490197 0.9607844 0.9607844 ... 0.3254902 0.35686275 0.35686275]
[0.9490197 0.9607844 0.9607844 ... 0.3372549 0.36862746 0.3647059 ]]

並且我們得到以下顯示規範化影像的視窗 -

示例

在此 Python 程式中,我們使用最小-最大範數規範化二進位制輸入影像。規範化後的影像畫素值要麼為 0 要麼為 1。

# import required library import cv2 # read the input image as grayscale image img = cv2.imread('jeep.jpg',0) print("Image data before Normalize:\n", img) # Apply threshold to create a binary image ret,thresh = cv2.threshold(img,140,255,cv2.THRESH_BINARY) print("Image data after Thresholding:\n", thresh) # normalize the binary image img_normalized = cv2.normalize(thresh, None, 0, 1.0, cv2.NORM_MINMAX, dtype=cv2.CV_32F) # visualize the normalized image cv2.imshow('Normalized Image', img_normalized) cv2.waitKey(0) cv2.destroyAllWindows() print("Image data after Normalize:\n", img_normalized)

輸出

執行上述 Python 程式時,將產生以下輸出 -

Image data before Normalize:
[[ 37 37 37 ... 55 55 55]
[ 39 39 39 ... 57 56 56]
[ 39 39 39 ... 56 56 56]
...
[243 244 244 ... 82 85 86]
[242 245 245 ... 83 91 91]
[242 245 245 ... 86 94 93]]
Image data after Thresholding:
[[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
...
[255 255 255 ... 0 0 0]
[255 255 255 ... 0 0 0]
[255 255 255 ... 0 0 0]]
Image data after Normalize:
[[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
...
[1. 1. 1. ... 0. 0. 0.]
[1. 1. 1. ... 0. 0. 0.]
[1. 1. 1. ... 0. 0. 0.]]

並且我們得到以下顯示規範化二進位制影像的視窗 -

更新於: 2023年8月26日

43K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告