Calculating Similarity between a single pixel and a surrounding region

5 views (last 30 days)
I want to calculate the color similarity between a singular pixel with some RGB value and an RGB region surrounding it. I was wondering if the best way to accurately do this is within the L*a*b colorspace or can I just use the RGB inputs into a function like deltaE() and reliably generate individual similarity scores?

Accepted Answer

Image Analyst
Image Analyst on 21 Dec 2023
Edited: Image Analyst on 21 Dec 2023
I'd use delta e. I have a demo i can post tomorrow. what is the region? The 8 surrounding pixels?

More Answers (1)

DGM
DGM on 21 Dec 2023
Edited: DGM on 21 Dec 2023
Depends what you mean by 'similarity'. If you just want the mean DE between a pixel and its neighbors?
% read the image
inpict = imread('peppers.png');
% parameters
windowsize = [7 7]; % [y x] (odd)
% pad the image to mitigate edge effects
padsize = floor(windowsize/2);
paddedimage = padarray(inpict,padsize,'replicate','both');
paddedimage = im2double(paddedimage);
% convert to LAB
paddedimage = rgb2lab(paddedimage);
% prepare
s0 = size(inpict);
outpict = zeros(s0(1:2),class(paddedimage));
os = windowsize-1;
% relevant indices within the window
% precalculate these to save a bit of time
cenidx = false(windowsize);
cenidx(ceil(prod(windowsize)/2)) = true;
cenidx = repmat(cenidx,[1 1 3]);
nhoodidx = ~cenidx;
% process the image
for m = 1:s0(1)
for n = 1:s0(2)
sample = paddedimage(m:(m+os(1)),n:(n+os(2)),:); % extract this window
centerpixel = reshape(sample(cenidx),[],3);
neighborhood = reshape(sample(nhoodidx),[],3);
DE = sqrt(sum((centerpixel - neighborhood).^2,2)); % plain DE76
outpict(m,n) = mean(DE); % or some other measure of group DE?
end
end
% normalize if desired
% this makes it simple to save images
% normalize to data extrema
% scale will not be comparable between images
outpict = mat2gray(outpict);
% normalize to the approx max distance across the projection of sRGB into LAB
% (or some other presumed maximal output value)
% scale will be fixed, so different results can be compared
% but most results will appear dim, especially for small window sizes
%outpict = outpict/259;
% show the result
imshow(outpict)
It's not really plausible that you'd run into many cases where the peak output actually reaches 259, since this is a local averaging process. You'd need to find a single pixel of red surrounded by a field of blue or vice-versa. That would probably only occur in a synthetic image, and I'm not sure whether that's a likely target for this sort of analysis. I'm not sure what a more pragmatic upper value would be. I suppose it depends what you intend to do with the images or whether DeltaE is even the appropriate thing to be using.
  2 Comments
Image Analyst
Image Analyst on 23 Dec 2023
It really depends on what your needs are. Delta E is generally regarded as the best metric of color difference, though other ones are under development (CIECAM02, etc.). Delta E is stated to be only valid for delta Es below about 5 or so, though I've used it for differences much greater than that. At some point it becomes meaningless, like can you say that this green is closer to red than that yellow is to blue? But in context it can be useful for larger values. However it was developed for color matching specimens (like fabrics, plastics, paint, etc.)
That said, it really depends on what two colors you're comparing (and that is often determined by the surrounding background of the two color patches). So in your question you wanted to compare the color difference between a pixel and its 8 neighbors. Where color is uniform, it will be close to zero, but where there is a color "edge" then the average color will have a lot of variation in it. That's why the delta E image above looks like an edge detection image. Whether that is meaningful or helpful to you, I don't know because we don't know what you plan on doing with the image.
For further reference see

Sign in to comment.

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!