Learn R Programming

recolorize v0.2.0

Color-based image segmentation (for people with other things to do).

Comparison of color maps produced using k-means clustering (center) vs. recolorize (right). Original image credits: John E. Randall, Nathan P. Lord, and Alison Davis-Rabosky.

  • Update, Feb. 2024: Please cite the article if you’re using the package: > Weller, H. I., Hiller, A. E., Lord, N. P., & Van Belleghem, S. M. (2024). recolorize: An R package for flexible colour segmentation of biological images. Ecology Letters, 27(2), e14378.) (Also, please read the article.)

What is this?

This is a package for making color maps, which are needed (or at least useful) for a wide range of color analysis techniques. It was born out of conversations with many biologists who found, to their surprise and mine, that generating color maps were the bottleneck step in their analyses. Fully automated methods rarely work all of the time, and are difficult to modify, while fully manual methods are subjective and time-consuming. This package tries to split the difference by giving you a mix of tools that will do a pretty good job with no user input, and then allow minor manual changes like merging and filtering layers or splitting components, before exporting them to the next step of your analysis (e.g. pavo, patternize). It’s also, for the most part, totally deterministic – no arbitrary seed-setting for repeatability.

Quick start

Install the package:

# development version:
install.packages("devtools")
devtools::install_github("hiweller/recolorize")

# OR
install.packages("recolorize") # CRAN release

To run recolorize on a single image:

library(recolorize)

# load an image that comes with the package:
img <- system.file("extdata/corbetti.png", package = "recolorize")
rc <- recolorize2(img, cutoff = 45)

A batch processing example:

# get list of all PNGs that come with the package:
images <- dir(system.file("extdata", package = "recolorize"),
             pattern = ".png", full.names = TRUE)

# for every image...
for (i in 1:length(images)) {

  # get an initial fit with generic clustering
  init_fit <- recolorize2(images[i], method = "hist", bins = 2, cutoff = 25, plotting = FALSE)

  # drop small patches
  refined_fit <- thresholdRecolor(init_fit, pct = 0.01, plotting = FALSE)

  # store in an output variable
  if (i == 1) {
    colormap_list <- list(refined_fit)
  } else {
    colormap_list[[i]] <- refined_fit
  }
}

# compare original to recolored images:
layout(matrix(1:10, nrow = 2, byrow = TRUE))
par(mar = rep(0, 4))
o <- lapply(colormap_list, function(i) plot(i$original_img))
r <- lapply(colormap_list, function(i) plotImageArray(recoloredImage(i)))

See package vignettes for detailed documentation.

How does it work?

Recolorize is a toolbox of automatic, semi-automatic, and manual methods that aims to give you reproducible results that work pretty well out-of-box, and which are easy to tailor to your needs. The general workflow is an initial clustering step -> an automatic or semi-automatic reclustering step -> optional manual refinements -> export to another format, like an image, a set of binary masks, or another R package.

Contact

Please do email me if this is something that may be relevant for you or if you have questions. Most of the features in the package are the result of feedback, and no method should be developed in a vacuum. If you include example images in your email, my likelihood of responding goes through the roof.

Email: hannahiweller@gmail.com

Copy Link

Version

Install

install.packages('recolorize')

Monthly Downloads

605

Version

0.2.0

License

CC BY 4.0

Issues

Pull Requests

Stars

Forks

Maintainer

Hannah Weller

Last Published

March 3rd, 2025

Functions in recolorize (0.2.0)

colorClusters

Generate color clusters from an image
adjust_color

Adjust the saturation and brightness of a color
apply_imager_operation

Apply imager operations to layers of an image
brick_to_array

Convert from a RasterBrick to an array
match_colors

Reorder a color palette to best match a reference palette
labelCol

Change colors of dendrogram tips
clean_merge_params

Clean up parameters passed to mergeLayers
col2col

Modified convertColor
hclust_color

Plot and group colors by similarity
absorbLayer

Absorb a layer into its surrounding color patches
cielab_coldist

Generate a 'coldist' object for CIE Lab colors
imDist

Calculates the distance between non-transparent pixels in images
plotColorPalette

Plot a color palette
plotImageArray

Plot a 3D array as an RGB image
add_image

Add a raster image to a plot
cimg_to_array

Converts from cimg to raster array
constructImage

Generate an image from pixel assignments and color matrix
classify_recolorize

Convert a recolorize object to a classify object
colorClustersHist

Cluster pixel colors using histogram binning
editLayers

Edit multiple color patches using morphological operations
mergeLayers

Merge layers in a recolorized image
medianColors

Change color centers to median color of all pixels assigned to it
raster_to_array

Convert from a (small-r) raster object to an RGB array
editLayer

Edit a color patch using morphological operations
readImage

Read in an image as a 3D array
imHeatmap

Plot a heatmap of a matrix of color distances
imposeColors

Recolor an image to a provided set of colors
recolorize_to_png

Save a recolored image as a PNG
recolorize-package

recolorize: Color-Based Image Segmentation
recolorize

Simplify the colors of an image
plot.recolorizeVector

Plot a recolorizeVector object
plotColorClusters

Plot color clusters in a color space
recolorize_adjacency

Run pavo's adjacency and boundary strength analysis on a recolorize object
expand_recolorize

Expand aspects of a recolorize object for other functions
plot.recolorize

Plot recolorized image results
pixelAssignMatrix

Make pixel assignment matrix for recoloring
recolorizeVector

Convert a recolorize object to a vector
recolorize2

Recolorize with automatic thresholding
rerun_recolorize

Rerun the sequence of calls used to produce a recolorize object
rgb2hsl

Convert RGB colors to HSL
werner

Werner's nomenclature of colors
recolorize_to_patternize

Convert a recolorize object to a raster object
wernerColor

Remap an image to Werner's nomenclature
reorder_colors

Reorder colors in a recolorize object
recoloredImage

Get recolored image from a recolorize object
recluster

Recluster color centers based on color similarity
thresholdRecolor

Drop minor colors from a recolorize object
splitByColor

Split color clusters in a recolorize object into layers
blurImage

Blur an image
backgroundCondition

Generate a background condition for masking
array_to_RasterStack

Convert from an array to a raster stack
assignPixels

Assign a 2D matrix of pixels to specified colors
array_to_cimg

Converts from a raster array to a cimg object
backgroundIndex

Index and remove background pixels for color clustering
colorClustersKMeans

Cluster pixel colors using K-means clustering
colorResiduals

Calculate squared residuals for color centers