Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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!

share|improve this question
add comment

4 Answers 4

up vote 10 down vote accepted

For example:

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

enter image description here

share|improve this answer
1  
ImageValue is also another choice –  HyperGroups May 8 '13 at 4:33
1  
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. –  Matthias Odisio May 8 '13 at 17:50
1  
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. –  nikie May 9 '13 at 9:16
    
Way to go beli :D –  Rojo May 12 '13 at 4:41
add comment
tp = ExampleData[{"TestImage", "Lena"}];
Manipulate[Column @ {tp, ImageValue[tp, p]}, {{p, {1, 1}}, Locator}]

a Manipulate for picking colors

share|improve this answer
add comment

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

share|improve this answer
add comment

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)

share|improve this answer
3  
As you hinted, in addition to ImageValue interpolating neighboring pixel values, the coordinate systems of ImageValue an PixelValue are off by one half. See the 'Properties and Relations' section of reference.wolfram.com/mathematica/ref/PixelValue.html –  Matthias Odisio May 8 '13 at 17:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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