Learn R Programming

patternize - An R package for quantifying color pattern variation

Quantifying variation in color patterns to study and compare the consistency of their expression necessitates the homologous alignment and color-based segmentation of images. Patternize is an R package that quantifies variation in color patterns as obtained from image data. Patternize defines homology between pattern positions across specimens either through fixed landmarks or image registration. Pattern identification is performed by categorizing the distribution of colors using either an RGB threshold or an unsupervised image segmentation. The quantification of the color patterns can be visualized as heat maps and compared between sets of samples.

Please do not hesitate to contact me with any questions or suggestions!

Install patternize in R from CRAN (not up to date at this time!!):

install.packages("patternize")

Install patternize in R from GitHub using devtools:

install.packages("devtools")
library(devtools)
install_github("StevenVB12/patternize")
# Morpho has committed a change that affects computeTransform used in
# the patternize functions patLanRGB and patLanK (not available in CRAN)
install_github("zarquon42b/Morpho")

Installation errors

Some people have noted platform specific installation errors (mostly Mac). If you don't find your solution here, please contact me.

error: unable to load shared object 
'/Library/Frameworks/R.framework/Versions/3.4/Resources/library/rgl/libs/rgl.so'

Solution: download XQuartz https://www.xquartz.org 

For examples see package examples or https://github.com/StevenVB12/patternize-examples

Workflow

Setting landmarks

