diff --git a/README.md b/README.md index 00f714b..883b732 100644 --- a/README.md +++ b/README.md @@ -4,4 +4,4 @@ ## Practicals -1. [Color Transfer via Discrete Optimal Transport using the sliced approach](https://codimd.math.cnrs.fr/s/2eRBqV9zl) +1. [Color Transfer via Discrete Optimal Transport using the sliced approach](https://codimd.math.cnrs.fr/s/s_rh7X9wF), [Material](https://github.com/dcoeurjo/CGDI-Practicals/tree/main/1-SlicedOptimalTransport) diff --git a/misc/README.md b/misc/README.md new file mode 100644 index 0000000..963b3bb --- /dev/null +++ b/misc/README.md @@ -0,0 +1,3 @@ +# Extra tools + +* `computeHistogram.py`: compute and the three RGB histograms from an image. diff --git a/misc/computeHistogram.py b/misc/computeHistogram.py new file mode 100755 index 0000000..42b0549 --- /dev/null +++ b/misc/computeHistogram.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 + # import the necessary packages +from matplotlib import pyplot as plt +import numpy as np +import argparse +import cv2 + +# construct the argument parser and parse the arguments +ap = argparse.ArgumentParser() +ap.add_argument("-i", "--image", required = True, help = "Path to the image") +ap.add_argument("-o", "--outputhist", required = True, help = "Path to the histogram image") +args = vars(ap.parse_args()) + +# load the image and show it +image = cv2.imread(args["image"]) +cv2.imshow("image", image) + + +# grab the image channels, initialize the tuple of colors, +# the figure and the flattened feature vector +chans = cv2.split(image) +colors = ("b", "g", "r") +plt.figure() +plt.title("") +plt.xlabel("Bins") +plt.ylabel("# of Pixels") +features = [] + +# loop over the image channels +for (chan, color) in zip(chans, colors): + # create a histogram for the current channel and + # concatenate the resulting histograms for each + # channel + hist = cv2.calcHist([chan], [0], None, [256], [0, 256]) + features.extend(hist) + + # plot the histogram + plt.plot(hist, color = color) + plt.xlim([0, 256]) + + +plt.savefig(args["outputhist"]) +# here we are simply showing the dimensionality of the +# flattened color histogram 256 bins for each channel +# x 3 channels = 768 total values -- in practice, we would +# normally not use 256 bins for each channel, a choice +# between 32-96 bins are normally used, but this tends +# to be application dependent +print("flattened feature vector size: %d" % (np.array(features).flatten().shape))