 # Return maximum value coordinates based from the sum of channel in a window in context of fill block.

To expand on the title, let’s say I have a 3 channels image with `5*5` window. I would like to return the coordinates of the max pixel using the sum of all channels using `5*5` window in context of fill block.

A short code from my head that extracts one set of coordinate.

``````sp cat
+compose_channels +
+z. 0,0,4,4
max_x={xM#-1}
max_y={yM#-1}
``````

That, but in context of fill block, and return rgb using the original image using max_v per crop coordinates.

This does the job : (not tested thoroughly, but should be OK) :

``````foo :
sp lena
100%,100%,1,2,"*
off = argmax(crop(#-1,x - 2,y - 2,0,c,5,5,1,1));
[ off%5, int(off/5) ] - 2"
``````

Using this code, I think it works as my goal was to return the color using the coordinates of maximum values of the sum of all channels

``````foo:
sp ,
1,1,1,100%,"
off=argmax(crop(#-1,x,y,0,c,5,5,1,1));
i(#-1,off%5,int(off/5));"
``````

But then, you are only looking for the max value in a 5x5 neighborhood ? Why not using

``````max(crop(#-1,x,y,0,c,5,5,1,1))
``````

then ?

Also, retrieving the max in a neighborhood is precisely the definition of the `dilate` operator, which is even faster to apply, with `dilate 5`.

Max of the sum of all channels. I find that the result is different here.

``````foo:
sp ,
1,1,1,100%,"
max(crop(#-1,x,y,0,c,50,50,1,1));
"
+z 0,0,49,49
compose_channels. +
xc={xM#-1}
yc={yM#-1}
1,1,1,{s#0},"
i(#0,"\$xc","\$yc");"
``````

In that case : `compose_channels. + dilate. 5` should do the job.

I’m not working on dilating as I’m working on extending features into rep_form_pixel as now it only offers weighed average. Using compose_channels. +, and argmax to find coordinates, and then use said coordinate in context of i(#n,x,y…) in context of fill block is the answer here.

Ok, what am I doing wrong here?

``````foo:
ww={ceil(w/\$1)*\$1}
hh={ceil(h/\$2)*\$2}
+compose_channels +
{ceil(w/\$1)},{ceil(h/\$2)},1,{s#0},"
pos=argmax(crop(#-1,x*\$1,y*\$2,0,0,\$1,\$2,1,1));
i(#-2,pos%\$1,int(pos/\$2));"
``````

Result aren’t exactly what I expect in the right.

EDIT: I forgotten x*\$1 and y*\$2. With this instead:

``````foo:
ww={ceil(w/\$1)*\$1}
hh={ceil(h/\$2)*\$2}
+compose_channels +
{ceil(w/\$1)},{ceil(h/\$2)},1,{s#0},"
pos=argmax(crop(#-1,x*\$1,y*\$2,0,0,\$1,\$2,1,1));
i(#-2,x*\$1+pos%\$1,y*\$2+int(pos/\$2));"
``````