05-02 06:32
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[Image Processing] 히스토그램 평활화 본문

Scientific Computing/Image Processing

[Image Processing] 히스토그램 평활화

cinema4dr12 2014. 5. 31. 12:49

[Step 1]

명암 값 j의 빈도 수 hist[j]를 계산하여 입력 영상의 히스토그램을 작성한다.


[Step 2]

각 명암 값 i에서 0~i까지의 누적 빈도 수(누적합)를 계산한다.


[Step 3]

2단계에서 구한 누적 빈도 수를 정규화한다(정규화 누적합).

n[i] = sum[i] * Imax / N

N: 화소의 총수

Imax: 최대 명도 값


[MATLAB CODE]

function Hist %% read an image img = imread('.\res\vectorc.jpg'); img = rgb2gray(img); [nrow, ncol] = size(img); subplot(1,2,1); imshow(img); %% vectorize img to tmp tmp = []; for(i = 1:nrow) tmp = [tmp img(i,:)]; end tmp = cast(tmp,'double'); %% histogram histo = zeros(256,1); MIN = 0; MAX = 0; for(i = 1:length(tmp)) histo(tmp(i)+1) = histo(tmp(i)+1) + 1; if(MIN > tmp(i)) MIN = tmp(i); end if(MAX < tmp(i)) MAX = tmp(i); end end %% accumulation of frequency dummy = 0; freq = zeros(length(histo),1); for(i = 1:length(histo)) dummy = dummy + histo(i); freq(i) = dummy; end %% normalization normal = zeros(length(freq),1); Imax = 255; N = nrow * ncol; for(i = 1:length(freq)) val = freq(i) * Imax / N; normal(i) = roundoff(val); end %% mapping newImg = tmp; for(i = 1:length(tmp)) newImg(i) = normal(tmp(i)+1); end %% separation for(i = i:nrow) img(i,:) = newImg((i-1)*ncol+1:i*ncol); end img = cast(img,'uint8'); subplot(1,2,2); imshow(img); end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function rndoff = roundoff(num) int = cast(num,'uint8'); deci = num - cast(cast(num,'uint8'),'double'); if(deci < 0.5) rndoff = cast(int,'double'); else rndoff = cast(int+1,'double'); end end

[RESULT]


Comments