I have a problem with image processing in matlab for a lab course. I have only basic matlab knowledge and hope to be able to explain my problem with my basic school english...
The code attached is a loop in matlab to calculate a "moving average" on a picture. The first and second loop is for the rows (german: Zeilen) and columns (Spalten) of the picture (P), the third and fourth loop is for the rows and columns of the filter matrix (kern). Then it sums up the values of the filter matrix and divides it by the sum of the loadings. ewr and ewc are needed to calculate the new border of the image. (zero padding or replicate)

My question:
I need a way to do this with fast matrix/vector operations if possible.
I am not allowed to use any imfilter, conv, conv2, block-processing or sliding-window functions.
What i can imagine is to exchange the 2 inner loops by a matrix-multiplication. I would multiply the filter matrix with the right submatrix of the image. But to replace the outer loops i need to know the position/index to know wich submatrix i have to multiply...
I read something about the not so well known accumarray() function but i don't know how i can use this for 2D operation.
Maybe someone can give me a hint...

Thanks in advance!

Greetings from Vienna, Austria.

beta

for ip = (ewr+1):(rc(1)+(ewr)) %Zeilen for np = (ewc+1):(rc(2)+ewc)%Spalten for Z = -ewr:ewr for S = -ewc:ewc PF(ip,np)=PF(ip,np)+P(ip+Z,np+S)*kern(Z+ewr+1,S+ewc+1); end end PF(ip,np)=PF(ip,np)/(sum(sum(kern,1),2)); endend

i did it with a matrix multiplication (see attached code) but it seems much slower than the previous version with four loops.

for ip = (row_ext+1):(rc(1)+row_ext) %rows for np = (col_ext+1):(rc(2)+col_ext)%columns PF(ip,np)=sum(sum(kern.*P( (ip-row_ext):(ip+row_ext) , (np-col_ext):(np+col_ext)),1),2)/sum(sum(kern,1),2); endend

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…

The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…