3
$\begingroup$

I would like to color code the brightness values of an image (8bit, gray scale, 1600*1200 pixels). Each pixel should be color coded according to its brightness value using a dedicated color table.

An example image can be downloaded here.

The brightness histogram is:

Histogram[Flatten@ImageData@image, {1/256}, ScalingFunctions -> "Log",
  Frame -> True, FrameLabel -> {{"# of Pixels", ""}, {"Brightness", ""}}, 
  BaseStyle -> {FontWeight -> "Bold", FontSize -> 40, 
  FontFamily -> "Calibri"}, PlotRange -> {{0, 1}, All}, 
  ImageSize -> 2000, ImagePadding -> {{All, All}, {All, 50}}]

So most of the brightness values are around 0.045.

enter image description here

I tested ListContourPlot but it is extremely slow (AbsoluteTiming gives 91 sec).

colTable = {{Black}, 
   Table[{Blend[{Blue, Green, Yellow, Red}, x]}, {x, 0.2, 1, 0.1}]}; 
colTable = Flatten[colTable]

ListContourPlot[ImageData@image, PlotLegends -> Automatic, 
 ColorFunction -> (Blend[colTable, #] &), Contours -> 10]

How can I improve the contrast of the color coded image?

Do you know an other or faster solution?

enter image description here

$\endgroup$
  • $\begingroup$ The code you used to make the ListContourPlot? $\endgroup$ – Jason B. Jul 13 '16 at 13:43
  • $\begingroup$ Have you looked at Colorize[]? $\endgroup$ – J. M. will be back soon Jul 13 '16 at 13:50
  • $\begingroup$ Is the image in the post the same as the link? $\endgroup$ – Young Jul 13 '16 at 14:00
  • $\begingroup$ @Young: The image in the link corresponds to the image in the code. $\endgroup$ – mrz Jul 13 '16 at 14:05
  • 1
    $\begingroup$ @mrz - I don't get the same result when I run your code: i.stack.imgur.com/12N5E.png $\endgroup$ – Jason B. Jul 13 '16 at 14:26
1
$\begingroup$

Update

New and fast (4 seconds) way to achieve plot and scale.

colTable = {{Black}, Table[{Blend[{Blue, Green, Yellow, Red}, x]}, {x, 0.2, 1, 0.1}]};
colTable = Flatten[colTable];

ArrayPlot[ImageData[imageOrg], ColorFunction -> (Blend[colTable, #] &),
          PlotLegends -> Automatic, FrameTicks -> All]

enter image description here


Other methods:

Colorize

Colorize[imageOrg, ColorFunction -> (Blend[colTable, #] &),
         ColorFunctionScaling -> False] //Timing

2.15281

enter image description here

How do I apply a ColorDataFunction[] to a grayscale image?


Image@Raster[ImageData[imageOrg, DataReversed -> True],
             ColorFunction -> (Blend[colTable, #] &)] // Timing

2.19961

enter image description here

$\endgroup$
  • $\begingroup$ Thank you ... this is very fast, but if I use my color table the output of your code is different than my image above, why? Is it possible to use Colorize with a legend showing which color corresponds to which brightness? $\endgroup$ – mrz Jul 13 '16 at 14:03
  • $\begingroup$ @mrz I'm unable to replicate your histogram or plot even when I run your code $\endgroup$ – Young Jul 13 '16 at 14:42
  • $\begingroup$ Now it should work. When I use Image@Raster[ImageData[image, DataReversed -> True], ColorFunction -> (Blend[colTable, #] &)] I get a relatively nice image. $\endgroup$ – mrz Jul 13 '16 at 16:47
  • $\begingroup$ @mrz Seems just as fast $\endgroup$ – Young Jul 13 '16 at 19:18
  • 2
    $\begingroup$ You might be interested in this. $\endgroup$ – J. M. will be back soon Jul 14 '16 at 2:45
0
$\begingroup$

Another color table:

colTable = 
 Flatten@{Table[Black, {x, 0, 12, 1}], 
   Table[{Blend[{Blue, Green, Yellow, Red}, x]}, {x, 1/10, 1, 1/10}], 
   Table[Black, {x, 24, 256, 1}]}

ArrayPlot[ImageData[image], ColorFunction -> (Blend[colTable, #] &), 
 PlotLegends -> Automatic, FrameTicks -> All]

enter image description here

Colorize[image, ColorFunction -> (Blend[colTable, #] &)]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.