Help with stereo matching and gpu, migrating code to reduce processing time

1 view (last 30 days)
Hi, I got my code that estimate disparity between two stereo images and I want to reduce the processing time, I watch some webinar videos about gpuArray and arrayfun to improve times but I can't figure out how to do it with my code, basically this is what I do:
1. Every estimation disparity is independent, take 1 position (i,j) and define a block around it, normally is a 7x7 block, this is my reference block, this is from the left image. 2. In the same position (i,j) create a search block, bigger than reference block, where the y-length is the same but the x-length is increased accordingly the range disparity, this one is from the right image. 3. Search for the best matching block (7x7 size) and find the "distance" between these two
What I do in my code, once I define reference block and search block, I do another cycle to search the best matching one.
I tried with gpuArray function, but everytime I define a block, this take too much time, also I tried with arrayfun in the last "for cycle" but both, the reference and search block must be the same size, any suggestions will be appreciated. Here is my code:
a1=double(rgb2gray(a)); %img left
[m n ~]=size(a);
b1=double(rgb2gray(b)); %img right
ddpp=zeros(m,n); %disparity image
for j=1:m
for i=1:n
xL=i-round(p/2); %creating reference block
xR=i+round(p/2); %and border restrictions
yU=j-round(p/2);
yD=j+round(p/2);
if xL<1
xL=1;
end
if xR>n
xR=n;
end
if yU<1
yU=1;
end
if yD>m
yD=m;
end
bloqueref=a1(yU:yD,xL:xR);
[M, N]=size(bloqueref);
x=xL-w+1; %border restrictions for search block
x1=xR+1;
y1=j-round(p/2);
y2=j+round(p/2)+M-1;
if x<1
x=1;
end
if x1>n
x1=n;
end
bloqbusq=b1(yU:yD,x:x1);
[f, h]=size(bloqbusq);
NCC=0; %matching one ref and one search block
for k=1:h-N
otrobloqref=bloqbusq(:,k:k+N-1);
NCC(k)= NCC_matching( bloqueref, otrobloqref);
end
[dy, dx]= find(NCC==max(NCC)); %find the distance, storing it
d=abs(dx-h+N); %like the disparity in that point
d1=d(1);
ddpp(j,i)=d1;
end
end

Answers (1)

Joss Knight
Joss Knight on 19 Mar 2014
gpuArray is not suited to serial computation on matrices of small size. Convert your images to gpuArrays at the start, and try to rewrite your algorithm so you compute your metrics at each level of disparity on every pixel at once, rather than one pixel at a time, perhaps taking advantage of the convolution function conv or the xcorr function from Signal Processing Toolbox.
If you have Computer Vision System Toolbox then their function disparity will do everything for you. If not, the Computer Vision literature should throw up a large number of simple algorithms for computing disparity maps via block matching (your approach) or more robust strategies.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!