Hi,
From what I can from the data in the image below, I think the explanation is
The single value index increases in a range from 0-N (in the example 0-20) from Left to Right, Top to Bottom
Linear2DSet: Length = 20
Dimension 1: Length = 4 Range = 0.0 to 3.0
Dimension 2: Length = 5 Range = 4.0 to 0.0
Dimension 1: is from 0-n (in the example 0-3) from left to right
Dimension 2: is from 0-n (in the example 4-0) from top left to bottom left (meaning a y index decreasing from top to bottom)
Is this correct? What would happen in cases where there are multiple levels? Would I need to make a 2D grid before using indexToValue()?
I am just trying to perform a box average over multiple pixels in an image and make sure I know what I am averaging. I believe my first attempt was completely incorrect....
Code: Select all
def averageOverPixels(grid,boxsize=(10,10)):
"""
newGrid=averageOverPixels(grid,boxsize=(x,y))
Create a new grid which is the average value in a specified box
for each pixel in original grid.
"""
if not (grid.isFlatField()):
grid = grid[0]
else:
grid = grid
#get the grid size
domainDimensions=getDomainDimension(grid)
if (domainDimension >= 3):
make2D(grid)
[nx,ny]=getDomainSizes(grid)
ds=grid.getDomainSet()
outGrid=grid.clone()
for x in xrange(0,nx,boxsize[0]):
for y in xrange(0,ny,boxsize[1]):
indices=[]
for xx in xrange(x,x+boxsize[0]):
for yy in xrange(y,y+boxsize[1]):
currentIndex= yy*nx + xx
print currentIndex, xx, yy, ds.indexToValue([currentIndex])
indices.append(currentIndex)
thisAverage=computeAverage(grid,indices)
outGrid=replace(outGrid,indices,thisAverage)
return outGrid
I am sorry this is so elementary, but I am a bit stuck on how to index the correct locations in the 1D array.
Joleen