8
$\begingroup$

I have an imported and cropped image:

TestPicture = Import["filepath.jpg"];
TestPictureCropped = ImageCrop[TestPicture, Min[ImageDimensions[TestPicture]]];

How can I specify a coordinate in the image and return the color of the nearest pixel to the coordinate? Thanks for any help!

$\endgroup$
11
$\begingroup$

For example:

tp = ExampleData[{"TestImage", "Airplane"}];
Manipulate[Column@{tp, Extract[ImageData[tp], Round /@ p]}, 
          {{p, {1, 1}}, Locator}]

enter image description here

$\endgroup$
  • 1
    $\begingroup$ ImageValue is also another choice $\endgroup$ – HyperGroups May 8 '13 at 4:33
  • 1
    $\begingroup$ This answer is incorrect because it messes up different coordinate systems, as can be seen with tp = Image@IdentityMatrix[100]. PixelValue and ImageValue are the preferred way to accomplish this task. $\endgroup$ – Matthias Odisio May 8 '13 at 17:50
  • 2
    $\begingroup$ This gives the wrong values! The values in p are coordinates, so they are 0-based, start at the bottom-left corner and are stored in x/y order. Extract expects array indices, which are 1-based, start at the top-left corner and are in row/column (i.e. y/x) order. So the values you get are transposed, upside down and shifted by one pixel. $\endgroup$ – Niki Estner May 9 '13 at 9:16
  • $\begingroup$ Way to go beli :D $\endgroup$ – Rojo May 12 '13 at 4:41
12
$\begingroup$
tp = ExampleData[{"TestImage", "Lena"}];
Manipulate[Column @ {tp, ImageValue[tp, p]}, {{p, {1, 1}}, Locator}]

a Manipulate for picking colors

$\endgroup$
11
$\begingroup$

As a side-note - you can quickly get color info from images using image assistant. Just click once on the image and in version 9 assistant will appear below:

enter image description here

$\endgroup$
10
$\begingroup$

Also worth noting is that there's a companion function to ImageValue, called PixelValue. The difference between the two is, as far as I can tell from the documentation, that ImageValue interpolates the color of a coordinate according to the colors of nearby pixels, whereas PixelValue finds the color of the pixel whose centre is nearest to the integer values of the coordinates. The difference is hard to spot on smooth photographs, but possible:

tp = ExampleData[{"TestImage", "Mandrill"}];
Manipulate[Column
  [{tp,
   Text[p],
   Text["ImageValue"],
   ImageValue[tp, p],
   Graphics[{
     RGBColor[ImageValue[tp, p]],
     Rectangle[{0, 0}, {3, 1}]}],
   Text["PixelValue"],
   PixelValue[tp, p],
   Graphics[{
     RGBColor[PixelValue[tp, p]],
     Rectangle[{0, 0}, {3, 1}]}]
   }],
 {{p, {1, 1}}, Locator}
 ] 

pixelvalue

(Code adapted from this answer)

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