7 views (last 30 days)

Matt J
on 29 Sep 2013

Edited: Matt J
on 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);

Image Analyst
on 30 Sep 2013

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

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

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

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.

Opportunities for recent engineering grads.

Apply TodayFind the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
## 0 Comments

Sign in to comment.