14
$\begingroup$

I want to change the above colored plot into a grayscale plot. I have tried this:

CCGray[pic_?ImageQ] := Module[{}, ColorConvert[pic, "GrayScale"]];

but it doesn't give me the degree of differentiation I would like.

$\endgroup$
4
  • 2
    $\begingroup$ @flurmer I believe your question has been answered here:mathematica.stackexchange.com/questions/8746/… $\endgroup$ Commented Sep 10, 2013 at 12:10
  • 3
    $\begingroup$ Can you operate on the BubblePlot itself? Ie. do you have access to the original data? $\endgroup$
    – DavidC
    Commented Sep 10, 2013 at 12:15
  • $\begingroup$ @David That's an excellent question! $\endgroup$
    – Mr.Wizard
    Commented Sep 10, 2013 at 12:24
  • $\begingroup$ It is indeed what I wanna do $\endgroup$
    – flumer
    Commented Sep 10, 2013 at 12:32

3 Answers 3

17
$\begingroup$

First, there is no need to use Module in your sample code above. You could write simply:

CCGray[pic_?ImageQ] := ColorConvert[pic,"GrayScale"]

Now, if you want to manage the channel mixing manually you can use ImageApply and Dot:

customGray[img_?ImageQ, ker_?VectorQ] := ImageApply[ker.# &, img]

img = Import["https://i.sstatic.net/wtlqF.jpg"];

customGray[img, {0.8, 0.5, -0.3}]

enter image description here

You'll notice that my three values add to one; this is typically necessary to preserve white as white. If you want to make this automatic just divide by the total:

customGray2[img_?ImageQ, ker_?VectorQ] := With[{k = ker/Tr@ker}, ImageApply[k.# &, img]]

Or with normalization control as an Option:

Options[customGray2] = {Normalize -> True};

customGray2[img_?ImageQ, ker_?VectorQ, OptionsPattern[]] :=
  With[{k = If[OptionValue[Normalize], ker/Tr@ker, ker]},
    ImageApply[k.# &, img]
  ]

This makes it easy to Manipulate:

Manipulate[
 customGray2[img, {a, b, c}],
 {a, -5, 5}, {b, -5, 5}, {c, -5, 5}
]

enter image description here

$\endgroup$
4
  • $\begingroup$ Thanks!that's REALLY what I want! $\endgroup$
    – flumer
    Commented Sep 10, 2013 at 12:27
  • 2
    $\begingroup$ @flumer You're welcome, and I'm glad I could help. However, as I tell everybody I suggest waiting 24 hours to Accept an answer, giving time for everyone around the world to reply. You never know what kind of great answer you might get if you give people a chance. (You can un-Accept by clicking the check-mark again.) $\endgroup$
    – Mr.Wizard
    Commented Sep 10, 2013 at 12:32
  • $\begingroup$ How come there are so many JPEG artifacts in your PNG images? $\endgroup$
    – user484
    Commented Sep 10, 2013 at 15:47
  • $\begingroup$ @Rahul This kind of extreme channel manipulation amplifies the JPEG artifacts of the original (which is not a PNG), particularly from the blue channel which receives less bytes due low human visual acuity on blue. $\endgroup$
    – Mr.Wizard
    Commented Sep 10, 2013 at 18:24
7
$\begingroup$

I assume you've rejected this option:

BubbleChart[RandomReal[{-1, 1}, {10, 5, 3}], 
  ColorFunction -> "GrayTones"]

gray bubbles

$\endgroup$
8
  • $\begingroup$ I think his response to David's question means that manipulation of an Image is indeed what he wants to do. $\endgroup$
    – Mr.Wizard
    Commented Sep 10, 2013 at 18:21
  • $\begingroup$ @Mr.Wizard lol, I misread the OPs answer comment, for a change - usually I don't understand the question! :) $\endgroup$
    – cormullion
    Commented Sep 10, 2013 at 18:53
  • 1
    $\begingroup$ Yes, I found it confusing as well, but then he Accepted my answer. Here's a pity vote: +1 :^) $\endgroup$
    – Mr.Wizard
    Commented Sep 10, 2013 at 21:06
  • $\begingroup$ @Mr.Wizard thanks, I'll treasure it! (You already had my (non-pity) vote...) $\endgroup$
    – cormullion
    Commented Sep 10, 2013 at 21:59
  • 1
    $\begingroup$ lol -- I guess you were right after all. @flumer In the future please try to make your question more clear and specific; you showed both a JPG as the input and a processing function that operates on an Image -- there is no indication that you were even generating the bubble chart yourself. $\endgroup$
    – Mr.Wizard
    Commented Sep 11, 2013 at 12:21
7
$\begingroup$

Here's an idea to post-process the grayscale image to increase the differentiation between shades. The image values (excluding pure black and white) are fed to HistogramTransformInterpolation which provides a transform function that approximately flattens the image histogram.

bc = BubbleChart[RandomReal[{-1, 1}, {10, 5, 3}]] ~ColorConvert~ "Grayscale"

enter image description here

ImageApply[HistogramTransformInterpolation @
  DeleteCases[ImageData[bc] ~Flatten~ 1, 1. | 0.], bc]

enter image description here

While this seems to work okay on an uncompressed image, it is not so good with the OP's original image due to the jpeg compression:

enter image description here

Better results can be had by removing near-white values from the image data before computing the histogram transform, but there are still visible artefacts.

ImageApply[HistogramTransformInterpolation@
  Select[ImageData[bc]~Flatten~1, 0 < # < 0.95 &], bc]

enter image description here

$\endgroup$
2
  • $\begingroup$ I wish I could try this in v7. Could you include the result of applying this to the OP's example image, for direct comparison? $\endgroup$
    – Mr.Wizard
    Commented Sep 10, 2013 at 14:32
  • $\begingroup$ @Mr.Wizard, results are, um, not that great with the original image... $\endgroup$ Commented Sep 10, 2013 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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