def gui(): img = imutils.imread(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..', 'data', 'flamingo.jpg')) app = QApplication(['Image Plot']) main_widget = DemoApplication() main_widget.displayImage(img) main_widget.show() sys.exit(app.exec_())
def __load_request(self): filename, _ = QFileDialog.getOpenFileName(self, "Open image", "", 'Images (*.bmp *.jpg *.jpeg *.png *.ppm);;All Files (*.*)', '', QFileDialog.DontUseNativeDialog) if filename is not None: img = imutils.imread(filename) self.displayImage(img)
def demo(): #TODO separate assets folder, use abspath img = imutils.imread('flamingo.jpg') rect = (180, 170, 120, 143) target_template = imutils.roi(img, rect) imvis.imshow(target_template, 'Template', wait_ms=10) warped, H_gt = _generate_warped_image(img, -45, -25, 20, 30, -30, -360) imvis.imshow(warped, 'Simulated Warp', wait_ms=10) # Initial estimate H0 H0 = np.eye(3, dtype=float) H0[0, 2] = rect[0] H0[1, 2] = rect[1] _logger.info(f'Initial estimate, H0:\n{H0}') # print('H0\n', H0) # print('H_gt\n', H_gt) verbose = True pyutils.tic('FC') align = Alignment(target_template, Method.FC, full_reference_image=img, num_pyramid_levels=5, verbose=verbose) align.set_true_warp(H_gt) H_est, result = align.align(warped, H0) pyutils.toc('FC') imvis.imshow(result, 'Result FC', wait_ms=10) pyutils.tic('IC') align = Alignment(target_template, Method.IC, full_reference_image=img, num_pyramid_levels=3, verbose=verbose) align.set_true_warp(H_gt) H_est, result = align.align(warped, H0) pyutils.toc('IC') imvis.imshow(result, 'Result IC', wait_ms=10) pyutils.tic('ESM') align = Alignment(target_template, Method.ESM, full_reference_image=img, num_pyramid_levels=5, verbose=verbose) align.set_true_warp(H_gt) H_est, result = align.align(warped, H0) pyutils.toc('ESM') imvis.imshow(result, 'Result ESM', wait_ms=-1)
def test_gaussian_blur(): assert gaussian_blur(None) is None exdir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'examples') img = imread(os.path.join(exdir, 'flamingo.jpg')) res1 = gaussian_blur(img, 3) res2 = gaussian_blur(img, 15) assert res1.shape == img.shape assert res2.shape == img.shape # We use PIL's ImageFilter module, so just resort to a simple sanity # check, i.e. whether blurring the image with a larger kernel degrades # its visual quality more than using a smaller kernel. diff1 = np.sqrt(np.sum((img - res1)**2)) diff2 = np.sqrt(np.sum((img - res2)**2)) assert diff1 > 0 assert diff2 > diff1
def load_images(): return [imutils.imread('calib/test{:d}.jpg'.format(i)) for i in range(7)]
def test_imsave(tmp_path): # This test will only work on Unix-based test environments because we # use the 'file' command to ensure the stored file is correct. exdir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'examples') out_fn = str(tmp_path / 'test.png') ########################################################################## # Test RGB img_in = imread(os.path.join(exdir, 'flamingo.jpg')) assert img_in.ndim == 3 and img_in.shape[2] == 3 assert img_in.dtype == np.uint8 # Save (lossless) and reload imsave(out_fn, img_in) _, finfo = safe_shell_output('file', out_fn) assert finfo.split(':')[1].strip( ) == 'PNG image data, 400 x 400, 8-bit/color RGB, non-interlaced' img_out = imread(out_fn) assert img_out.ndim == 3 and img_out.shape[2] == 3 assert img_out.dtype == np.uint8 assert np.all(img_in[:] == img_out[:]) ########################################################################## # Test incorrect parameter order with pytest.raises(TypeError): imsave(img_in, out_fn) # Test storing non-image data with pytest.raises(TypeError): imsave(out_fn, 'foo') # Ensure that the output path is created novel_folder = tmp_path / 'foo' / 'bar' assert not novel_folder.exists() novel_img_path = novel_folder / 'test.jpg' imsave(novel_img_path, img_in) assert novel_folder.exists() assert novel_img_path.exists() ########################################################################## # Test RGB with flipping channels img_in = imread(os.path.join(exdir, 'flamingo.jpg')) assert img_in.ndim == 3 and img_in.shape[2] == 3 assert img_in.dtype == np.uint8 # Save (lossless) and reload imsave(out_fn, img_in, flip_channels=True) _, finfo = safe_shell_output('file', out_fn) assert finfo.split(':')[1].strip( ) == 'PNG image data, 400 x 400, 8-bit/color RGB, non-interlaced' img_out = imread(out_fn) assert img_out.ndim == 3 and img_out.shape[2] == 3 assert img_out.dtype == np.uint8 for c in range(3): assert np.all(img_in[:, :, c] == img_out[:, :, 2 - c]) ########################################################################## # Test monochrome 8 bit img_in = imread(os.path.join(exdir, 'peaks.png')) assert img_in.ndim == 2 or img_in.shape[2] == 1 assert img_in.dtype == np.uint8 # Save (lossless) and reload imsave(out_fn, img_in) _, finfo = safe_shell_output('file', out_fn) assert finfo.split(':')[1].strip( ) == 'PNG image data, 256 x 256, 8-bit grayscale, non-interlaced' img_out = imread(out_fn) assert img_out.ndim == 2 or img_out.shape[2] == 1 assert img_out.dtype == np.uint8 assert np.all(img_in[:] == img_out[:]) ########################################################################## # Test monochrome 16 bit (will be loaded as np.int32, using PIL's 'I' mode) img_in = imread(os.path.join(exdir, 'depth.png')) assert img_in.ndim == 2 or img_in.shape[2] == 1 assert img_in.dtype == np.int32 # Explicitly cast to uint16 img_in = img_in.astype(np.uint16) # Save (lossless) and reload imsave(out_fn, img_in) _, finfo = safe_shell_output('file', out_fn) assert finfo.split(':')[1].strip( ) == 'PNG image data, 800 x 600, 16-bit grayscale, non-interlaced' img_out = imread(out_fn) assert img_out.ndim == 2 or img_out.shape[2] == 1 # Loading, however, will still produce a int32 image. assert img_out.dtype == np.int32 assert np.all(img_in[:] == img_out[:]) ########################################################################## # Test 1-bit PNG (requires specifying the mode!) img_in = imread(os.path.join(exdir, 'space-invader.png'), mode='L').astype(bool) assert img_in.ndim == 2 or img_in.shape[2] == 1 assert img_in.dtype == bool imsave(out_fn, img_in) _, finfo = safe_shell_output('file', out_fn) assert (finfo.split(':')[1].strip() == 'PNG image data, 200 x 149, 1-bit colormap, non-interlaced' or finfo.split(':')[1].strip() == 'PNG image data, 200 x 149, 1-bit grayscale, non-interlaced') img_out = imread(out_fn, mode='L').astype(bool) assert img_out.ndim == 2 or img_out.shape[2] == 1 assert np.all(img_in[:] == img_out[:])
def test_imread(): exdir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'examples') assert imread(None) is None for fn in ['', 'a-non-existing.file']: with pytest.raises(FileNotFoundError): imread(fn) # Forget the keyword to be passed on to PIL with pytest.raises(TypeError): imread(os.path.join(exdir, 'flamingo.jpg'), 'RGB') # Load RGB JPG img = imread(os.path.join(exdir, 'flamingo.jpg')) assert img.ndim == 3 and img.shape[2] == 3 assert img.dtype == np.uint8 # Same image, but BGR img_flipped = imread(os.path.join(exdir, 'flamingo.jpg'), flip_channels=True) for c in range(img.shape[2]): complement = 2 - c if c < 3 else 3 assert np.all(img[:, :, c] == img_flipped[:, :, complement]) # Same image, but monochrome img = imread(os.path.join(exdir, 'flamingo.jpg'), mode='L') assert img.ndim == 2 or img.shape[2] == 1 assert img.dtype == np.uint8 # Load 8-bit single-channel PNG img = imread(os.path.join(exdir, 'peaks.png')) assert img.ndim == 2 or img.shape[2] == 1 assert img.dtype == np.uint8 # ... now enforce loading it as RGB/BGR img = imread(os.path.join(exdir, 'peaks.png'), mode='RGB') assert img.ndim == 3 and img.shape[2] == 3 assert img.dtype == np.uint8 # ... all channels should contain the same information for c in [1, 2]: assert np.all(img[:, :, c] == img[:, :, 0]) # Load 16-bit PNG img = imread(os.path.join(exdir, 'depth.png')) assert img.ndim == 2 or img.shape[2] == 1 assert img.dtype == np.int32 # Load 16-bit PNG, specifying mode manually: img = imread(os.path.join(exdir, 'depth.png'), mode='I') assert img.ndim == 2 or img.shape[2] == 1 assert img.dtype == np.int32 # Load 1-bit PNG (requires specifying the mode!) img = imread(os.path.join(exdir, 'space-invader.png'), mode='L') assert img.ndim == 2 or img.shape[2] == 1 assert img.dtype == np.uint8
This example script needs PIL (Pillow package) to load images from disk. """ import os import sys # Extend the python path sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..')) from vito import colormaps from vito import flowutils from vito import imutils from vito import imvis if __name__ == "__main__": # Standard loading/display rgb = imutils.imread('flamingo.jpg', mode='RGB') imvis.imshow(rgb) # Load as BGR bgr = imutils.imread('flamingo.jpg', mode='RGB', flip_channels=True) imvis.imshow(bgr) # Load a single-channel image peaks = imutils.imread('peaks.png', mode='L') # Colorize it colorized = imvis.pseudocolor(peaks, limits=None, color_map=colormaps.colormap_viridis_rgb) imvis.imshow(colorized) # Load optical flow and visualize it