def __get_color_histograms(self, src, args, bin_mask=None): """Executes :meth:`features.color_histograms`.""" histograms = [] for colorspace, bins in vars(args).iteritems(): if colorspace.lower() == "bgr": colorspace = ft.CS_BGR img = src elif colorspace.lower() == "hsv": colorspace = ft.CS_HSV img = cv2.cvtColor(src, cv2.COLOR_BGR2HSV) elif colorspace.lower() == "luv": colorspace = ft.CS_LUV img = cv2.cvtColor(src, cv2.COLOR_BGR2LUV) else: raise ValueError("Unknown colorspace '%s'" % colorspace) hists = ft.color_histograms(img, bins, bin_mask, colorspace) # Get the color space ranges. Correct for the exclusive upper # boundaries. ranges = np.array(ft.CS_RANGE[colorspace]).astype(float) - [0,1] for i, hist in enumerate(hists): # Normalize the features if a scaler is set. if self.scaler: self.scaler.fit(ranges[i]) hist = self.scaler.transform( hist.astype(float) ) histograms.extend( hist.ravel() ) return histograms
def __get_color_histograms(self, src, args, bin_mask=None): """Executes :meth:`features.color_histograms`.""" histograms = [] for colorspace, bins in vars(args).iteritems(): if colorspace.lower() == "bgr": colorspace = ft.CS_BGR img = src elif colorspace.lower() == "hsv": colorspace = ft.CS_HSV img = cv2.cvtColor(src, cv2.COLOR_BGR2HSV) elif colorspace.lower() == "luv": colorspace = ft.CS_LUV img = cv2.cvtColor(src, cv2.COLOR_BGR2LUV) else: raise ValueError("Unknown colorspace '%s'" % colorspace) hists = ft.color_histograms(img, bins, bin_mask, colorspace) # Get the color space ranges. Correct for the exclusive upper # boundaries. ranges = np.array(ft.CS_RANGE[colorspace]).astype(float) - [0, 1] for i, hist in enumerate(hists): # Normalize the features if a scaler is set. if self.scaler: self.scaler.fit(ranges[i]) hist = self.scaler.transform(hist.astype(float)) histograms.extend(hist.ravel()) return histograms
def main(): print __doc__ parser = argparse.ArgumentParser(description="Get image color statistics") parser.add_argument("image", metavar="FILE", help="Input image") args = parser.parse_args() img = cv2.imread(args.image) if img == None or img.size == 0: sys.stderr.write("Failed to read %s\n" % args.image) return 1 sys.stderr.write("Processing %s...\n" % args.image) # Scale the image down if its perimeter exceeds the maximum. img = common.scale_max_perimeter(img, 1000) cs_str = "BGR" hists = ft.color_histograms(img) for i, hist in enumerate(hists): print "%s: %s" % (cs_str[i], hist.astype(int).ravel()) print "BGR ranges:", get_min_max(img) img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) cs_str = "HSV" hists = ft.color_histograms(img_hsv) for i, hist in enumerate(hists): print "%s: %s" % (cs_str[i], hist.astype(int).ravel()) print "HSV ranges:", get_min_max(img_hsv) img_luv = cv2.cvtColor(img, cv2.COLOR_BGR2LUV) cs_str = "Luv" hists = ft.color_histograms(img_luv) for i, hist in enumerate(hists): print "%s: %s" % (cs_str[i], hist.astype(int).ravel()) print "LUV ranges:", get_min_max(img_luv) return 0
def main(): print __doc__ parser = argparse.ArgumentParser(description='Get image color statistics') parser.add_argument('image', metavar='FILE', help='Input image') args = parser.parse_args() img = cv2.imread(args.image) if img == None or img.size == 0: sys.stderr.write("Failed to read %s\n" % args.image) return 1 sys.stderr.write("Processing %s...\n" % args.image) # Scale the image down if its perimeter exceeds the maximum. img = common.scale_max_perimeter(img, 1000) cs_str = "BGR" hists = ft.color_histograms(img) for i, hist in enumerate(hists): print "%s: %s" % (cs_str[i], hist.astype(int).ravel()) print "BGR ranges:", get_min_max(img) img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) cs_str = "HSV" hists = ft.color_histograms(img_hsv) for i, hist in enumerate(hists): print "%s: %s" % (cs_str[i], hist.astype(int).ravel()) print "HSV ranges:", get_min_max(img_hsv) img_luv = cv2.cvtColor(img, cv2.COLOR_BGR2LUV) cs_str = "Luv" hists = ft.color_histograms(img_luv) for i, hist in enumerate(hists): print "%s: %s" % (cs_str[i], hist.astype(int).ravel()) print "LUV ranges:", get_min_max(img_luv) return 0
def get_color_histograms(self, src, args, bin_mask=None): histograms = [] for colorspace, bins in vars(args).iteritems(): if colorspace.lower() == "bgr": colorspace = ft.CS_BGR img = src elif colorspace.lower() == "hsv": colorspace = ft.CS_HSV img = cv2.cvtColor(src, cv2.COLOR_BGR2HSV) elif colorspace.lower() == "luv": colorspace = ft.CS_LUV img = cv2.cvtColor(src, cv2.COLOR_BGR2LUV) else: raise ValueError("Unknown colorspace '%s'" % colorspace) hists = ft.color_histograms(img, bins, bin_mask, colorspace) for hist in hists: hist = cv2.normalize(hist, None, -1, 1, cv2.NORM_MINMAX) histograms.extend( hist.ravel() ) return histograms