Finding adjacent pixels of a certain color around a blue pixel in an image

7 ビュー (過去 30 日間)
MICHAEL DZENZEOL
MICHAEL DZENZEOL 2020 年 2 月 20 日
回答済み: Athul Prakash 2020 年 2 月 25 日
I am trying to write a function named countBlueWithAdjacentColor which receives the name of an image as input and returns the number of blue pixels that have an adjacent pixel (any direction) that is of the color specified by an 1-by-3 array named color which contains the red, green, and blue values that define the color in RGB (0-255) form.
Required function name and parameters: countBlueWithAdjacentColor(iName, color).
Sample function call: countBlueWithAdjacentColor('Image03.tif', [0,255,0])
Returned by function: [8]
.
  3 件のコメント
REID OLSON
REID OLSON 2020 年 2 月 23 日
yes.
Adam Danz
Adam Danz 2020 年 2 月 23 日
編集済み: Adam Danz 2020 年 2 月 23 日
Sounds like this might be an assignment. How far are you in solving this? If you need help starting, here's a hint.
An important question would be, what defines "blue"?

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

回答 (1 件)

Athul Prakash
Athul Prakash 2020 年 2 月 25 日
Hi Michael,
Here's one way to do it:-
  • Create 2 masks, one identifying all blue pixels and another identifying all pixels adjacent to your given color.
  • If we '&' both the masks, we get a mask of the required pixels.
  • Sum over this mask to get your count.
% I assume you have 'blue' defined as a RGB value.
blueColor = [0,0,255];
% 'myColor' is the RGB triplet of the color that should be adjacent to blue.
myColor = [10,20,30];
% Create 2D masks over the image, 1 mask for blue pixels and another for pixels of 'myColor'.
blueMask = mask(img, blueColor);
myColorMask = mask(img, myColor);
% We need a mask for every pixel that could be adjacent to myColor.
k1 = ones(3,3);
% If myColorMask is convolved with a kernel of 3x3 1's, every value adjacent to 'myColor' would have non-zero values.
adjMask = (conv2(myColorMask, k1) > 0);
% The '&' operation gives a mask of the reqd. pixels. Sum over that to get the count.
result = sum(adjMask & blueMask, 'all');
function out = mask(img, colorVal)
colorVal = reshape(colorVal, [1 1 3]);
out = (img==colorVal);
out = (out(:,:,1) & out(:,:,2) & out(:,:,3));
end
Hope it Helps!

カテゴリ

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

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by