5 views (last 30 days)

Hi

I have this image where I have identified the different zones using RBG.

After that I stored the different colorareas from zone 1-9.

Ive placed the image inside a x - y axis.

xlim([0 10]) & ylim([0 10])

Now I have a point value of: point = ([5 2]);

This when i plot this, it is inside the "blue" area stored as zone_6.

The RGB color is: ([0 0 204])

How can I make a code that, when I plot a value it gives me which zone it is in?

Ive placed my code in a zip file, so you don't have to identify the different zones, Ive already done that. But I cant solve how to find the zones from a x-y coordinate.

Guillaume
on 6 Sep 2017

Edited: Guillaume
on 6 Sep 2017

As per José-Luis' comment, using numbered variables is a bad idea. In your case, it's going to make the code harder to expand as well. What if you want 10 zones? Ok, you can copy/paste two more lines. What if you want 100 zones? copy/paste is going to be a nightmare.

You could make your zone creation more clever (and shorter), which at the same time would help with your question, identifying which zone a pixel belongs to.

let's start by defining your zones colours:

zonecolours = [128 128 0 %rgb triplet

193 221 198

126 0 1

127 127 127

205 204 0

0 0 204

229 126 127

0 229 0

185 122 87];

Note that I use the power of matlab: matrices, to just have everything in one variable. It's trivial to add more zones: just add rows.

Now let's find out which zone a pixel belongs to:

%img: a rgb colour image of arbitrary size

[~, pixelzone] = ismember(reshape(img, [], 3), zonecolours, 'rows');

pixelzone = reshape(pixelzone, size(img, 1), size(img, 2));

What I've done here is reshape the image into just rows of pixels where each column is an RGB triplet. Then the second output of ismember tells me which row of zonecolour each pixel belongs to (it'll be 0 if the pixel doesn't belong to any zone). I then reshape that output back into the image shape.

Now you can simply use that pixelzone matrix for your imfill:

for zoneindex = 1:size(zonecolours, 1)

imfill(pixelzone == zoneindex, 'holes');

end

And finding which zone a pixel belongs to is trivial: it's the pixelzone matrix, pixelzone(row, col) tells you which zone the pixel at (row, col) belongs to. If you work in axes coordinates, you just have to convert these coordinates to pixel coordinates.

Guillaume
on 6 Sep 2017

There are many ways you could do the conversion. I've just discovered the function axes2pix (by doing a search for matlab axes coordinates to image pixel), which you can use like so:

%...

himg = imagesc([0 10], [0 10], img); %get a handle to the image

%...

x = 5; y = 2;

pixelrow = axes2pix(size(img, 1), himg.YData, y);

pixelcol = axes2pix(size(img, 2), himg.XData, x);

Or you could calculate it yourself since you know the mapping:

pixelrow = round(interp1(himg.YData, [1 size(img, 1)], y);

pixelcol = round(interp1(himg.XData, [1 size(img, 2)], x);

Floriane Madeleine Schreiber
on 5 Sep 2017

Hello Mikkel,

I think, the first thing to do, is to find the value in your matrix corresponding to your coordinates. For example : x = 5. in your matrix the length of x is 2000. You use a cross multiplication, and you find the index_x = 1000. for y=2, you do the same, but now you find a decimal number index_y=337.8000. You cab round it (index_y=338) or look the value before (index_y=337) and after (index_y=338); Now, i have the two index that i can put in my matrix img; like : a=img(338,1000,:);

and a will be :

a(:,:,1) = 0 a(:,:,2) = 0 a(:,:,3) = 204

so you have your RGB code for your x and y coordinates.

now, you can add some if-condition with the number like :

b=[a(:,:,1) a(:,:,2) a(:,:,3)];

if b(1)==0 && b(2)==0 && b(3)==204 sprintf('it is blue and it is in the zone_6') zone='zone_6'; end

with your different RGB codes !

Hope it helps you and answers at your question. Don't hesitate to ask me if it is not clear or not complete.

Guillaume
on 6 Sep 2017

@Floriane,

Please use the formatting tools to make your post more readable. See the help link that you can click each time you write/edit a comment/answer.

Kevin Krüger
on 6 Sep 2017

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/355264-identify-which-color-zone-a-x-y-point-is-in#comment_482203

⋮## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/355264-identify-which-color-zone-a-x-y-point-is-in#comment_482203

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/355264-identify-which-color-zone-a-x-y-point-is-in#comment_482276

⋮## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/355264-identify-which-color-zone-a-x-y-point-is-in#comment_482276

Sign in to comment.