From: omegayen on
Hi,

So I was wondering if anyone knows how to make this faster (without just using one parfor).

Essentially what the below code does is the following. I have a mask (logical of 0 or 1). If the mask is equal to 1 then in the image X (square image with length imagesize) I take that corresponding pixel and set it equal to the sum of the pixels to the top, left, bottom, and right and then divide by 4.

for ii=1:imagesize
for jj=1:imagesize
if mask(ii,jj)==1 %mask is same size as X
X(ii,jj)=(X(ii-1,jj)+X(ii,jj-1)+X(ii,jj+1)+X(ii+1,jj))/4;
end
end
end

Does anyone know of a better way to accomplish this in MATLAB? Thanks.
From: ImageAnalyst on
Simple. Just 3 lines. Just make up a kernel that's your pattern.
kernel = [0 1 0; 1 0 1; 0 1 0] / 4;
then use that in conv2 with your image
filteredImage = conv2(imageArray, kernel); % or use imfilter()
Then just mask your image
result = filteredImage .* (mask > 0)
and you're done.
From: omegayen on
ImageAnalyst <imageanalyst(a)mailinator.com> wrote in message <dbb2de4f-e112-46b7-94df-c9d490221b60(a)f12g2000yqn.googlegroups.com>...
> Simple. Just 3 lines. Just make up a kernel that's your pattern.
> kernel = [0 1 0; 1 0 1; 0 1 0] / 4;
> then use that in conv2 with your image
> filteredImage = conv2(imageArray, kernel); % or use imfilter()
> Then just mask your image
> result = filteredImage .* (mask > 0)
> and you're done.

works great ImageAnalyst thanks for your help..

one note though is that I had to change your above code slightly

kernel = [0 1 0; 1 0 1; 0 1 0] / 4;
filteredImage = conv2(X, kernel);
result = filteredImage(2:imagesize+1,2:imagesize+1) .* (mask > 0);

filteredImage was adding an extra row and column on each side thus if imageArray was 128 by 128 then filteredImage was 130 by 130
From: ImageAnalyst on
You can use the 'same' option so that you don't have to do that:

Cs = conv2(A,B,'same') % Cs is the same size as A