       Re: ListCorrelate[] ??

• To: mathgroup at smc.vnet.net
• Subject: [mg30182] Re: ListCorrelate[] ??
• From: "Mariusz Jankowski" <mjkcc at usm.maine.edu>
• Date: Wed, 1 Aug 2001 02:19:49 -0400 (EDT)
• Organization: University of Southern Maine
• References: <9jtkeq\$894\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Brent,
"Brent Pedersen" <bpederse at nature.berkeley.edu> wrote in message
news:9jtkeq\$894\$1 at smc.vnet.net...
> with a real square matrix H, I calculate the sum of each cell and its 8
> neighbors as:
> Htot=ListCorrelate[kern, H ,{2,-2}];     (thanks to the help of some of
> you)
> where:
> kern = Table[1, {3}, {3}];
>

You probably should use Htot=ListCorrelate[kern, H ,{2, 2}]. For a 3x3
kernel
ListCorrelate[kern,H,{2,-2}] == ListCorrelate[kern,H,{2,2}], but this is not
true for larger kernels.

> now, for each 9 cell neighborhood around (and including cell H[[i,j]], I
> wish to calculate:
> Sum[(H[[m,n]]/Htot[[i,j]])^c,{m,i-1,i+1},{n,j-1,j+1}];
> (when c=1, this will be 1.)
> of course, this code won't work as is but the idea is there.I can write
> loops containing a Sum[] similar to that above,but i am sure that
> ListCorrelate[] operates much faster (which is important since H is
> 256*256).in short, i need to output a 256*256 matrix where each cell
> contains the result of the Sum[] function above.
>
> ListCorrelate[kern, H/Htot,{2,-2}]; does not work because it contains
> H[[i,j]]/Htot[[i,j]]  not H[[i,j]]/Htot[[m,n]];
>

Right, correlation will not return the same result, but here is a reasonable
solution:

Table[(Tr[Flatten[(#1/Tr[Flatten[#1]] )^c]] & )[ H[[{i - 1, i, i + 1},{j -
1, j, j + 1}]] ],
{i, 2, 255}, {j, 2, 255}]

or

Htot=ListCorrelate[kern,H,{2,2}];

Table[(Tr[Flatten[(#1/Htot[[i,j]])^c]] & )[ H[[{i - 1, i, i + 1},{j - 1, j,
j + 1}]] ],
{i, 2, 255}, {j, 2, 255}]

where Tr[Flatten[...]] is just a fast way to calculate Sum. The second
solution may be faster since you are precomputing the averages. If you
REALLY need to have a 256x256 result, then you should pad the original data
prior to the calculations shown above and change the iterators in Table to
1,256

A question. The calculation you desire is a form of edge
enhancement/detection. The calculation using two subsequent ListCorrelates
which you rejected also enhances edges. Why is one better than the other?

Mariusz

--
======================================
Mariusz Jankowski
University of Southern Maine
email: mjkcc at usm.maine.edu

```

• Prev by Date: RE: SquareFreeQ vs. MoebiusMu
• Next by Date: Re: Lists and speed
• Previous by thread: Re: SquareFreeQ vs. MoebiusMu
• Next by thread: Re: Re: ListCorrelate[] ??