def myHybridImages(lowImage: np.ndarray, lowSigma: float, highImage: np.ndarray, highSigma: float) -> np.ndarray: """ Create hybrid images by combining a low-pass and high-pass filtered pair. :param lowImage: the image to low-pass filter (either greyscale shape=(rows,cols) or colour shape=(rows,cols,channels)) :type numpy.ndarray :param lowSigma: the standard deviation of the Gaussian used for low-pass filtering lowImage :type float :param highImage: the image to high-pass filter (either greyscale shape=(rows,cols) or colour shape=(rows,cols,channels)) :type numpy.ndarray :param highSigma: the standard deviation of the Gaussian used for low-pass filtering highImage before subtraction to create the high-pass filtered image :type float :returns returns the hybrid image created by low-pass filtering lowImage with a Gaussian of s.d. lowSigma and combining it with a high-pass image created by subtracting highImage from highImage convolved with a Gaussian of s.d. highSigma. The resultant image has the same size as the input images. :rtype numpy.ndarray """ # All images are normalised lowfImage = convolve(lowImage / 255, makeGaussianKernel(lowSigma)) highfImage = (highImage / 255) - convolve(highImage / 255, makeGaussianKernel(highSigma)) return (lowfImage + highfImage) * 255 # rescale image to range 0-255
def myHybridImages(lowImage: np.ndarray, lowSigma, highImage: np.ndarray, highSigma): # make kernel low_kernel = makeGaussianKernel(lowSigma) high_kernel = makeGaussianKernel(highSigma) # convolve low-pass pictures low_image = convolve(lowImage, low_kernel) # make high-pass picture high_image = (highImage - convolve(highImage, high_kernel)) # final picture # the weights between and final lighting can be changed flexibly weight = 1 weight2 = 1 adjustment = 0 hybrid_image = high_image * weight2 + low_image * weight + adjustment # hybrid_image = high_image + low_image # randomly double check the output # print(hybrid_image[11][22][1]) # print(hybrid_image[44][55][0]) # print(hybrid_image[357][159][2]) return hybrid_image
def myHybridImages(lowImage: np.ndarray, lowSigma: float, highImage: np.ndarray, highSigma: float) -> np.ndarray: """ Create hybrid images by combining a low-pass and high-pass filtered pair. :param lowImage: the image to low-pass filter (either greyscale shape=(rows,cols) or colour shape=(rows,cols,channels)) :type numpy.ndarray :param lowSigma: the standard deviation of the Gaussian used for low-pass filtering lowImage :type float :param highImage: the image to high-pass filter (either greyscale shape=(rows,cols) or colour shape=(rows,cols,channels)) :type numpy.ndarray :param highSigma: the standard deviation of the Gaussian used for low-pass filtering highImage before subtraction to create the high-pass filtered image :type float :returns returns the hybrid image created by low-pass filtering lowImage with a Gaussian of s.d. lowSigma and combining it with a high-pass image created by subtracting highImage from highImage convolved with a Gaussian of s.d. highSigma. The resultant image has the same size as the input images. :rtype numpy.ndarray """ low_pass = convolve(lowImage, makeGaussianKernel(lowSigma)) high_pass = highImage - convolve(highImage, makeGaussianKernel(highSigma)) # We need to threshold the image. # To avoid different roundings of different displaying functions, I have taken the floor of each # element in the output. # Pillow, the library I used, needed the ndarray to be of type uint8. return np.uint8(np.clip(high_pass + low_pass, 0, 255))
def myHybridImages(lowImage: np.ndarray, lowSigma: float, highImage: np.ndarray, highSigma: float) -> np.ndarray: """ Create hybrid images by combining a low-pass and high-pass filtered pair. :param lowImage: the image to low-pass filter (either greyscale shape=(rows,cols) or colour shape=(rows,cols,channels)) :type numpy.ndarray :param lowSigma: the standard deviation of the Gaussian used for low-pass filtering lowImage :type float :param highImage: the image to high-pass filter (either greyscale shape=(rows,cols) or colour shape=(rows,cols,channels)) :type numpy.ndarray :param highSigma: the standard deviation of the Gaussian used for low-pass filtering highImage before subtraction to create the high-pass filtered image :type float :returns returns the hybrid image created by low-pass filtering lowImage with a Gaussian of s.d. lowSigma and combining it with a high-pass image created by subtracting highImage from highImage convolved with a Gaussian of s.d. highSigma. The resultant image has the same size as the input images. :rtype numpy.ndarray """ #normalize each image lowImage = lowImage.astype(float) highImage = highImage.astype(float) lowImage /= 255 highImage /= 255 low_freq_kernel = makeGaussianKernel(lowSigma) low_pass = convolve(lowImage,low_freq_kernel) high_freq_kernel = makeGaussianKernel(highSigma) high_pass = highImage - convolve(highImage,high_freq_kernel) cv2.imwrite('results/dog_lowpass_gray_2.bmp', low_pass * 255) cv2.imwrite('results/cat_highpass_gray_2.bmp', (high_pass + 0.5) * 255) return (low_pass + high_pass) * 255
import numpy as np from MyConvolution import convolve from scipy.signal import convolve2d # import cv2 iterTest = 10 for i in range(iterTest): row, col = np.random.randint(10, 100), np.random.randint(10, 100) # testImage = np.random.rand(row, col, 3) testImage = np.random.rand(row, col) trow, tcol = np.random.choice([3, 5, 7]), np.random.choice([3, 5, 7]) testKernel = np.random.randint(-5, 5, size=(trow, tcol)) myconv = convolve(testImage, testKernel) # myconv = myconv[~np.all(myconv == 0, axis=(1, 2))] # myconv = myconv[:, ~np.all(myconv == 0, axis=(0, 2))] scipyconv = np.zeros(testImage.shape) scipyconv = convolve2d(testImage, testKernel, mode='same') # for j in range(3): # scipyconv[:, :, j] = convolve2d(testImage[:, :, j], testKernel, mode='same', boundary='fill', fillvalue=0) myconv = myconv.astype(np.float32) scipyconv = scipyconv.astype(np.float32) if (myconv == scipyconv).all(): print(True) else: print(False)
def myHybridImages(lowImage, lowSigma, highImage, highSigma): low1 = convolve(lowImage, makeGaussianKernel(lowSigma)) low2 = convolve(highImage, makeGaussianKernel(highSigma)) high2 = highImage - low2 res = low1 + high2 return res