0

is it possible in matlab to get the location of a pixel(rows and column) if the value at that pixel location is known?

Thanks in advance.

Regards

Shai
  • 111,146
  • 38
  • 238
  • 371
Roma
  • 49
  • 9

1 Answers1

2

You can use find to get the coordinates of the pixel

[y x] = find( grayImg == val, 1 ); %// find one pixel that has intensity val

For RGB image, you need three values

[y x] = find( rgbImg(:,:,1) == r_val & rgbImg(:,:,2) == g_val & rgbImg(:,:,3) == b_val, 1 )

In case of single precision image, one might find the comparison == too strict (see, e.g. this thread). Therefore, a relaxed version can be applied:

thresh = 1e-5;
[row col] = find( abs( grayImg - val ) < thresh, 1 );

To find a pixel within thresh tolerance of val.

You may also try and find the pixel with value closest to val:

[~, lidx] = min( abs( grayImg(:) - val ) );
[row col] = ind2sub( size(grayImg), lidx );
Community
  • 1
  • 1
Shai
  • 111,146
  • 38
  • 238
  • 371
  • @Roma are you certain there is a pixel with that value in your image? make sure your image values are in the same scale as the values you are searching for; For instance, if your image is of type `uint8` the values should be in range `0..255` – Shai Apr 10 '16 at 15:08
  • the type is single – Roma Apr 10 '16 at 15:09
  • using `==` with floating points can be tricky [see this thread for example](http://stackoverflow.com/questions/686439/why-is-24-0000-not-equal-to-24-0000-in-matlab). you might need to relax the `==` condition or convert to `uint8` for comparisons. – Shai Apr 10 '16 at 15:10
  • what changes are supposed to be made in the code that you have provided? – Roma Apr 10 '16 at 15:11
  • @Roma what variant? the gray image or the rgb image? – Shai Apr 10 '16 at 15:13
  • how to modify it if the image is a complex image where real and imaginary parts are floating point in nature – Roma Apr 10 '16 at 15:27
  • @Roma you can combine the insights from single precision image and the RGB solutions – Shai Apr 10 '16 at 16:25
  • is it possible for me to convert the matlab code to scilab? – Roma Apr 13 '16 at 05:39