2
$\begingroup$

For example, to change the color of each pixel to the mean color of the three channels, I tried

i = ExampleData[{"TestImage", "Lena"}];
Mean[i]

but it just remains unevaluated:

enter image description here

How can I read the colors of an image into a list or matrix and change the color codes and save it back to an image?

$\endgroup$
  • $\begingroup$ I would rather not close but on second read I agree that this question needs to be refined. $\endgroup$ – Mr.Wizard Feb 11 '12 at 14:58
  • $\begingroup$ I reworded the question. $\endgroup$ – nilo de roock Feb 11 '12 at 15:06
  • $\begingroup$ The title of your question was too broad, which attracted answers that were tangential. I think your core question was on how to access the underlying information pixel by pixel in an image. I've reworded your question to reflect that. Please let me know if this was indeed what you intended $\endgroup$ – rm -rf Feb 11 '12 at 18:05
7
$\begingroup$

Certainly. For instance, here's how to reduce the number of colours to 10 (randomly chosen in RGB space):

i = Import["ExampleData/lena.tif"]

Mathematica graphics

You can try ImageData[i] to see the actual RGB values for each pixel. Now produce ten random triplets of reals between 0. and 1., and construct a function to quickly pick the one closest to some given number:

colours = RandomReal[{0, 1}, {10, 3}];
nf = Nearest[colours];

Then map the thing over the RGB values of the image and look at it:

Map[First[nf[#]] &, ImageData[i], {-2}] // Image

Mathematica graphics

Try increasing the number of randomly selected colours to see what happens:

Manipulate[
 Module[{colours = RandomReal[{0, 1}, {num, 3}], nf},
  nf = Nearest[colours];
  Map[First[nf[#]] &, ImageData[i], {-2}] // Image
  ],
 {{num, 10}, 1, 1000, 1}
 ]

Mathematica graphics

$\endgroup$
  • $\begingroup$ Note that good ol' Lenna can also be invoked with ExampleData[{"TestImage", "Lena"}]. $\endgroup$ – J. M. is away Feb 11 '12 at 15:09
  • $\begingroup$ Wow, and so easy. Thanks. $\endgroup$ – nilo de roock Feb 11 '12 at 15:09
  • $\begingroup$ @ndroock1 yes it feels like cheating $\endgroup$ – acl Feb 11 '12 at 15:11
5
$\begingroup$

The ImageApply function applies any suitable Mathematica function to every pixel in an image. You just have to specify the transformation you want to make. I recently asked this: Image levels: how to alter 'exposure' of dark and light areas? question - and got many good answers...

$\endgroup$
  • $\begingroup$ If I wasn't sitting here with just my iPad today I'd have given a better example... $\endgroup$ – cormullion Feb 11 '12 at 16:18
4
$\begingroup$

Have you looked through the included documentation?

guide/ImageProcessing

tutorial/ImageProcessing

There are also a number of entries on the Wolfram Blog that relate to image processing:

the incredible convenience of mathematica image processing

aMAZEing image processing in mathematica

the battle of the marlborough maze at blenheim palace continues

fun with line-art

fixing bad astrophotography

To learn more on the subject ask Heike. ;-)

$\endgroup$
  • $\begingroup$ That is, I checked the Documentation Center. Thanks I'll plan a day to go through it all. $\endgroup$ – nilo de roock Feb 11 '12 at 15:02
3
$\begingroup$

There are also several built-in effects available through ImageEffect. For example:

ImageEffect[ExampleData[{"Image","Lena"}], #]& /@ 
    {"Charcoal", {"OilPainting", 10}, {"Posterization", 5}}

Mathematica graphics

$\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.