dominantcolor

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2023 License: MIT Imports: 7 Imported by: 16

README

Dominantcolor

GoDoc

Find dominant color in images

import "github.com/cenkalti/dominantcolor"

Package dominantcolor provides a function for finding a color that represents the calculated dominant color in the image. This uses a KMean clustering algorithm to find clusters of pixel colors in RGB space.

The algorithm is ported from Chromium source code:

See more at: http://godoc.org/github.com/cenkalti/dominantcolor

Example
package main

import (
	"fmt"
	"github.com/cenkalti/dominantcolor"
	"image"
	_ "image/jpeg"
	_ "image/png"
	"os"
)

func FindDomiantColor(fileInput string) (string, error) {
	f, err := os.Open(fileInput)
	defer f.Close()
	if err != nil {
		fmt.Println("File not found:", fileInput)
		return "", err
	}
	img, _, err := image.Decode(f)
	if err != nil {
		return "", err
	}

	return dominantcolor.Hex(dominantcolor.Find(img)), nil
}

func main() {
	fmt.Println(FindDomiantColor("aa.png"))
}

Output:
#CA5527

Thanks to @stuartmscott for creating a GUI: https://github.com/stuartmscott/dominantcolor

Documentation

Overview

Package dominantcolor provides a function for finding a color that represents the calculated dominant color in the image. This uses a KMean clustering algorithm to find clusters of pixel colors in RGB space.

The algorithm is ported from Chromium source code:

https://src.chromium.org/svn/trunk/src/ui/gfx/color_analysis.h
https://src.chromium.org/svn/trunk/src/ui/gfx/color_analysis.cc

RGB KMean Algorithm (N clusters, M iterations):

1. Pick N starting colors by randomly sampling the pixels. If you see a color you already saw keep sampling. After a certain number of tries just remove the cluster and continue with N = N-1 clusters (for an image with just one color this should devolve to N=1). These colors are the centers of your N clusters.

2. For each pixel in the image find the cluster that it is closest to in RGB space. Add that pixel's color to that cluster (we keep a sum and a count of all of the pixels added to the space, so just add it to the sum and increment count).

3. Calculate the new cluster centroids by getting the average color of all of the pixels in each cluster (dividing the sum by the count).

4. See if the new centroids are the same as the old centroids.

a) If this is the case for all N clusters than we have converged and can move on.

b) If any centroid moved, repeat step 2 with the new centroids for up to M iterations.

5. Once the clusters have converged or M iterations have been tried, sort the clusters by weight (where weight is the number of pixels that make up this cluster).

6. Going through the sorted list of clusters, pick the first cluster with the largest weight that's centroid falls between |lower_bound| and |upper_bound|. Return that color. If no color fulfills that requirement return the color with the largest weight regardless of whether or not it fulfills the equation above.

Example
package main

import (
	"fmt"
	"image"
	"os"

	"github.com/cenkalti/dominantcolor"
	_ "image/png"
)

func main() {
	f, _ := os.Open("firefox.png")
	img, _, _ := image.Decode(f)
	f.Close()
	fmt.Println(dominantcolor.Hex(dominantcolor.Find(img)))
}
Output:

#CB5A27

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Find

func Find(img image.Image) color.RGBA

Find returns the dominant color in img.

func FindN

func FindN(img image.Image, nClusters int) []color.RGBA

FindN returns the first-N dominant colors in an image. If nClusters is less than or equal to 0, the value defaults to 4. Clusters are returned in their order of dominance.

func Hex

func Hex(c color.RGBA) string

Hex returns a string representing the color in "#AABBCC" format.

Types

type Color

type Color struct {
	color.RGBA
	Weight float64
}

func FindWeight

func FindWeight(img image.Image, nClusters int) []Color

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL