如何通過Matlab以百分比計算圖像的每個RGB顏色通道的亮度? 以下Matlab代碼不能正常工作:如何通過Matlab以百分比計算圖像的每個RGB顏色通道的亮度?
I = imread('3.png'); % read image
Ir=I(:,:,1); % read red chanel
Ig=I(:,:,2); % read green chanel
Ib=I(:,:,3); % bule chanel
% figure, imshow(I), title('Original image')
% figure, imshow(Ir), title('Red channel')
% figure, imshow(Ig), title('Green channel')
% figure, imshow(Ib), title('Blue channel')
%% read the size of the
m = size(I,1);
n = size(I,2);
R_total= 0;
G_total= 0;
B_total= 0;
for i = 1:m
for j = 1:n
rVal= int64(Ir(i,j));
gVal= int64(Ig(i,j));
bVal= int64(Ib(i,j));
R_total= R_total+rVal;
G_total= G_total+gVal;
B_total= B_total+bVal;
end
end
disp (R_total)
disp (G_total)
disp (B_total)
%% Calcualte the image total intensity
I_total = R_total + G_total + B_total;
disp(I_total)
%% Calculate the percentage of each Channel
R_precentag = R_total/I_total * 100 ; %% Red Channel Precentage
G_precentag = G_total/I_total * 100 ; %% Green Channel Precentage
B_precentag = B_total/I_total * 100 ;
我無法看到每個通道R,G強度百分比B.
不知道如何解決這個問題?
添加了一個示例,感謝您的建議。 –
不要忘記在進行總和之前將其轉換爲double。 – gnovice
@gnovice爲什麼你會先轉換爲double? 'sum'沒有把整數作爲輸入的問題,默認情況下會返回一個'double'。 –