简体   繁体   English

如何将归一化图像数学方程转换为Python?

[英]How to Transform Normalization Image Math Equation to Python?

I try to learn how to transform equation to python script. 我尝试学习如何将方程式转换为python脚本。

I choose to start it from FingerPrint Enhancement from Academic resources here . 我选择从指纹增强的学术资源,开始它在这里

to start learn i search a fingerprint image to be enhance. 开始学习,我搜索要增强的指纹图像。 I choose this image : 我选择这张图片

在此处输入图片说明

so, i do the first step is converting to gray: 所以,我要做的第一步就是转换为灰色:

import cv2
import numpy as np

input = 'PATH OF IMAGE'
img = cv2.imread(input)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

and below is the result: 结果如下: 在此处输入图片说明

ok the problem start from here... 好的,问题从这里开始...

please try to understood me, I try to learn how to convert math equation to python script. 请尝试理解我,我尝试学习如何将数学方程式转换为python脚本。 not try to looking for another / existing script in Github (for example). 不要尝试在Github中寻找其他/现有脚本(例如)。

the equation is: 等式是: 在此处输入图片说明

all detail from the academic research . 所有细节都来自学术研究 Told that: Let I(i, j) denote the gray-level value at pixel (i, j), M and VAR denote the estimated mean and variance of I, respectively, and G(i, j) denote the normalized gray-level value at pixel (i, j). 告诉我们:让I(i,j)表示像素(i,j)的灰度值,M和VAR分别表示I的估计均值和方差,而G(i,j)表示归一化的灰度-像素(i,j)处的电平值。 A gray-level fingerprint image, I is defined as an N x N matrix, where I(i, j) represents the intensity of the pixel at the i-th row and j-th column. 灰度指纹图像I定义为N x N矩阵,其中I(i,j)代表第i行第j列像素的强度。 We assume that all the images are scanned at a resolution of 500 dots per inch (dpi). 我们假设所有图像均以每英寸500点(dpi)的分辨率进行扫描。 The mean and variance of a gray-level fingerprint image, I, are defined as 灰度指纹图像I的均值和方差定义为

在此处输入图片说明

and 在此处输入图片说明 respectively 分别

ok, we start to transform the equation: 好的,我们开始转换方程式:

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)

the result is out of expected: 结果超出预期: 在此处输入图片说明

all is white. 都是白色的。

can somebody help me? 有人可以帮我吗? please your advise. 请您指教。

to remind you, I'm not try to looking for the another script / another example . 提醒您, 我不是要寻找另一个脚本/另一个示例 I try to understood how to transform a math equation to python script . 我试图了解如何将数学方程式转换为python脚本 about another script, i already have, even i already map it here . 关于另一个脚本,我已经有了,即使我已经在这里映射

This is a simple problem of not respecting the data types in between transformations. 这是一个不考虑转换之间的数据类型的简单问题。 Specifically, when you load in the image, it is going to be unsigned 8-bit integer so the expected values should be within [0, 255] , yet your calculations for the mean and variance will exceed this dynamic range and thus your calculations will overflow. 具体来说,当您加载图像时,它将是无符号的8位整数,因此期望值应在[0, 255] ,但您的均值和方差的计算将超出此动态范围,因此您的计算将溢出。 The quickest way to resolve this problem is to convert your image so that it will respect a data type that can handle the precision of the calculations you want, like floating-point. 解决此问题的最快方法是转换图像,使其遵循可以处理所需计算精度的数据类型,例如浮点数。 Perform the calculations, and when you're done convert the image back to the expected data type, so unsigned 8-bit integer. 执行计算,完成后将图像转换回期望的数据类型,即无符号的8位整数。

In addition, there are several errors in your code. 此外,您的代码中还有几个错误。 For one thing, you didn't provide the variable N , which should be the total number of pixels in the image. 一方面,您没有提供变量N ,它应该是图像中像素的总数。 In addition, your var function accepts gray as the variable yet you are using img to try and access pixel data, so this will also give off an error when you try and run it. 此外,您的var函数接受gray作为变量,但是您正在使用img尝试访问像素数据,因此在尝试运行它时也会产生错误。 Finally, you omitted the packages you're using so I added these in. 最后,您省略了正在使用的软件包,因此在其中添加了它们。

I've also downloaded your image locally so I can run the code to verify that it works. 我还从本地下载了您的图片,因此我可以运行代码以验证其是否有效。 I've patched up the end of your code so that the image window that displays the result properly closes after you push a key and I've written the output image to file. 我已经修补了代码的末尾,以便在按下键并将输出图像写入文件后,可以正确关闭显示结果的图像窗口。

Therefore: 因此:

# 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)

The output image we get is: 我们得到的输出图像是:

产量

I didn't run your code but make sure G0 or G1 doesn't get too big. 我没有运行您的代码,但请确保G0或G1不会太大。 It could be that your value is above 255, thus the resulting all-white image. 可能是您的值大于255,因此得到的全白图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM