简体   繁体   中英

Getting pixel location given its value

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

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, eg 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 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM