-1
我已經編寫了彩色圖像直方圖均衡的代碼。該代碼適用於灰度圖像,但會對彩色圖像引發錯誤。我將rgb圖像轉換爲hsv,然後執行直方圖均衡。對此有何想法?Matlab:索引必須是正整數或邏輯
代碼:
% GIm=imread('tire.tif'); this is grayscale image, which works fine
I=imread('D:\came.jpg');
GIm=rgb2hsv(I);% converting color image to hsv
numofpixels=size(GIm,1)*size(GIm,2);
figure,imshow(GIm);
title('Original Image');
HIm=uint8(zeros(size(GIm,1),size(GIm,2)));
freq=zeros(256,1);
probf=zeros(256,1);
probc=zeros(256,1);
cum=zeros(256,1);
output=zeros(256,1);
%freq counts the occurrence of each pixel value.
%The probability of each occurrence is calculated by probf.
for i=1:size(GIm,1)
for j=1:size(GIm,2)
value=GIm(i,j);
freq(value+1)=freq(value+1)+1; % giving error here
probf(value+1)=freq(value+1)/numofpixels;
end
end
sum=0;
no_bins=255;
%The cumulative distribution probability is calculated.
for i=1:size(probf)
sum=sum+freq(i);
cum(i)=sum;
probc(i)=cum(i)/numofpixels;
output(i)=round(probc(i)*no_bins);
end
for i=1:size(GIm,1)
for j=1:size(GIm,2)
HIm(i,j)=output(GIm(i,j)+1);
end
end
figure,imshow(HIm);
title('Histogram equalization');
subplot(2,2,2);
plot(GIm);
%bar(GIm);
title('Before Histogram equalization');
subplot(2,2,4);
plot(HIm)
%bar(HIm);
title('After Histogram equalization');
錯誤:
Attempted to access freq(1.61378); index must be a positive integer or logical.
Error in histogramequ (line 18)
freq(value+1)=freq(value+1)+1;
詹姆斯 - 仔細觀察HSV的圖像,GIm。在命令窗口中鍵入'class(GIm)'和'max(GIm(:))'。你可能會注意到,HSV數據類型(類)是** double **,最大值是1.所以所有的像素值都在[0,1]區間內,錯誤信息是有意義的。同時檢查RGB和HSV圖像的尺寸。對於灰度圖像可以有三個不同的尺寸。您的代碼可能需要更新才能處理。 – Geoff 2014-09-04 02:05:18
我不知道你將如何創建一個直方圖 - 可能是每個色相,飽和度和值分量(HSV圖像)將[0,1]區間分割成固定數量的區域。例如,[0,0.1],[0.1,0.2),...,[0.8,0.9),[0.9,1]可能是您嘗試「填充」的十個垃圾箱。雖然你可能需要更多的人(這只是一個例子)。 – Geoff 2014-09-04 02:12:36