def jpeg(img, div, fft=False, display8x8=False): stop = False display = False matSize = 8 # size of matrix blocks to perform dct imgCopy = img.copy() idm = classes.ImageDisplayManager() # begin scanning for rowStart in range(0, 512, matSize): for colStart in range(0, 512, matSize): # take a matSize x matSize block imgSlice = img[rowStart:rowStart + matSize, colStart:colStart + matSize] if (not fft): # compute the discrete cosine transform of the block B = cv2.dct(imgSlice.astype('float32')) # quantize the block of DCT coefficients Bprime = cf.quantize(B, div) # calculate the inverse dct B = cv2.dct(Bprime.astype('float32'), B, cv2.DCT_INVERSE) else: # compute the discrete cosine transform of the block B = np.fft.fft2(imgSlice) # quantize the block of DCT coefficients Bprime = cf.quantize(B, div) # calculate the inverse dct B = np.fft.ifft2(Bprime) # replace the quantized blocks in the image's copy imgCopy[rowStart:rowStart + matSize, colStart:colStart + matSize] = B # display the 8 x 8 blocks for human inspection if display8x8: # display an image with another resolution on a resizable window idm.showImg(imgSlice, '8 x 8') return imgCopy
return imgCopy # --------------------------------------------------------------------- # # MAIN # # --------------------------------------------------------------------- if __name__ == "__main__": # clear screen sp.call('cls', shell=True) # initialize ImageDisplayManager idm = classes.ImageDisplayManager() # load a color image in grayscale img = cv2.imread('./test images/peppers_gray.tif', 0) idm.add(img, 'Original image') # perform quantization, measure time taken, and add to display list div = 2 # divisor used for quantization tmStart = time.time() imgQuantized = quantize(img, 2) tmEnd = time.time() tmElapsed = round((tmEnd - tmStart) * 1000, 2) print('Quantization time elapsed = {} ms'.format(tmElapsed)) idm.add(imgQuantized, 'Quantized image, div = {}'.format(div)) # perform averaging of pixels based on neighboring pixels