def make_histogram(args, what_roi, session, metadata): """ Make a histogram of every image and write the values to a file. Expects the result of an argument parser, a string of what kind of roi needs to be used and a connection to an existing metadata database via an SQLAlchemy Session instance 'session' and an SQLAlchemy MetaData instance 'metadata' which describes the database tables. A connection to the database table 'Photos' is made. Every file that is an image is opened and the title of that image is taken from the database. A mask is created to isolate a part of the image (Region Of Interest (ROI)) and a histogram is made of that ROI. The values in the histogram-list are normalized and relevant data is written to the outputfile. """ Base = automap_base(metadata=metadata) Base.prepare() configure_mappers() Photo = Base.classes.photos # Open outputfile. outputfile = open(args.outputfile, 'a') # Walk through files. for root, dirs, files in os.walk(args.imdir): for filename in files: sys.stderr.write("File %s is being processed...\n" % filename) # Make path to file. path = os.path.join(root, filename) # Open file and check datatype. img = cv2.imread(path, 1) if not isinstance(img, np.ndarray): sys.stderr.write("File is no image: will be skipped.\n") continue photo_id = filename.split(".")[0] # Get title of image from database. # Set default in case there is no database entry for it. title = photo_id for pic in session.query(Photo).filter(Photo.id == photo_id): title = photo_id if pic.title is None else pic.title img, contour = create_mask(img, args, what_roi) hist = ft.color_bgr_means(img, contour, bins=args.bins) means_norm = hist_means(hist) write_to_output_file(photo_id, title, means_norm, args, outputfile) # Close outputfile. outputfile.close()
def make_histogram(args, what_roi, session, metadata): """ Make a histogram of every image and write the values to a file. Expects the result of an argument parser, a string of what kind of roi needs to be used and a connection to an existing metadata database via an SQLAlchemy Session instance 'session' and an SQLAlchemy MetaData instance 'metadata' which describes the database tables. A connection to the database table 'Photos' is made. Every file that is an image is opened and the title of that image is taken from the database. A mask is created to isolate a part of the image (Region Of Interest (ROI)) and a histogram is made of that ROI. The values in the histogram-list are normalized and relevant data is written to the outputfile. """ Base = automap_base(metadata=metadata) Base.prepare() configure_mappers() Photo = Base.classes.photos # Open outputfile. outputfile = open(args.outputfile, 'a') # Walk through files. for root, dirs, files in os.walk(args.imdir): for filename in files: sys.stderr.write("File %s is being processed...\n" % filename) # Make path to file. path = os.path.join(root, filename) # Open file and check datatype. img = cv2.imread(path, 1) if not isinstance(img, np.ndarray): sys.stderr.write("File is no image: will be skipped.\n") continue photo_id = filename.split(".")[0] # Get title of image from database. # Set default in case there is no database entry for it. title = photo_id for pic in session.query(Photo).filter(Photo.id == photo_id): title = photo_id if pic.title is None else pic.title img, contour = create_mask(img, args, what_roi) hist = ft.color_bgr_means(img, contour, bins=args.bins) means_norm = hist_means(hist) write_to_output_file(photo_id, title, means_norm, args, outputfile) # Close outputfile. outputfile.close()
def get_color_bgr_means(self, src, args, bin_mask=None): if self.bin_mask == None: raise ValueError("Binary mask cannot be None") # Get the contours from the mask. contour = ft.get_largest_contour(bin_mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) if contour == None: raise ValueError("No contour found for binary image") # Create a masked image. img = cv2.bitwise_and(src, src, mask=bin_mask) bins = getattr(args, 'bins', 20) output = ft.color_bgr_means(img, contour, bins) # Normalize data to range -1 .. 1 return output * 2.0 / 255 - 1
def __get_color_bgr_means(self, src, args, bin_mask=None): """Executes :meth:`features.color_bgr_means`.""" if self.bin_mask is None: raise ValueError("Binary mask cannot be None") # Get the contours from the mask. contour = ft.get_largest_contour(bin_mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) if contour is None: raise ValueError("No contour found for binary image") # Create a masked image. img = cv2.bitwise_and(src, src, mask=bin_mask) bins = getattr(args, 'bins', 20) hor_means, ver_means = ft.color_bgr_means(img, contour, bins) output = np.append(hor_means, ver_means).astype(float) # Normalize the features if a scaler is set. if self.scaler: self.scaler.fit([0.0, 255.0]) output = self.scaler.transform( output ) return output
def __get_color_bgr_means(self, src, args, bin_mask=None): """Executes :meth:`features.color_bgr_means`.""" if self.bin_mask is None: raise ValueError("Binary mask cannot be None") # Get the contours from the mask. contour = ft.get_largest_contour(bin_mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) if contour is None: raise ValueError("No contour found for binary image") # Create a masked image. img = cv2.bitwise_and(src, src, mask=bin_mask) bins = getattr(args, 'bins', 20) hor_means, ver_means = ft.color_bgr_means(img, contour, bins) output = np.append(hor_means, ver_means).astype(float) # Normalize the features if a scaler is set. if self.scaler: self.scaler.fit([0.0, 255.0]) output = self.scaler.transform(output) return output