Programatically creating a polygonal ROI mask

Hello,

I'm looking to analyze data in an image locally between subsets of points of a sparse grid that I have computed for the image. I was reading through the IGOR documentation and thought of creating a ROI mask from a polygon whose vertices have been picked from the sparse grid and then using ImageGenerateROIMask to create the mask. The only problem with this idea is that for each subset of data I want to perform an analysis on I will need to display the image, the polygon, and run ImageGenerateROIMask, something that needlessly slows down the computation.

So my question is, how can I do all that without going through displaying the image with the new polygon every time I go from one polygon to the next?
You do not need to use ImageGenerateROIMask and you certainly do not need to display any image in order to generate an ROI wave.

The ROI wave needs to satisfy two conditions: it has to have the same dimensions as the image and it has to be unsigned byte wave. You can generate the wave using commands, e.g.,
Make/B/U /N=(100,200) roiWave=0
roi[20,40][50,60]=1
or if you have pairs of waves defining a boundary you could use ImageBoundaryToMask. In both cases you do not need to display anything.

A.G.
WaveMetrics, Inc.
Yes, but in the case you're describing the roiwave is a rectangle of ones defined by vertices (20,50) (40,50) (20,60) and (40,60).

In the case I'm looking into the vertices do not form a rectangle; it's more like (20,30) (10,50) (40,20) and (50,80). That is why I was looking into drawing tools and how to programmatically define a polygon through the coordinates of it's vertices, then passing that shape to ImageGenerateROIMask, which I gather is how the ROI tool of the ImageProcessing toolbox does things.

Any suggestions as to how do that? If it's possible through a simple wave assignment statement I'm more than happy to use that.

At the end of AG's reply, he mentioned using an XY pair of waves to define a non-rectangular ROI.

It sounds like you know the coordinated of the vertices of your polygon. So this method should be appropriate for you. Once you have created the XY waves, use ImageBoundaryToMask to create the ROI.

Thanks for the replies!

I ended up implementing the even-odd rule to test whether a point is in the polygon defined by the vertices and to set the value of the mask accordingly. Here's the test (adapted from https://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html). It might not be the most elegant implementation, but it works nicely.

Function isPointInPath(testx,testy,polyV)

Wave polyV

Variable testx,testy

Variable num = dimSize(polyV,0)

Variable i =0

Variable j = num-1

Variable c=0

For(i=0; i<num; i+=1)

    if (((polyV[i][1]>testy) != (polyV[j][1]>testy)) && (testx<((polyV[j][0]-polyV[i][0])*(testy-polyV[i][1])/(polyV[j][1]-polyV[i][1])+polyV[i][0])))
   
        c = !c
   
    endif
   
    j=i
   
Endfor

return c

End