Functions that analyse images using Scholar machine-learning primitives.
The public API is unique_colors/1, unique_color_count/1 and
k_means/2, which underpin Image.k_means/2 and
Image.reduce_colors/2.
Summary
Functions
Clusters the unique colors of an image using the K-means algorithm.
Returns the number of unique colors in an image.
Returns the unique colors in an image and the count of each color.
Functions
Clusters the unique colors of an image using the K-means algorithm.
Arguments
imageis any 3- or 4-bandVix.Vips.Image.t/0with{:u, 8}band format.optionsis a keyword list of options passed toScholar.Cluster.KMeans.fit/2.
Returns
{:ok, model}wheremodelis a fittedScholar.Cluster.KMeansor{:error, reason}.
Examples
iex> image = Image.open!("./test/support/images/Kip_small.jpg")
iex> {:ok, model} = Image.Scholar.k_means(image, num_clusters: 3, key: Nx.Random.key(42))
iex> Nx.shape(model.clusters)
{3, 3}
@spec unique_color_count(image_or_tensor :: Vix.Vips.Image.t() | Nx.Tensor.t()) :: {:ok, non_neg_integer()} | {:error, Image.Error.t()}
Returns the number of unique colors in an image.
Prefer this over unique_colors/1 when only the count is
needed.
Arguments
image_or_tensoris any 3- or 4-bandVix.Vips.Image.t/0with{:u, 8}band format, or the{height, width, bands}tensor of such an image as returned byImage.to_nx/2. Pass the tensor when one is already to hand, to avoid converting the image twice.
Returns
{:ok, count}or{:error, reason}.
Example
iex> Image.Scholar.unique_color_count(Image.new!(4, 4, color: :red))
{:ok, 1}
Returns the unique colors in an image and the count of each color.
Arguments
imageis any 3- or 4-bandVix.Vips.Image.t/0with{:u, 8}band format.
Returns
{:ok, {color_count_tensor, unique_colors_tensor}}or{:error, reason}.
Examples
iex> image = Image.new!(8, 8, color: [10, 20, 30])
iex> {:ok, {counts, colors}} = Image.Scholar.unique_colors(image)
iex> {Nx.shape(counts), Nx.shape(colors)}
{{1}, {1, 3}}