Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. %--------------------------------------------------------------------------
  2. % Created: 04/24/2014 Simon Nguyen
  3. %
  4. % Revision History:
  5. %
  6. % Purpose: This function takes an input image and integer k that produces k
  7. % regions on the image.
  8. %
  9. % Variables:
  10. % Inputs:
  11. % im = input image
  12. % k = integer value for number of regions
  13. % Outputs:
  14. % img = output image with boundaries drawn
  15. %
  16. % function img = mykmeans(im, k)
  17. %--------------------------------------------------------------------------
  18.  
  19. function img = mykmeans(im, k)
  20. % Choose k random points from within the image
  21. [r, c, ~] = size(im);
  22. pts = [randi(r, 1, k); randi(c, 1, k)];
  23.  
  24. % Find the colors at the chosen points
  25. m = zeros(size(pts)); m = [m; m(1,:)];
  26. for kk = 1 : k
  27. pt = pts(:,kk);
  28. m(:,kk) = [im(pt(1), pt(2), 1);
  29. im(pt(1), pt(2), 2);
  30. im(pt(1), pt(2), 3)];
  31. end
  32.  
  33. % Create a temporary image containing the clusters.
  34. kcluster = uint8(zeros(r,c));
  35.  
  36. % Limit iterations to 100
  37. ic = 1;
  38. while ic < 100
  39.  
  40. % After each iteration, values within the regions are averaged and
  41. % chosen as the next set of m-values
  42. if ic > 1
  43. prevkm = m;
  44.  
  45. for kk = 1 : k
  46. curkc = uint8(kcluster == kk);
  47. curr = curkc.*im(:,:,1);
  48. curg = curkc.*im(:,:,2);
  49. curb = curkc.*im(:,:,3);
  50.  
  51. nvals = sum(kcluster(:) == kk);
  52. m(:,kk) = [sum(curr(:))/nvals;
  53. sum(curg(:))/nvals;
  54. sum(curb(:))/nvals];
  55. end
  56. end
  57.  
  58. % Iterate through each pixel of the image and find its color distance
  59. % with respect to each of the m-values, assigning the pixel to the
  60. % corresponding group.
  61. for cc = 1 : c
  62. for rr = 1 : r
  63. codiff = zeros(1, length(m(1,:)));
  64.  
  65. for mm = 1 : length(m(1,:))
  66. km = m(:,mm);
  67. curPixel = double([im(rr, cc, 1);
  68. im(rr, cc, 2);
  69. im(rr, cc, 3)]);
  70.  
  71. codiff(:,mm) = sqrt( (km(1) - curPixel(1))^2 + ...
  72. (km(2) - curPixel(2))^2 + ...
  73. (km(3) - curPixel(3))^2);
  74. end
  75.  
  76. codmin = find(codiff == min(codiff), 1);
  77. kcluster(rr, cc) = codmin;
  78.  
  79. end
  80. end
  81.  
  82. % After each iteration, check if the difference between the previous
  83. % clustering is similar enough to the current iteration.
  84. if ic > 1
  85. kmDiff = abs((prevkm-m)./m);
  86.  
  87. if sqrt(kmDiff(1,:).^2 + kmDiff(2,:).^2 + kmDiff(3,:).^2) < .05
  88. break;
  89. end
  90. end
  91.  
  92. ic = ic + 1;
  93.  
  94. end
  95.  
  96. % Create the boundaries for each region
  97. kcvals = unique(kcluster);
  98. newim = im;
  99. tempim = label2rgb(kcluster);
  100.  
  101. for kk = 1 : length(kcvals)
  102.  
  103. curkc = kcluster == kk;
  104. nkc = (curkc - imerode(curkc, strel('square', 2)));
  105. [r, c] = find(nkc == 1);
  106.  
  107. for rr = 1 : length(r)
  108. aa = r(rr);
  109. bb = c(rr);
  110. newim(aa, bb, 1) = tempim(aa, bb, 1);
  111. newim(aa, bb, 2) = tempim(aa, bb, 2);
  112. newim(aa, bb, 3) = tempim(aa, bb, 3);
  113. end
  114. end
  115.  
  116. img = newim;
  117. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement