Advertisement
Guest User

Untitled

a guest
May 20th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.59 KB | None | 0 0
  1. function [outputImg, meanColors] = quantize_RGB(origImg, k)
  2. imgDouble = im2double(origImg);
  3. [x,y,z] = size(imgDouble);
  4. shapedImg = reshape(imgDouble, x*y, 3);
  5. cluster_idx = kmeans(shapedImg, k);
  6. pixel_labels = reshape(cluster_idx, x, y);
  7. imshow(pixel_labels,[]), title('image labeled by cluster index');
  8. meanColors = 1;
  9. outputImg = pixel_labels;
  10.  
  11. segmented_images = cell(1,3);
  12. rgb_label = repmat(pixel_labels,[1 1 3]);
  13.  
  14. for j = 1:k
  15.     color = imgDouble;
  16.     color(rgb_label ~= j) = 0;
  17.     segmented_images{j} = color;
  18. end
  19.  
  20. outputImg = segmented_images;
  21. imshow(outputImg);
  22.  
  23. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement