Advertisement
Guest User

Untitled

a guest
May 20th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.67 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, meanColors] = kmeans(shapedImg, k);
  6. pixel_labels = reshape(cluster_idx, x, y);
  7. meanColors = uint8(meanColors*255);
  8.  
  9. outputImg = uint8(zeros(x,y,3));
  10.  
  11.  
  12. for i = 1:k
  13.     A = uint8(zeros(x,y,3));
  14.     A(:,:,1) = pixel_labels == i;
  15.     A(:,:,2) = pixel_labels == i;
  16.     A(:,:,3) = pixel_labels == i;
  17.     A(:,:,1) = A(:,:,1) * meanColors(i,1);
  18.     A(:,:,2) = A(:,:,2) * meanColors(i,2);
  19.     A(:,:,3) = A(:,:,3) * meanColors(i,3);
  20.     outputImg = outputImg + uint8(A);
  21.    
  22. end
  23. imshow(outputImg)
  24. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement