/image_analysisII/Lab3/FuzzConn/adjacency.m

http://uppsala-university.googlecode.com/ · MATLAB · 35 lines · 19 code · 7 blank · 9 comment · 3 complexity · e27221c61e59c4685b86722fb044f2f4 MD5 · raw file

  1. function A=adjacency(I,n,k1)
  2. %function A=adjacency(I,n,k1)
  3. % Input: image (used for size only), n=L1 distance of neighbourhood, k1=distance decline factor
  4. %
  5. % Compute adjacency according to (2.8) in Udupa '96
  6. %
  7. %Author: Joakim Lindblad
  8. [r,c]=size(I);
  9. N=r*c;
  10. %abs delta of column and row coordinates
  11. dC=abs(repmat([1:c],c,1)-repmat([1:c].',1,c));
  12. dR=abs(repmat([1:r],r,1)-repmat([1:r].',1,r));
  13. A=sparse(N,N); %output matrix
  14. for dc=0:n %for each L1 distance level
  15. [bi,bj]=find(dC == dc); %r*r blocks with dc<=n
  16. for dr=0:n-dc
  17. [i,j]=find(dR == dr); %pixels within block with dr+dc<=n
  18. %The adjaceny
  19. a=1./(1+k1.*sqrt(dr.^2+dc.^2));
  20. %compute global indices and put things into A
  21. p=[];q=[];
  22. for k=1:length(bi)
  23. p=[p;(bi(k)-1)*r+i];
  24. q=[q;(bj(k)-1)*r+j];
  25. end
  26. A=A+sparse(p,q,repmat(a,size(q)),N,N);
  27. end
  28. end