How to extract RGB DNs from an image using R?

659 Views Asked by At

enter image description here

This is a sample image from a fish eye lens camera. I want to extract the digital number from this image for my further research.

How to extract RGB DNs from an image using R?

1

There are 1 best solutions below

3
On

The optimal approach will most probably differ depending on what your overall aim is. To get you started, you could try the functionality provided by the package EBImage, a general-purpose image processing and analysis toolbox with applications in bioimage analysis. It provides functions to load images into R, and perform various image transformations and filtering. The example below illustrates how to load your sample image and draw a histogram. See the package vignette for details on image data representation, and an overview of the provided functionality.

# package installation form Biocondcutor
# need to run this only once
source("https://bioconductor.org/biocLite.R")
biocLite("EBImage")

library("EBImage")
img <- readImage("https://i.stack.imgur.com/JTSWO.jpg")

## Image 
##   colorMode    : Color 
##   storage.mode : double 
##   dim          : 1500 1001 3 
##   frames.total : 3 
##   frames.render: 1 
## 
## imageData(object)[1:5,1:6,1]
##            [,1]       [,2]       [,3]       [,4]       [,5]       [,6]
## [1,] 0.05098039 0.09019608 0.04705882 0.00000000 0.02352941 0.02745098
## [2,] 0.06274510 0.09411765 0.05490196 0.01176471 0.02745098 0.02745098
## [3,] 0.06274510 0.08235294 0.05098039 0.01960784 0.02745098 0.02352941
## [4,] 0.04705882 0.05098039 0.03529412 0.01960784 0.01960784 0.01960784
## [5,] 0.02352941 0.01176471 0.01176471 0.01960784 0.01176471 0.01176471

hist(img)

enter image description here

For example, you can easily calculate the mean pixel intensity value for each color channel by applying the function over the 3rd image dimension.

apply(img, 3, mean)

## [1] 0.2216768 0.2592197 0.1629841

These values correspond to the average pixel values in the red, green and blue color channels, respectively. Pixel values are usually normalized to the [0, 1] range, so these numbers represent the fractions of the respective colors in your image. Note that if you are interested in reliably comparing images taken at the same spot but at different time points you should make sure that they were taken with the same fixed value of white balance.