Solved

Change loop to matrix operation in matlab (image processing)

Posted on 2008-10-29
1
1,772 Views
Last Modified: 2016-02-10
Hei!

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));  
    end
end

Open in new window

0
Comment
Question by:strupet
1 Comment
 

Accepted Solution

by:
strupet earned 0 total points
ID: 22838649
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);
    end
end

Open in new window

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now