Message Boards Message Boards

0
|
4891 Views
|
7 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Why doesn't /.{r_ ,g_ ,b_ }:>1-{r,g,b} invert RGB encoding?

Posted 9 years ago

In the book by Paul Wellin "Programming With Mathematica: An Introduction" (3rd printing; ISBN 978-1-107-00946-2 hardback), he gives an example on page 85 in "Patterns and Rules, Section 4.1".

It appears that the example attempts to demonstrate the use of "/." (ReplaceAll) to invert the RGB encoding of the example Image using the Rule {r,g,b_}:>1-{r,g,b}.

When I attempt to use this construct on an Image that I have imported, the function does not invert the encoding. I am using Mathematica v10.3.

POSTED BY: Coach KLC
7 Replies
Posted 9 years ago

Maybe something like this can be of help?

color /. RGBColor[r_, g_, b_] -> 1 - # & /@ RGBColor[r, g, b]

enter image description here

POSTED BY: Hans Milton

Or one could use ColorNegate:

ColorNegate[img]
POSTED BY: Sander Huisman

Hi coachkic.

images can contain an alpha channel, i.e. each pixel then has the form {r,g,b,alpha}. You can check e.g. using:

Dimensions @ ImageData[img]

More information you can find in the documentation on AlphaChannel and friends.

If this is not the case here you should supply the image.

Regards -- Henrik

POSTED BY: Henrik Schachner
Posted 9 years ago

Thanks, Henrik -- The attached notebook excerpt contains the image. I really appreciate your response. Coachklc

Attachments:
POSTED BY: Coach KLC

Then I think one has to do it like:

newImg = Image[ImageData[oldImg] /. {r_, g_, b_} :> 1 - {r, g, b}]
POSTED BY: Henrik Schachner
Posted 9 years ago

Thank you very much, Henrik!

POSTED BY: Coach KLC

Well, I think my answer was sort of incomplete! It is a good and legitimate question, why the replacement is only working on the data, but not on the image itself. The reason is that Image yields an atomic object:

img = Image[RandomReal[1, {2, 2, 3}], ImageSize -> 200];
AtomQ[img]  (*  Out:  True  *)

And that means that one has no access to its subparts, i.e. one can not do things like replacements or e.g.:

List @@ img

Regards -- Henrik

POSTED BY: Henrik Schachner
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract