I have many grayscale images that I want to normalize by using mean and standard deviation. I use the following process:
Calculate the image's mean and standard deviation.
Subtract the mean from the image.
Divide the resulting image by the standard deviation.
However, I got a black image as a result. What is wrong in my code?
    import cv2
    img = cv2.imread('E.png')   # read an image 
    gray_image = cv2.cvtColor(img , cv2.COLOR_BGR2GRAY)  # converting the image to grayscale image
    img = cv2.resize(gray_image, (60, 60))  # Resize the image to the size 60x60 pixels
    cv2.imwrite("Grayscale Image.png",img)  #To write the result  
    mean, stdDev = cv2.meanStdDev(img)  #Get Mean and Standard-deviation
    image = (img-mean)/stdDev  #Normalization process
    cv2.imwrite("Normalized Image.png",image)  #To write the result 
Input image : 
Grayscale output: 
Normalized image output: 
When you save the image you need to consider the data type. To save the normalized image as png, you need to scale the normalized values to integer range (such as [0, 255]) or use image format that supports floating point format.
When using z-score normalization (as in your code), you could save it as png with
image -= image.min() 
image /= image.max()
image *= 255 # [0, 255] range
cv2.imwrite("Normalized Image.png", image)
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With