# How can i calculate the max value in a polygon?

7 views (last 30 days)
Maayan on 29 Sep 2013
Commented: Matt J on 1 Oct 2013
Hi, I have an image and i have a certain polygon that i draw on the image.I would like to know what is the max value in this polygon and also it's indices. I have been trying to find a function that does it but with no success. Thanks ahead for the help.

Matt J on 29 Sep 2013
Edited: Matt J on 29 Sep 2013
Once you've done this,
You can get the max value and its location just by doing
[maxval,idx]= max(yourImage(BW));
[i,j]=find(BW);
imax=i(idx);
jmax=j(idx);

Image Analyst on 30 Sep 2013
I don't remember seeing the last few lines. But with those, it will work. I was looking at idx, which is not the same linear index as in the full sized matrix. And if you used yourImage(idx) you wouldn't get the max, but with the rest of the code if you use yourImage(imax, jmax) you will get the correct location, as you said.
Maayan on 1 Oct 2013
Matt, I used what you wrote and it works perfectly (thank you very much). I just want to really understand what it does exactly and i don't understand what idx stands for and what happens there. I will really appreciate a brief explanation.
Matt J on 1 Oct 2013
The expression yourImage(BW) extracts the pixels in the polygon and puts them in a separate vector. The statement
[maxval,idx]= max(yourImage(BW));
finds the maximum value and its location (idx) within that vector. The rest of the code just converts idx to the location of the max in the coordinates of the original image.

Image Analyst on 29 Sep 2013
How did you draw it? With roipolyold()? With impoly()? With roipoly()? Anyway if you have the vertices you can get a mask with poly2mask()
[rows, columns] = size(grayImage);
binaryImage = poly2mask(xVertices, yVertices, rows, columns);
Then find the max value
maxValue = max(grayImage(binaryImage));
minValue = min(grayImage(binaryImage));
maskedImage(~binaryImage) = minValue; % Outside mask is set to the min value.
And find the row(s) and columns(s) where the max occurs in the masked region:
[maxRows, maxColumns] = find(grayImage == maxValue);
That will work unless your minValue is the same as your maxValue (so you should check for that if you want robust code).

Maayan on 29 Sep 2013
I used the impoly fuction. Can you help me understand the difference between poly2mask and createMask? This is part of my code:
handle=impoly;
accepted_pos = wait(handle);
and i didn't understand why you look for the min value? why do i need it?
Image Analyst on 29 Sep 2013
poly2mask makes a mask if all you have is some vertex locations. If you've use h=impoly, you can just do
%or
I got the min value because I like to write robust code. Let me explain. You said you want the location of the max. So if you just did find(grayImage(binaryImage) == maxValue) then what that does is to create a 1D vector out of grayImage(binaryImage). So then you're essentially doing find(vector1D== maxValue). Now all location reference to your original image is totally lost. You will get an index into that 1D vector but you don't know where that lies in your original image. Using ind2sub() won't even help you. So to avoid that, I used find on the whole image. But what if the max value also occurs OUTSIDE your polygon? It would find it outside the polygon also, which you don't want. So how do you avoid finding any max values that might be living outside the polygon, yet retain the ability to know the true (column, row) location? Well, you can set everything outside the polygon to something less than the max value inside the polygon. I could have set it to maxValue - 1, in fact I should have because that wouldn't require any time to compute like finding the min does. But I just set it equal to the min in case you wanted to visualize the masked image. But either way works. I hope that explains it better.