2

I have grayscale images which I want to quantize to different gray levels.

To be more precise, in the EBImage package, we have a function equalize() which has an argument levels. we can set levels value to 256 or 128 or 64 etc to quantize our grayscale images. (But the equalize() function will perform a histogram equalization of the given grayscale image, which is not preferred for my current situation)

Can somebody suggest a formula or a function which we can use to change the number of gray levels in the given grayscale image.

zx8754
  • 52,746
  • 12
  • 114
  • 209
lijin
  • 36
  • 4

2 Answers2

1

First convert the format in something continous. Now pseudocode.

int x = (int) (value / (Quantisation)); (new format) y = x * Quantisation;

It is also possible to compress images that lousy way.

special_A
  • 19
  • 3
1

The default image data representation in EBImage is a continuous range between 0 and 1. In order to quantize an image to a given number of levels, first convert it to integers in the range of 0:(levels-1), and then back to 0:1, as in the quantize function from the following example.

library(EBImage)

## sample grayscale image
x = readImage(system.file("images", "sample.png", package="EBImage"))

## function for performing image quantization
quantize = function(img, levels) round(img * (levels-1)) / (levels-1)

## quantize the image
y = quantize(x, levels = 8) 

## show the result
display(y)

enter image description here

aoles
  • 1,525
  • 10
  • 17