From: magda magdaleno on
Hi...
i apologize for what may be trivial to most. I am new to Matlab and have not figured out how to fully optimize my routines.

Anyway... I am trying to read in an image of size rows x cols(817x1544). In which i use a 32x32 tile to compute the mean and replace the 4 center elements with the mean of the 32x32 tiles

i.e. 32x32 tile with a 2x2 kernel.
so my final image will be of size rowsxcols (817x1544) and it consist of the means of the image.

I have a working routine, but it takes about 10 seconds or so to run. Is there a way to make this run faster?


[rows,cols] = size(imgin);

mean_frame(1:rows,1:cols)=0;

Tsize=32; % tile size is 32x32
T_y_loc=Tsize; % the end location of the y-dir tile
T_x_loc=Tsize; % the end location of the x-dir tile
row_loc=1; % loc to store result in the new matrix
col_loc=1; % loc to store result in the new matrix


for T_y_loc=Tsize:2:cols
meany_loc= (T_y_loc+col_loc-1)/2;
for T_x_loc=Tsize:2:rows
roi= imgin(row_loc:1:T_x_loc,col_loc:1:T_y_loc); %32x32 stepping by 2
Tmean= mean2(roi);
meanx_loc= (T_x_loc+row_loc-1)/2;

mean_frame(meanx_loc:meanx_loc+1,meany_loc:meany_loc+1)= Tmean;
row_loc = row_loc+2;
end
col_loc = col_loc+2;
row_loc=1;

end
From: ImageAnalyst on
magda:
I'm not sure I exactly understand the 2by2 stuff versus the 32 by 32
stuff, but maybe conv2 will help. I think this is what you're after.

clc;
imageArray = zeros(817,1544);
% Take average within a 32x32 window.
kernel = ones(32,32) / (32*32);
tic;
meanImage = conv2(imageArray, kernel, 'same');
toc; % First timing.
outputImage = imageArray; % Copy original
% Copy blurred in checkerboard fashion.
% Only assign every other one.
outputImage(2:2:end, 2:2:end) = meanImage(2:2:end, 2:2:end);
toc; % Second timing

Elapsed time is 0.018213 seconds.
Elapsed time is 0.048766 seconds.

Regards,
ImageAnalyst
From: magda magdaleno on
convolution (conv2) is exactly what i needed!!!
I should have figured that out. I guess I need to re-learn linear Algebra!!
THANKS for your help!!!

FYI - the 2x2 stuff - i am basically using the mean of the 32x32 kernel/tile and replacing the 4 center pixels with that mean.
After comparing the 2x2 stuff with the mean for each pixel location, i think I will get rid of the 2x2 stuff since it doesn't buy me anything.

Thanks again for your help. You just made my life much easier!!
10seconds down to a fraction of a second!!

From: magda magdaleno on
What would be the best way to handle edge pixels?

edge pixels are not contained within a 32x32 kernel, so i can't simply divide by 32*32.

i was thinking of creating a matrix A that would contain all of the correct divisors. i.e. 32*32 throughout the image and the edge would contain the corresponding divisor.

then -
Kernel would simply be ones(32,32)
after conv2

OutputImage= meanImage./A


or is there a better way to handle the edge pixels using conv2

thanks
From: magda magdaleno on
NM
txs anyway...

i padded top/bottom left/right and then performed conv2

txs