Skip to content

Instantly share code, notes, and snippets.

@zabela
Last active March 11, 2024 13:47
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save zabela/8539116 to your computer and use it in GitHub Desktop.
Save zabela/8539116 to your computer and use it in GitHub Desktop.
Algorithm to measure images colourfulness, adapted from "Measuring colourfulness in natural images" (Hasler and Susstrunk, 2003) http://infoscience.epfl.ch/record/33994/files/HaslerS03.pdf
function C = getColourfulness( im )
%
% C = getColourfulness( im )
%
% MATLAB algorithm implementation of the
% "Measuring colourfulness in natural images"
% (Hasler and Susstrunk, 2003)
%
% Input:
% im - image in RGB
%
% Output:
% C - colourfulness
%
i = 1;
[W, H, RGB] = size(im);
for x=1:W
for y=1:H
% rg = R - G
rg(i) = abs(double(im(x,y,1)) - double(im(x,y,2)));
% yb = 1/2(R + G) - B
yb(i) = abs(.5 * (double(im(x,y,1)) + double(im(x,y,2))) - double(im(x,y,3)));
i = i+1;
end
end
% standard deviation and the mean value of the pixel
% cloud along direction, respectively
stdRG = std(rg);
meanRG = mean(rg);
stdYB = std(yb);
meanYB = mean(yb);
stdRGYB = sqrt((stdRG)^2 + (stdYB)^2);
meanRGYB = sqrt((meanRG)^2 + (meanYB)^2);
C = stdRGYB + 0.3*meanRGYB;
end
@MohitLamba94
Copy link

The two for loops

for x=1:W
        for y=1:H

are redundant and takes much time as can be done in a single step, as shown in https://gist.github.com/MohitLamba94/7d40393f2fc1a478c84a6c5a4faaafdb. The code presented here and in the link given have exactly the same output but does not iterate over each pixel in two for loops by taking advantage of code vectorization.

Anyways thanks for the code.

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