Reputation: 49
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
Upvotes: 0
Views: 142
Reputation: 114786
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 );
Upvotes: 2