Reputation: 23
how can I get the x,y coordinates for pixels in an image by rgb values in matlab ?
for example : I've got an image that I want to locate the pixels coordinates od the black area in it..
Upvotes: 2
Views: 775
Reputation: 23685
There is a built-in function that does this: impixel
.
From the official documentation:
Return Individual Pixel Values from Image
% read a truecolor image into the workspace
RGB = imread('myimg.png');
% determine the column c and row r indices of the pixels to extract
c = [1 12 146 410];
r = [1 104 156 129];
% return the data at the selected pixel locations
pixels = impixel(RGB,c,r)
% result
pixels =
62 29 64
62 34 63
166 54 60
59 28 47
Link: https://it.mathworks.com/help/images/ref/impixel.html
[EDIT]
Ok, I misunderstood your question. So to accomplish what you are looking for just use the following code:
img = imread('myimg.png');
r = img(:,:,1) == uint8(0);
g = img(:,:,2) == uint8(0);
b = img(:,:,3) == uint8(255);
[rows_idx,cols_idx] = find(r & g & b);
The example above finds all the pure blue pixels inside the image (#0000FF) and returns their indices. You can also avoid casting values to uint8
, it should work anyway by implicitly converting values during comparison.
Upvotes: 0
Reputation: 114866
If you want to find all coordinates of pixles with values (R, G, B)
then
[y, x] = find(img(:,:,1)==R & img(:,:,2)==G & img(:,:,3)==B);
For black pixels choose R=0, G=0, B=0
Upvotes: 2