- Using numbered variables is horrible code™. Best not to do it or it is a near certainty you'll regret it later.
- You pass nine (numbered) variables to your function and proceed to immediately delete everything. What's the point of that? To add insult to injury, you also hard-code them after having destroyed them.
Identify which color zone a x - y point is in
1 view (last 30 days)
Show older comments
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/167041/image.png)
2 Comments
José-Luis
on 6 Sep 2017
Edited: José-Luis
on 6 Sep 2017
Two quick comments about your code:
I don't have the Image Processing Toolbox so it is tricky for me to help you further.
Stephen23
on 6 Sep 2017
"After that I stored the different colorareas from zone 1-9."
As everyone has already told you, creating and accessing numbered variables is invariably a bad way to write code. Read this to know why:
Accepted Answer
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.
3 Comments
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);
More Answers (2)
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.
2 Comments
Guillaume
on 6 Sep 2017
Edited: 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
If I understand this correctly, you already specified each Zone by the points which belongs to it. Now to solve your Problem, you have to go the other direction. With a given Point you can determine the corresponding color. And with the color you can determine the corresponding Zone. I think this is what Floriane is doing.
0 Comments
See Also
Categories
Find more on Modify Image Colors in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!