def load(self, data): """Loads data into a Hologram object. The data can either be a numpy array already in memory or a filename to read from disk using opencv. If the data is a numpy array, it is assumed to be floating point and single channel. If the data is a string, it is treated as a file on disk. The file is read into an array, converted to grayscale if it is 3-channel, and divided by 255 to convert to floating point.""" if isinstance(data, basestring): img = cv2utils.imread(data) if imageutils.nchannels(img) == 3: print "Converting 3-channel image to grayscale..." img = cv2color.bgr2gray(img) self.data = img / 255. #converts to float, [0..1] range elif isinstance(data, numpy.ndarray): self.data = data else: raise TypeError("Must be a numpy array or a filename.")
def test_image(image_name=None): """Loads a test image with the closest match to the supplied name.""" test_image_dir = image_dir() available_images = dir_by_ext(image_dir(), IMAGE_EXT) if image_name is None: print 'Available images are: %s' % available_images return None #find the matching image names assert(isinstance(image_name, basestring)) matches = [file_name for file_name in available_images if file_name.startswith(image_name)] if len(matches) == 0: print 'No name match found for %s.' % image_name return None elif len(matches) > 1: print 'Multiple matches found for %s: %s.' % (image_name, matches) return None else: #load the matching image filename = os.path.join(test_image_dir, matches[0]) print 'loading: %s' % filename return cv2utils.imread(filename)