Asked by Mikkel Ibsen
on 5 Sep 2017

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.

Answer by Guillaume
on 6 Sep 2017

Edited by Guillaume
on 6 Sep 2017

Accepted Answer

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.

Mikkel Ibsen
on 6 Sep 2017

Wow, it looks so easy :), but there is still the small task of a point value from the defined x and y axis i put on the image. How do I make it give me which zone it is in?

% Create figure

figure1 = figure;

% % Create axes

axes1 = axes('Parent',figure1);

hold(axes1,'on');

imagesc([0 10], [0 10], img);

xlim(axes1,[0 10]);

ylim(axes1,[0 10]);

axis(axes1,'ij');

I put this below your code, and now I place a point at coordinate 5,2, then it should tell me it belongs in zone 6. Please help :) Dont know how to transfer a axial coordinate to a pixel coordinate :)

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

Mikkel Ibsen
on 6 Sep 2017

Thanks Guillaume :) just what I was looking for. If I wanted to make the x and y axis logarithmic why doesnt this work?

himg = imagesc(loglog([0.1 10], [1 1000]), img); %get a handle to the image

Is there a way that I can make the picture in logarithmic scale so when I go in with [0.1 600] it says its in zone 7 and not 5? Its a small tester for the code.

Sign in to comment.

Answer by 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.

Mikkel Ibsen
on 6 Sep 2017

Hey Floriane

Im a bit confused on how you use cross multiplication to get the x value in the image?

Can you write the code for me to find the x and y component in the image? (x = 5) == (index_x = 1000) and (y = 2) == (index_y = 337.8)

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.

Sign in to comment.

Answer by Kevin Krüger
on 6 Sep 2017

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## José-Luis (view profile)

## Direct link to this comment

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

## Stephen Cobeldick (view profile)

## 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.