I set landmarks using the Fiji distribution of ImageJ (https://fiji.sc/):

Fiji
> File > open (image)
> Multi-point (make sure points are set in the same order for each sample)
> Save As > XY coordinates

But you can also use (setting a resampleFactor > 0 speeds up things, but reduces resolution):

landmarkList <- sampleLandmarks(sampleList, resampleFactor = NULL, crop = c(0,0,0,0))

If you are working with Heliconius or related butterflies, consider using this landmark scheme:

Setting RGB value

You can assign an RGB vector manually (e.g. RGB <- c(255,0,0) for red) or use:

RGB <- sampleRGB(image, resampleFactor = NULL, crop = c(0,0,0,0))

Making cartoon for plotting (or masking)

To plot a cartoon of the organism or trait of interest I use XY coordinates of an outline or lines obtained in the Fiji distribution of ImageJ (https://fiji.sc/). This is an annoying manual task, but if done precisely it can provide an outline or cartoon for any type of shape. The cartoon should be drawn for the reference (target) image when using image registration (patRegRGB or patRegK) or when using landmark transformation to a target image (patLanRGB or patLanK; transformRef = 'sample_ID'). When using transformRef = 'meanshape', the cartoon will also be transformed to the mean shape.

outline

Fiji
> File > open (image)
> Polygon selections (draw outline)
> Save As > XY coordinates

lines (same as setting landmarks)

Fiji
> File > open (image)
> Multi-point (draw lines by setting points)
> Save As > XY coordinates

Calibrate images using ColorChecker

The ColorChecker (Macbeth ColorChecker) is a color calibration target consisting of a framed arrangement of 24 squares of painted samples. The chart patches are selected to mimic those of natural objects and to have consistent color appearance under a variety of lighting conditions. By including this chart in an image, it can be used to calibrate images taken in different lightning conditions.

You can calibrate images like this:

IDlist <- c('image1', 'image2')
extension <- '.jpg'
colorChecker(IDlist, extension)

The function is also available as a stand alone app: https://stevenvanbelleghem.shinyapps.io/shiny_colorchecker/ (see https://github.com/StevenVB12/shiny_ColorChecker)

This calculates a second order polynomial regression between the observed and expected RGB values of the 24 ColorChecker patches and performs the calibration of the image. The function will ask you to define the corners of the ColorChecker like this (also works for images taken with a skew angle):

Copy Link

Version

Install

install.packages('patternize')

Monthly Downloads

254

Version

0.0.5

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Steven Van Belleghem

Last Published

August 22nd, 2023

Functions in patternize (0.0.5)

colorChecker_customGray

Calibrate images using ColorChecker.
colorChecker_half

Calibrate images using (right) half of ColorChecker. Only works for X-Rite.
imageList

imageList
extdata

External patternize data
patGMM

Extract colors using GMM clustering (for pre-aligned images).
alignReg

Aligns images using niftyreg utilities for automated image registration..
patK_HSV

Extract colors using k-means clustering (for pre-aligned images).
maskOutline

Intersects a RasterStack with an outline. Everything outside of the outline will be removed from the raster.
patLanK_HSV

Aligns images usings transformations obtained from fixed landmarks and extracts colors using k-means clustering.
colorChecker

Calibrate images using ColorChecker.
patLanHSV

Aligns images usings transformations obtained from fixed landmarks and extracts colors using a predefined RGB values and cutoff value.
patArea

This fucntion calculates the area in which the color pattern is expressed in each sample as the relative proportion using the provided outline of the considered trait or structure.
kImageHSV

kmeans clustering of image imported as a RasterStack. This function is used by patLanK and patRegK.
kImage

kmeans clustering of image imported as a RasterStack. This function is used by patLanK and patRegK.
patLanW

Extracts color pattern from landmark transformed image using watershed segmentation. This function works interactively by allowing to pick a starting pixel within each pattern element from which the watershed will extract the pattern. This function works best for patterns with sharp boundaries.
patPCA

This function transforms the individual color pattern rasters as obtained by the main patternize functions to a dataframe of 0 and 1 values that can be used for Principal Component Analysis (prcomp). This function also allows to plot the analysis including a visualization of the shape changes along the axis. Pixel values are predicted by multiplying the rotation matrix (eigenvectors) with a vector that has the same length as the number of rows in the rotation matrix and in which all values are set to zero except for the PC value for which we want to predict the pixel values.
patRDA

This function transforms the individual color pattern rasters as obtained by the main patternize functions to a dataframe of 0 and 1 values that can be used for constrained Redundancy Analysis (RDA) (rda). This function also allows to plot the analysis including a visualization of the shape changes along the axis.
GMMImage

GMM clustering of image imported as a RasterStack.
alignLan

Align images using landmarks
lanArray

Build landmark array for Morpho.
patK

Extract colors using k-means clustering (for pre-aligned images).
landmarkArray

landmarkArray
patLanK

Aligns images usings transformations obtained from fixed landmarks and extracts colors using k-means clustering.
makeList

Build list of landmarks or RasterStacks from images using filepath and file extension.
landmarkList

landmarkList
plotHeat

Plots the color pattern heatmaps from sumRaster output.
patRegRGB

Aligns images using niftyreg utilities for automated image registration and extracts colors using a predefined RGB values and cutoff value.
plotRasterstackAsImage

Plot rasterStack as image.
patLanRGB

Aligns images usings transformations obtained from fixed landmarks and extracts colors using a predefined RGB values and cutoff value.
patRegW

Aligns images using niftyreg utilities for automated image registration and extracts color pattern using watershed segmentation. This function works interactively by allowing to pick a starting pixel within each pattern element from which the watershed will extract the pattern. This function works best for patterns with sharp boundaries.
redRes

Reduce the resolution of an image imported as a RasterStack by downsampling.
patRegHSV

Aligns images using niftyreg utilities for automated image registration and extracts colors using a predefined HSV values and cutoff value.
sumRaster

This function sums the individual color pattern RasterLayes as obtained by the main patternize functions.
rasterList_regK

rasterList_regK
patRegK_HSV

Aligns images using niftyreg utilities for automated image registration and extracts colors using k-means clustering.
patRegK

Aligns images using niftyreg utilities for automated image registration and extracts colors using k-means clustering.
patternize

patternize - An R package for quantifying color pattern variation.
rasterList_regRGB

rasterList_regRGB
sampleLandmarks

Sample landmarks in an image.
setMask

Interactive function to to draw an outline for masking.
rasterList_lanK

rasterList_lanK
rasterList_lanRGB

rasterList_lanRGB
sampleRGB

Interactive function to sample RGB value from pixel or square area in an image.
createPhenotype

Plot color pattern prediction for specified PCA values
createTarget

Create a target image (RasterStack) from a polygon.