Distance of rgb pixel from each pixel of image

3 views (last 30 days)
I am using impixel to select the similar pixel values of rgb image and find their mean. those mean rgb values are Ra,Ba,Ga. I want to find the eucledian distance of each rgb valued pixel from these mean values but i am unable to do so.
im = imread('coloredChips.png');
p=impixel(im)
R=p(:,1); %extracting red value in rgb matrix
G=p(:,2); %extracting green value in rgb matrix
B=p(:,3); %extracting blue value in rgb matrix
Ra=mean(R);
Ga=mean(G);
Ba=mean(B);
r1=im(:,:,1); %separating red, green,blue layer of image
g1=im(:,:,2);
b1=im(:,:,3);
rr=(r1-Ra).^2;
gg=(g1-Ga).^2;
bb=(b1-Ba).^2;
v=sqrt(rr+gg+bb);

Accepted Answer

Image Analyst
Image Analyst on 14 Jan 2023
impixel just gives you the value at a single point. So the mean there is just going to be the value there.
If you want the RGB as separate color channels use imsplit
rgbImage = imread('peppers.png');
imshow(rgbImage);
axis('on', 'image');
[R, G, B] = imsplit(rgbImage);
uiwait(helpdlg('Select a point'))
[x,y] = ginput(1) % Ask user for a point.
x = round(x); % Convert to integer
y = round(y);
hold on;
plot(x, y, 'r+', 'LineWidth', 2, 'MarkerSize', 200)
rPoint = double(R(y, x));
gPoint = double(G(y, x));
bPoint = double(B(y, x));
% Get mean of whole image
meanR = mean2(R)
meanG = mean2(G)
meanB = mean2(B)
rr=(rPoint-meanR).^2;
gg=(gPoint-meanG).^2;
bb=(bPoint-meanB).^2;
v=sqrt(rr+gg+bb)
Also see my attached "magic wand" demo to get all colors within a certain delta E (color distance) of a region.
  7 Comments
DGM
DGM on 14 Jan 2023
That's the distance between all pixels and the image mean, not the mean of a set of selected pixels.

Sign in to comment.

More Answers (1)

