[英]How to Transform Normalization Image Math Equation to Python?
我嘗試學習如何將方程式轉換為python腳本。
我選擇從指紋增強的學術資源,開始它在這里 。
開始學習,我搜索要增強的指紋圖像。 我選擇這張圖片 :
所以,我要做的第一步就是轉換為灰色:
import cv2
import numpy as np
input = 'PATH OF IMAGE'
img = cv2.imread(input)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
好的,問題從這里開始...
請嘗試理解我,我嘗試學習如何將數學方程式轉換為python腳本。 不要嘗試在Github中尋找其他/現有腳本(例如)。
所有細節都來自學術研究 。 告訴我們:讓I(i,j)表示像素(i,j)的灰度值,M和VAR分別表示I的估計均值和方差,而G(i,j)表示歸一化的灰度-像素(i,j)處的電平值。 灰度指紋圖像I定義為N x N矩陣,其中I(i,j)代表第i行第j列像素的強度。 我們假設所有圖像均以每英寸500點(dpi)的分辨率進行掃描。 灰度指紋圖像I的均值和方差定義為
好的,我們開始轉換方程式:
def mean(gray):
rows, cols = gray.shape
sum = 0
for i in range(0,rows):
for j in range(0, cols):
pix = (gray[i,j].item())
sum += pix
M = sum/N
return M
def var(gray, M):
rows, cols = gray.shape
N = gray.size
sum = 0
for i in range(0,rows):
for j in range(0, cols):
vix = ((img[i,j].item()) - M)**2
sum += vix
VAR = sum/N
return VAR
def normalize(img, M0, VAR0):
M = mean(img)
VAR = var(img, M)
rows,cols = img.shape
normim = np.zeros((rows, cols))
for i in range(0, rows):
for j in range(0, cols):
if (gray[i,j].item()) > M:
G0 = M0 + ((((VAR0)*(((gray[i,j].item())-(M))**2))/(VAR))**(1/2))
normim[i,j] = int(G0)
else:
G1 = M0 - ((((VAR0)*(((gray[i,j].item())-(M))**2))/(VAR))**(1/2))
normim[i,j] = int(G1)
return normim
M0 = 100 #follow the academic research document
VAR0 = 100 #follow the academic research document
normgray = normalize(gray, 100,100)
cv2.imshow('test', normgray)
cv2.waitKey(1)
都是白色的。
有人可以幫我嗎? 請您指教。
提醒您, 我不是要尋找另一個腳本/另一個示例 。 我試圖了解如何將數學方程式轉換為python腳本 。 關於另一個腳本,我已經有了,即使我已經在這里映射了 。
這是一個不考慮轉換之間的數據類型的簡單問題。 具體來說,當您加載圖像時,它將是無符號的8位整數,因此期望值應在[0, 255]
,但您的均值和方差的計算將超出此動態范圍,因此您的計算將溢出。 解決此問題的最快方法是轉換圖像,使其遵循可以處理所需計算精度的數據類型,例如浮點數。 執行計算,完成后將圖像轉換回期望的數據類型,即無符號的8位整數。
此外,您的代碼中還有幾個錯誤。 一方面,您沒有提供變量N
,它應該是圖像中像素的總數。 此外,您的var
函數接受gray
作為變量,但是您正在使用img
嘗試訪問像素數據,因此在嘗試運行它時也會產生錯誤。 最后,您省略了正在使用的軟件包,因此在其中添加了它們。
我還從本地下載了您的圖片,因此我可以運行代碼以驗證其是否有效。 我已經修補了代碼的末尾,以便在按下鍵並將輸出圖像寫入文件后,可以正確關閉顯示結果的圖像窗口。
因此:
# Added so the code can run
import cv2
import numpy as np
# Added so the code can run
gray = cv2.imread('gnN4Q.png', 0)
gray = gray.astype(np.float) # Change to floating-point
N = gray.shape[0]*gray.shape[1]
def mean(gray):
rows, cols = gray.shape
sum = 0
for i in range(0,rows):
for j in range(0, cols):
pix = (gray[i,j].item())
sum += pix
M = sum/N # Added above
return M
def var(gray, M):
rows, cols = gray.shape
N = gray.size
sum = 0
for i in range(0,rows):
for j in range(0, cols):
vix = ((gray[i,j].item()) - M)**2 # Change
sum += vix
VAR = sum/N
return VAR
def normalize(img, M0, VAR0):
M = mean(img)
VAR = var(img, M)
rows,cols = img.shape
normim = np.zeros((rows, cols))
for i in range(0, rows):
for j in range(0, cols):
if (gray[i,j].item()) > M:
G0 = M0 + ((((VAR0)*(((gray[i,j].item())-(M))**2))/(VAR))**(1/2))
normim[i,j] = int(G0)
else:
G1 = M0 - ((((VAR0)*(((gray[i,j].item())-(M))**2))/(VAR))**(1/2))
normim[i,j] = int(G1)
return normim
M0 = 100 #follow the academic research document
VAR0 = 100 #follow the academic research document
normgray = normalize(gray, 100,100)
normgray = normgray.astype(np.uint8) # Added - convert back to uint8
cv2.imshow('test', normgray)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('output.png', normgray)
我們得到的輸出圖像是:
我沒有運行您的代碼,但請確保G0或G1不會太大。 可能是您的值大於255,因此得到的全白圖像。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.