Skip to content

Instantly share code, notes, and snippets.

@metadelete
Created December 6, 2014 14:21
Show Gist options
  • Save metadelete/fa866bc14524caa103ca to your computer and use it in GitHub Desktop.
Save metadelete/fa866bc14524caa103ca to your computer and use it in GitHub Desktop.
This is implementation of popularity algorithm which is inroduced in Image Processing Lecture
function []=popularity_algorithm()
x=imread('lena512.bmp');
indirge=input('resmi indirgemek istediğiniz renk sayısını seçiniz:');
[height width]=size(x);
figure(1);imshow(x);
%Bu algoritmaya göre histogram bilgilerine sahip olunan bir resmin tüm renkleri resimdeki toplam
%sayısına göre büyükten küçüğe doğru sıralanmalıdır.
%Daha sonra en popüler olan renklerden quantize edilmek istenen renk sayısı kadarı seçilir.
renk_sayisi=zeros(256,2);
for i=1:256
renk_sayisi(i,1)=i;%renk_sayisi(1:256,1)
end
for j=1:height
for i=1:width
renk_sayisi(x(i,j)+1,2)=renk_sayisi(x(i,j)+1,2)+1;%renk sayısı iki boyulu (renk,renk_sayisi) şeklinde
end
end
sirala=sortrows(renk_sayisi,2);
%sortrows büyüyen sıraya göre sıralamaktadır o halde sondan [indirge] kadar renkler bizim popüler renklerimizdir
sayac=1;tot_renk=256;
populer_renkler=zeros(indirge);
while(sayac<=indirge)
populer_renkler(sayac)=sirala(257-sayac);
sayac=sayac+1;
end
Y=degistir(x,height,width,populer_renkler);
figure(2);imshow(Y);
%popüler renkler tespit edildi. bu işlemin hemen ardından bu seçilen yeni renkler tüm piksellere uygulanır
%tabi piksel üzerindeki renkler bu seçilen en popüler renklere en yakın olanı seçilerek düzenlenmelidir
endfunction
function [Y]=degistir(x,height,width,populer_renkler)
for i=1:height
for j=1:width
kim=mesafe(populer_renkler,x(i,j));
x(i,j)=populer_renkler(kim);
end
end
Y=x;
endfunction
function [kim]=mesafe(populer_renkler,EX)%kim değeri değiştirilecek x(i,j) değerinin hangi popüler renk olacağıdır.
width=size(populer_renkler);
enk_mesafe=9999;
for i=1:width
if abs(populer_renkler(i)-EX)<=enk_mesafe
enk_mesafe=abs(populer_renkler(i)-EX);
kim=i;
endif
end
endfunction
@metadelete
Copy link
Author

Use Octave instead of matlab in order to run perfect on your computer.

@metadelete
Copy link
Author

lena512.bmp must be a grey level image and placed in same directory where your working terminal is currently running Octave program.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment