2015-01-25 7 views
0

000에서 111까지의 RGB 방법을 사용하여 이미지에서 최대 8 가지 색상을 계산할 수있는 프로그래밍을 완료했습니다. 일부 수정이 필요합니다. 지금까지 나는 128보다 큰 숫자가 128보다 1이면 &이 0이 될 것이라고 선언합니다. 이것은 8 가지 색상으로 계산됩니다. 0-64,65-128,129-192,193-255에서 부분적으로 말하면 색상 수를 늘리는 방법. 간격의 수를 증가8 대신에 색상 수가 증가합니다.

count=zeros(1,8); 
for i = 1:w 
    for j = 1:h 

     if redChannel(i,j) > 128, 
      aredChannel2 = 1; 
     else 
      aredChannel2=0; 
     end 
     quantizedImage(i,j,1)=aredChannel2*255; 

     if greenChannel(i,j) > 128, 
      agreenChannel2 = 1; 
     else 
      agreenChannel2=0; 
     end 
     quantizedImage(i,j,2)=agreenChannel2*255; 

     if blueChannel(i,j) > 128, 
      ablueChannel2 = 1; 
     else 
      ablueChannel2=0; 
     end 
     quantizedImage(i,j,3)=ablueChannel2*255; 

     bin=4*aredChannel2+2*agreenChannel2+ablueChannel2+1; 

     count(bin)=count(bin)+1; 

    end 
end 

figure, imshow(uint8(quantizedImage)) 

답변

1

당신이 계산하는 기반을 증가 : 대신 2^3=8 양자화 된 색상이 4^3=64 양자화 된 색상이있을 것이다.

rgb = imread(...); %// read you image here 
qImage = zeros(size(rgb(:,:,1))); %// preallocate result 
intervals = permute([64, 128, 192, 256], [1 3 2]); %// the quantization intervals 
base = numel(intervals); 
for ci=1:size(rgb,3) %// for each channel 
    whichInterval = bsxfun(@le, rgb(:,:,ci), intervals); %// select per pixel, which interval is relevant 
    [~, q] = max(whichInterval, [], 3); %// get index of relevant interval 
    qImage = qImage*base + (q-1); %// -1 to convert from matlab's 1-based indexing 
end 
+0

편집을위한 @rayryeng 편집 – Shai

+0

문제 없음 :) 실제로 몇 시간 후까지 예고하지 않았습니다! 나는 단지'permute'와'bsxfun'으로 당신의 우아한 솔루션을 바라 보던 중이었습니다. 아주 잘 했어. – rayryeng

+0

@rayryeng 이봐, 이해할 수 없었다. 좀 더 구체적으로하기 위해 if else를 사용할 수 있습니까? 그리고 카운트는 어때? bin = 4 * aredChannel2 + 2 * agreenChannel2 + ablueChannel2 + 1; 카운트 (빈) = 카운트 (빈) +1; –

관련 문제