TransWikia.com

Changing colour of ImageHistogram

Mathematica Asked on March 4, 2021

Is there a way I can show the three ImageHistogram[]‘s in different colours?

Show[ ImageHistogram[edit[[1]], Appearance -> "Separated"], ImageHistogram[edit[[2]], Appearance -> "Separated"], ImageHistogram[edit[[3]], Appearance -> "Separated"], ]

enter image description here

2 Answers

You can use ColorReplace to replace {Red, Green, Blue} with desired colors:

ClearAll[colReplace]
colReplace = ColorReplace[ImageHistogram[#, Appearance -> "Separated", ImageSize -> Large], 
    Thread[{Red, Green, Blue} -> #2]] &;

Examples:

edit1 = Import["https://i.stack.imgur.com/jUr0a.png"]

enter image description here

edit2 = ImageAdjust[edit, .1];

edit3 = ColorBalance[edit, .1];

Row[{edit1, edit2, edit3}, Spacer[10]]

enter image description here

colReplace[edit1, ColorData[97] /@ {1, 2, 3}]

enter image description here

Show[MapThread[colReplace, 
  {{edit1, edit2, edit3}, Partition[ColorData[97] /@ Range[9], 3]}]]

enter image description here

You can also use ReplaceAll to get the same results:

ClearAll[replaceColors]
replaceColors = ReplaceAll[Thread[{Red, Green, Blue} -> #2]]@
    ImageHistogram[#, Appearance -> "Separated", ImageSize -> Large] &;

Answered by kglr on March 4, 2021

The second suggestion by @kglr seems like a natural approach (which ensures the result still has Head Graphics and not Image).

Another solution would be to manually make a Histogram for each channel:

GraphicsColumn[
MapThread[Histogram[Flatten[ImageData[#1]],ChartStyle->#2]&,{ColorSeparate[edit1,"RGB"],Array[ColorData[61],3]}]
]

However, this also serves as more of an extended comment to highlight a curious behaviour (as hinted by the comments above). Note that ImageHistogram clearly has special logic for handling RGB images. This can be seen by comparing a 4-channel image ColorSpace, e.g. CMYK:

With[{cmyk = ColorCombine[ColorSeparate[edit1, "CMYK"], "CMYK"]},
 ImageHistogram[cmyk, Appearance -> "Separated"]]

enter image description here

Curiously, adding PlotStyle -> Array[ColorData[57], 4] also fails on this example, returning all histograms with the last specified color.
Perhaps this is indeed worth reporting to WR Support.

Answered by George Varnavides on March 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP