/Berkeley_Segmentation/segbench/Benchmark/boundaryPR.m

http://phd-workspace.googlecode.com/ · MATLAB · 72 lines · 50 code · 2 blank · 20 comment · 2 complexity · c72f3e80c8b29906ed032129494b704a MD5 · raw file

  1. function [thresh,cntR,sumR,cntP,sumP] = boundaryPR(pb,segs,nthresh)
  2. % function [thresh,cntR,sumR,cntP,sumP] = boundaryPR(pb,segs,nthresh)
  3. %
  4. % Calcualte precision/recall curve.
  5. % If pb is binary, then a single point is computed.
  6. % The pb image can be smaller than the segmentations.
  7. %
  8. % INPUT
  9. % pb Soft or hard boundary map.
  10. % segs Array of segmentations.
  11. % [nthresh] Number of points in PR curve.
  12. %
  13. % OUTPUT
  14. % thresh Vector of threshold values.
  15. % cntR,sumR Ratio gives recall.
  16. % cntP,sumP Ratio gives precision.
  17. %
  18. % See also boundaryPRfast.
  19. %
  20. % David Martin <dmartin@eecs.berkeley.edu>
  21. % January 2003
  22. if nargin<3, nthresh = 100; end
  23. if islogical(pb), nthresh = 1; end
  24. nthresh = max(1,nthresh);
  25. [height,width] = size(pb);
  26. nsegs = length(segs);
  27. thresh = linspace(1/(nthresh+1),1-1/(nthresh+1),nthresh)';
  28. % compute boundary maps from segs
  29. bmaps = cell(size(segs));
  30. for i = 1:nsegs,
  31. bmaps{i} = double(seg2bmap(segs{i},width,height));
  32. end
  33. % make sure the boundary maps are thinned to a standard thickness
  34. for i = 1:nsegs,
  35. bmaps{i} = bmaps{i} .* bwmorph(bmaps{i},'thin',inf);
  36. end
  37. % zero all counts
  38. cntR = zeros(size(thresh));
  39. sumR = zeros(size(thresh));
  40. cntP = zeros(size(thresh));
  41. sumP = zeros(size(thresh));
  42. if nthresh>1, progbar(0,nthresh); end
  43. for t = 1:nthresh,
  44. % threshold pb to get binary boundary map
  45. bmap = (pb>=thresh(t));
  46. % thin the thresholded pb to make sure boundaries are standard thickness
  47. bmap = double(bwmorph(bmap,'thin',inf));
  48. % accumulate machine matches, since the machine pixels are
  49. % allowed to match with any segmentation
  50. accP = zeros(size(pb));
  51. % compare to each seg in turn
  52. for i = 1:nsegs,
  53. %fwrite(2,'+');
  54. % compute the correspondence
  55. [match1,match2] = correspondPixels(bmap,bmaps{i});
  56. % accumulate machine matches
  57. accP = accP | match1;
  58. % compute recall
  59. sumR(t) = sumR(t) + sum(bmaps{i}(:));
  60. cntR(t) = cntR(t) + sum(match2(:)>0);
  61. end
  62. % compute precision
  63. sumP(t) = sumP(t) + sum(bmap(:));
  64. cntP(t) = cntP(t) + sum(accP(:));
  65. if nthresh>1, progbar(t,nthresh); end
  66. end