How can i calculate the max value in a polygon?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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.
0 Kommentare
Akzeptierte Antwort
Matt J
am 29 Sep. 2013
Bearbeitet: Matt J
am 29 Sep. 2013
Once you've done this,
BW = createMask(handle);
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);
5 Kommentare
Matt J
am 1 Okt. 2013
Bearbeitet: Matt J
am 1 Okt. 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.
Weitere Antworten (1)
Image Analyst
am 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));
Then mask the image
maskedImage = grayImage; % Initialize
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).
2 Kommentare
Image Analyst
am 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
binaryImage = h.createMask;
%or
binaryImage = createMask(h);
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.
Siehe auch
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!