def _downscale_image(self, image: Image) -> Image: scale_size = self._calculate_scaled_size(image.size) inv_scale_size = self._inverse_tuple(scale_size) scaled_pixels = cv2.resize(image._pixels, inv_scale_size, interpolation=cv2.INTER_LINEAR) return Image(scaled_pixels)
def highpass_filter(image: Image, freq=10) -> Image: f = np.fft.fft2(image.src) f_shift = np.fft.fftshift(f) rows, cols = image.dimensions crow, ccol = int(rows / 2), int(cols / 2) f_shift[crow - freq:crow + freq, ccol - freq:ccol + freq] = 0 f_ishift = np.fft.ifftshift(f_shift) img_back = np.fft.ifft2(f_ishift) img_back = np.abs(img_back) return Image(np.uint8(img_back))
def main(): image_path = sys.argv[1] casc_path = sys.argv[2] image = Image(image_path) image_loaded = image.get_image() preprocessor = Preprocessor(image_loaded) image_processed = preprocessor.preprocess() detector = Detector(casc_path) faces = detector.detect(image_processed) print "Found {0} faces!".format(len(faces)) print "Faces: ", faces visualizer = Visualizer(image.get_image()) visualizer.viz(faces) cv2.waitKey(0)
def __init__(self, argv: list, argc: int): """ argv: ./__main__.py [SOURCE] [ALGORITHM] [MODE] [COMPRESSION] [OVERFLOW] [TARGET] argc: number of command line arguments Methods: validImageFile validExtension validCompression getValidInput saveLog run """ self.name = argv[0] self.validExt = (".jpg", ".jpeg", ".png", ".tif") self.initialized = False if argc == 2: print(self.usage() + "\n") elif argc == 1 or argc > 8: print(self.usage()) return print("Initializing...") self.imgName = None self.source = None self.path = None self.algorithm = None self.mode = None self.compression = None self.overflow = False self.shouldLog = False self.target = None self.resourcePath = os.path.join(os.getcwd(), "input") self.outputPath = os.path.join(os.getcwd(), "output") self.variances = { "low": 90., "medium": 95., "high": 99.99, } # parse cmd line arguments and get user input if argc >= 2: imgName = argv[1] self.source = argv[1] # will get changed to full path later if argc >= 3: self.algorithm = argv[2] if argc >= 4: self.mode = argv[3] if argc >= 5: self.compression = argv[4] if argc >= 6: try: self.overflow = bool(int(argv[5])) except: self.overflow = not bool( self.getValidInput( "\nAllow overflow?\n\t1) True\n\t2) False", int, valid={1, 2}) - 1) if argc >= 7: try: self.shouldLog = bool(int(argv[6])) except: self.shouldLog = not bool( self.getValidInput( "\nLog data?\n\t1) True\n\t2) False: ", int, valid={1, 2}) - 1) if argc == 8: self.target = argv[7] if not self.validExtension(self.target): self.target = self.getValidInput( "\nInput valid target file name: ", str, isValid=self.validExtension) if self.source is None or not self.validImageFile( self.source): # get valid image file name available = os.listdir(self.resourcePath) if len(available) == 0: print( "No images in \"images\" folder. Add more and try again..." ) return options = "" for imageName in available: options += "\n\t- " + imageName self.source = self.getValidInput( "\nSelect valid file from \"images\" folder." + options, str, isValid=self.validImageFile) self.path = os.path.join(self.resourcePath, self.source) self.imgName = "" for l in imgName: if l != ".": self.imgName += l else: break self.image = Image(self.imgName, self.path) if self.algorithm is None or self.algorithm not in { "pca", "svd" }: # check which algorithm user wants to use choice = self.getValidInput( "\nWhich algorithm would you like to use?\n\t1) PCA\n\t2) SVD", int, valid={1, 2}) if choice == 1: self.algorithm = "pca" else: # choice == 2 self.algorithm = "svd" if self.mode is None or self.mode not in { "v", "c", "q" }: # check which mode user wants to use if self.algorithm == "pca": choice = self.getValidInput( "\nHow would you like to determine compression?\n\tv) Variance\n\tc) Component\n\tq) Qualitative", str, valid={"v", "c", "q"}) else: # self.algorithm == "svd" choice = "c" self.mode = choice if self.algorithm == "svd" and not self.mode == "c": print("Invalid mode for SVD: " + str(self.mode) + "\n\t- setting mode to \"c\", \"components\"") self.mode = "c" self.compression = None if self.compression is None or not self.validCompression( self.mode, self.compression): # get value for compression if self.mode == "v": # mode is variance choice = self.getValidInput( "\nHow much variance would you like to retain? (0, 100): ", float, lower=0, upper=100) elif self.mode == "c": # mode is components choice = self.getValidInput( "\nHow many components would you like to use? (0, " + str(self.image.data.shape[0]) + "): ", int, lower=0, upper=self.image.data.shape[0]) else: # mode is qualitative quality = { 1: "high", 2: "medium", 3: "low", } choice = quality[self.getValidInput( "\nHow much quality would you like?\n\t1) High\n\t2) Medium\n\t3) Low", int, valid={1, 2, 3})] self.compression = choice elif self.mode == "v": # compression passed through command line self.compression = float(self.compression) elif self.mode == "c": # compression passed through command line self.compression = int(self.compression) if self.mode == "q": self.compression = self.variances[self.compression] self.mode = "v" self.initialized = True
def histogram_equalization(img: Image) -> Image: # create a CLAHE object (Arguments are optional). clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(2, 2)) equalized = clahe.apply(img.grayscale) return Image(equalized)
def laplacian_gradient(image: Image): tmp = cv2.Laplacian(image.src, cv2.CV_64F) tmp = np.absolute(tmp) return Image(np.uint8(tmp))
def _upscale_image(self, image: Image, original_size: tuple) -> Image: inv_size = self._inverse_tuple(original_size) upscaled_pixels = cv2.resize(image._pixels, inv_size, interpolation=cv2.INTER_LANCZOS4) return Image(upscaled_pixels)