Image.Scholar (image v0.73.0)

Copy Markdown View Source

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

k_means(image, options \\ [])

Clusters the unique colors of an image using the K-means algorithm.

Arguments

Returns

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}

unique_color_count(image_or_tensor)

@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_tensor is any 3- or 4-band Vix.Vips.Image.t/0 with {:u, 8} band format, or the {height, width, bands} tensor of such an image as returned by Image.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}

unique_colors(image)

Returns the unique colors in an image and the count of each color.

Arguments

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}}