I am trying to get the average value of a triangle, defined by three X,Y coordinates, on a 2D image. I'm pretty new to using the image processing functions so I am wondering if someone can point me in the right direction on this one. See the attached for visual explanation. The question is how to get at the average value within the triangle.
There are several approaches that one could take here and they depend mostly on how often you need to solve this type of problem and what sort of accuracy you expect.
I can't guess at the resolution of your data based on the image. If you have sufficient resolution you can define your triangle as an ROI (use the Image Processing Tools) and generate a mask wave such that it is set to 1 for every pixel inside the ROI and zero otherwise. If you call your mask wave mw then:
MatrixOP/O aa=sum(mw*imageWave)/sum(mw)
will give you the average value in the triangle.
If your image does not have sufficient resolution you could interpolate it using, e.g., ImageInterpolate/U=n, for some reasonable factor n. In this case you might also want to consider using Duplicate/R to start from the smallest part of the image that completely contains your triangle; there is no point in interpolating data in regions that do not affect the result.
If you do not want to depend on the mask approach you can always convert this into a 2D integration problem. You will need to provide a function that samples the data at arbitrary position (x,y) and a mathematical expression to define the boundaries of the triangle. The first part here (the function providing the value at (x,y) is essentially equivalent to the ImageInterpolate step suggested above.
Finally, I should mention that you would also get a pretty reasonable answer using a statistical/Monte-Carlo type approach:
1. Determine the X and Y range of the triangle.
2. Write a function that returns an interpolated value of data(x,y).
3. Generate a large number of XY pairs drawn from a uniform distribution.
4. Test if each XY pair falls in the triangle and if it does accumulate the value returned by data(x,y) and increment the count of points in the triangle.
5. Divide the accumulated sum by the count of points in the triangle.
I can't guess at the resolution of your data based on the image. If you have sufficient resolution you can define your triangle as an ROI (use the Image Processing Tools) and generate a mask wave such that it is set to 1 for every pixel inside the ROI and zero otherwise. If you call your mask wave mw then:
will give you the average value in the triangle.
If your image does not have sufficient resolution you could interpolate it using, e.g., ImageInterpolate/U=n, for some reasonable factor n. In this case you might also want to consider using Duplicate/R to start from the smallest part of the image that completely contains your triangle; there is no point in interpolating data in regions that do not affect the result.
If you do not want to depend on the mask approach you can always convert this into a 2D integration problem. You will need to provide a function that samples the data at arbitrary position (x,y) and a mathematical expression to define the boundaries of the triangle. The first part here (the function providing the value at (x,y) is essentially equivalent to the ImageInterpolate step suggested above.
Finally, I should mention that you would also get a pretty reasonable answer using a statistical/Monte-Carlo type approach:
1. Determine the X and Y range of the triangle.
2. Write a function that returns an interpolated value of data(x,y).
3. Generate a large number of XY pairs drawn from a uniform distribution.
4. Test if each XY pair falls in the triangle and if it does accumulate the value returned by data(x,y) and increment the count of points in the triangle.
5. Divide the accumulated sum by the count of points in the triangle.
I hope this helps,
A.G.
WaveMetrics, Inc.
October 11, 2016 at 01:12 pm - Permalink