In [2]:
import os
import cv2
import numpy as np
imagesList = list() # all images read from input folder (50)
INPUT_PATH = "./images/" # input folder
OUTPUT_PATH = "./results/" # output folder
# a function to display an image to screen and save it to a file
def showImageAndSaveToFile(img,output):
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow('image', img)
cv2.waitKey(0) and cv2.destroyAllWindows()
cv2.imwrite(output, img)
for fileName in os.listdir(INPUT_PATH): # read all images in input folder
imagesList.append(cv2.imread(INPUT_PATH + fileName, cv2.IMREAD_GRAYSCALE))
xSize, ySize = imagesList[0].shape # get their shape
imgSum = np.zeros((xSize, ySize), dtype=np.uint16) # temporary matrix to store the sum of various uint8 matrixes (the images)
imgAvg = np.zeros((xSize, ySize), dtype=np.uint8) # matrix of the average of all the images read
for img in imagesList:
imgSum = np.add(imgSum, img)
imgSum = np.divide(imgSum,len(imagesList)) # get average
imgAvg = imgSum.astype(np.uint8) # convert to uint8, expected by OpenCv
showImageAndSaveToFile(imgAvg, OUTPUT_PATH + "imgAvg.jpeg") # show the average image
imgBuff = np.zeros((xSize, ySize), dtype=np.float64) # temporary matrix to receive the computation of average noise image
imgNes = np.zeros((xSize, ySize), dtype=np.uint8) # matrix to output the computation done in the temporary matrix
for img in imagesList: #computing average noise
imgBuff = np.add(imgBuff,np.subtract(imgAvg, img)**2)
imgBuff = np.square(np.divide(imgBuff, len(imagesList)))
imgNes = imgBuff.astype(np.uint8) # convert to uint8, expected by OpenCv
showImageAndSaveToFile(imgNes, OUTPUT_PATH + "imgNoiseAvg.jpeg") # show the average noise image
avgNoise = (np.sum(imgNes))/(xSize*ySize) # average noise escalar
print(avgNoise)