Calculate the mean value of border pixels in image and minus that from the whole image

1 回表示 (過去 30 日間)
Hello,
I am looking for what I am hoping is quite a simple solution.
I have an image that at the moment is 200 by 200 but has the ability to change size (lengh and width but at an equal rate).
What I need: To look at only the outside 5 pixels or so of the whole image ( so a 5 pixel wide band around the image) and calculate the mean pixel value.
I then need to subtract that value from each pixel in the image.
Any help is greatly appreciated,
Ollie.

採用された回答

Jan
Jan 2021 年 4 月 8 日
編集済み: Jan 2021 年 4 月 8 日
img = rand(30, 60, 3); % Assuming RGB images;
border = 5;
siz = size(img);
mask = false(siz(1), siz(2));
mask(1:border, :) = true;
mask(:, 1:border) = true;
mask(siz(1)-border+1:siz(1), :) = true;
mask(:, siz(2)-border+1:siz(2)) = true;
tmp = reshape(img, [], 3);
meanBorder = mean(tmp(mask, :), 1);
img = img - reshape(meanBorder, 1, 1, 3);
% Cross-check: do the border pixels have a zero mean value now:
tmp = reshape(img, [], 3);
meanBorder = mean(tmp(mask, :), 1)
meanBorder = 1×3
1.0e+-15 * 0.0130 0.1765 -0.0405
Fine. But now some pixels have negative values.
  3 件のコメント
Jan
Jan 2021 年 4 月 8 日
Maybe your input is not an RGB image, which are [M x N x 3] arrays.
If it is a grayscale image, which are [M x N] matrices, use:
siz = size(img);
mask = false(siz(1), siz(2));
mask(1:border, :) = true;
mask(:, 1:border) = true;
mask(siz(1)-border+1:siz(1), :) = true;
mask(:, siz(2)-border+1:siz(2)) = true;
meanBorder = mean(img(mask));
img = img - meanBorder;
Oliver Horrobin
Oliver Horrobin 2021 年 4 月 9 日
Hi Jan,
It must be a grayscale image. It works perfectly now.
Thankyou so much,
Ollie

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeImages についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by