DGM
DGM on 14 Jan 2023
Edited: DGM on 14 Jan 2023
For RGB distance between a single pixel and the image mean:
% a small bit of a test image
inpict = imread('peppers.png');
inpict = inpict(1:10,1:10,:); % just pick a sample
% some points
x = 5;
y = 5;
meancolor = mean(inpict,1:2);
mypixel = double(inpict(y,x,:));
% distance between one point and mean color
Dpointmn = sqrt(sum((mypixel - meancolor).^2,3))
Dpointmn = 2.7740
For distance between all pixels and the image mean (see Dallmn(5,5)):
% distance between all points and mean color
Dallmn = sqrt(sum((double(inpict) - meancolor).^2,3))
Dallmn = 10×10
3.0356 1.8695 3.5937 2.8204 5.6511 0.5430 2.5090 1.5984 3.3697 4.2302 0.5430 3.0814 4.9634 2.4403 4.3675 3.7007 2.5090 1.1979 0.9460 2.2572 3.2886 1.0747 0.9460 2.9249 2.7740 2.3441 3.7088 2.6258 1.6837 2.1529 2.0037 4.0761 3.5909 2.2572 0.5430 1.8533 2.1389 4.5050 4.0786 4.9371 0.5430 2.0087 2.1389 2.1342 2.7740 2.4970 3.3727 2.7740 4.4827 3.0814 1.6837 6.6959 4.9573 3.4110 3.5348 4.1394 3.8672 1.5149 1.8533 3.3519 1.9378 2.9249 2.6636 3.6925 1.3019 3.5909 5.2759 2.5090 3.3429 5.5168 3.7168 3.8490 4.7429 2.8204 2.4525 3.2611 2.0087 1.5984 2.0087 2.5209 4.1899 1.3247 1.5984 1.6837 2.2748 2.7884 2.1529 2.9249 2.0087 4.1346 3.3786 1.7014 1.7014 3.8334 5.4913 2.5719 1.9378 1.8695 2.7740 2.1529
For the distance between all pixels and a single pixel:
% distance between all points and single color
Dall = sqrt(sum((double(inpict) - mypixel).^2,3))
Dall = 10×10
5.3852 4.2426 5.1962 1.7321 4.4721 3.1623 5.0990 3.1623 3.7417 3.6056 3.1623 5.8310 7.6811 2.4495 4.6904 6.0000 5.0990 2.4495 2.8284 3.1623 2.4495 3.3166 3.6056 1.0000 0 5.0000 6.4031 2.0000 2.2361 3.7417 3.7417 3.0000 2.2361 3.1623 2.2361 4.2426 3.7417 2.2361 3.3166 4.5826 3.1623 4.5826 3.0000 1.4142 0 3.3166 2.4495 0 4.1231 3.0000 3.1623 9.0554 7.3485 3.0000 2.2361 4.4721 4.1231 4.1231 4.2426 4.2426 4.5826 5.0990 4.1231 2.4495 2.0000 6.3246 7.8740 5.0990 2.8284 6.0828 4.5826 2.2361 2.2361 1.7321 2.4495 3.1623 2.4495 3.1623 4.5826 5.0990 6.4807 3.0000 3.1623 3.1623 3.4641 4.1231 4.3589 5.0990 4.5826 6.0828 5.3852 4.4721 4.4721 5.3852 6.0828 3.0000 2.4495 4.2426 5.0000 4.3589
For the distance between all pixels and the mean color local to a specific point:
% get a 3x3 local sample
r = 1; % sample radius
sx = min(max([x-r x+r],1),size(inpict,2));
sy = min(max([y-r y+r],1),size(inpict,1));
sample = inpict(sy(1):sy(2),sx(1):sx(2),:);
% find the local mean color
sampmean = mean(sample,1:2);
% distance between all points and local mean color
Dallmn = sqrt(sum((double(inpict) - sampmean).^2,3))
Dallmn = 10×10
4.3617 3.5935 4.8215 1.3833 4.5488 1.7708 3.3370 1.3426 2.0062 2.5652 1.7708 4.5731 6.1934 0.9558 3.3370 4.0307 3.3370 1.8922 2.4545 1.1653 2.1140 1.9213 2.6504 2.1915 2.0608 3.9892 4.7519 3.1662 1.3005 3.6549 2.4994 4.2586 3.8762 1.1653 1.4614 2.7532 2.8328 4.2586 4.7284 5.8044 1.7708 3.3866 2.6504 1.3426 2.0608 1.6741 2.4994 2.0608 5.3668 3.9892 1.6405 7.1198 5.6590 1.9783 1.8626 3.8038 4.9354 2.4771 2.7532 4.7167 3.2527 3.1309 2.0876 2.2167 0.4969 4.6930 6.8656 3.3370 3.8618 6.8575 4.8902 2.7330 3.3866 1.3833 2.4994 4.1663 2.9102 1.3426 3.3866 4.0582 5.8521 2.5652 1.3426 1.6405 3.3036 4.0991 3.7892 3.1309 3.3866 4.0445 3.5154 3.1662 3.1662 5.4490 6.7923 3.1837 2.7532 3.5935 3.0409 3.7892
To find the distance between all pixels and the mean color of a set of pixels defined by scattered points:
% some points (row vectors)
x = [4 5 6];
y = [5 5 5];
% get samples from selected locations
sz = size(inpict);
idx = sub2ind(sz(1:2),y,x) + [0;1;2]*prod(sz(1:2));
sample = permute(inpict(idx),[3 2 1]); % this is 1xNx3
% find mean color of selection
sampmean = mean(sample,1:2);
% distance between all points and mean color of selected pixels
Dallmn = sqrt(sum((double(inpict) - sampmean).^2,3))
Dallmn = 10×10
4.7140 3.7268 4.7842 1.2472 4.5338 2.1344 3.9016 1.9720 2.5604 2.7487 2.1344 4.9554 6.6500 1.3744 3.8152 4.6786 3.9016 1.7951 2.4267 1.7951 2.1344 2.3570 2.8674 1.6997 1.3744 4.2687 5.3125 2.6874 1.2472 3.5434 2.9250 3.8586 3.2998 1.7951 1.4907 3.1447 2.9250 3.5901 4.1899 5.3125 2.1344 3.6818 2.4944 1.1055 1.3744 2.0548 2.1344 1.3744 4.9216 3.5901 1.9720 7.7388 6.1554 2.0548 1.6997 4.0689 4.6428 2.9814 3.1447 4.4597 3.6818 3.8152 2.7487 1.9720 0.7454 5.2175 7.1570 3.9016 3.3500 6.5490 4.7842 2.4944 2.8674 1.2472 2.4267 3.7268 2.5604 1.9720 3.6818 4.3843 6.0461 2.6247 1.9720 1.9720 3.1447 3.9441 3.8586 3.8152 3.6818 4.7140 4.1899 3.5434 3.5434 5.3748 6.5490 3.0912 2.5604 3.7268 3.6818 3.8586
Note that these examples will work regardless of how many channels the image has. No need to split or handle RGB images differently than gray images.
At least for 1976 Delta E, this is the same idea that you can apply in LAB (if you don't have deltaE)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!