フィルターのクリア

How to check 4 neighbor connectivity in grayscale image!!

1 回表示 (過去 30 日間)
Zulqurnain Jutt
Zulqurnain Jutt 2016 年 3 月 20 日
コメント済み: DGM 2023 年 3 月 19 日
matrix =
147 155 139 104 84 139
136 134 99 73 60 144
98 82 60 54 47 118
86 59 46 48 38 90
88 66 50 44 35 67
88 75 53 40 43 48
p = matrix(3,3)
q = matrix(2,5)
V = {1:60}

採用された回答

Image Analyst
Image Analyst 2016 年 3 月 20 日
What's your definition of connected? There is a virtually infinite number of paths between the 3,3 pixel and the 2,5 pixel.
  2 件のコメント
Zulqurnain Jutt
Zulqurnain Jutt 2016 年 3 月 20 日
all i want is to check if there exist a path between pixel p and q using vector V, for example given matrix above there exist a paths: path1: (3,3) , (3,4) , (3,5) , (2,5) path2: (3,3) , (4,3) , (4,4) , (4,5) , (3,5) , (2,5) and many other paths, Note: pixel value in each path's coordinate should be in 'V' and obviously p and q should also be in 'V'. that's what i am trying to achieve
Image Analyst
Image Analyst 2016 年 3 月 20 日
Then make a binary image from V and label it. If the two points have the same label, they're connected.
% Construct a binary image the same size as matrix.
binaryImage = false(size(matrix));
% Extract the 60 element array from the V cell.
Vcontents = V{1};
for k = 1 : length(Vcontents)
% Find all elements that have value Vcontents(k)
mask = Vcontents(k) == matrix;
% Set those elements of the binary image to true.
binaryImage = binaryImage | mask;
end
% Now we have the binary image and we can label it
labeledImage = bwlabel(binaryImage);
% Now see if point p has the same value as point q.
% If they do, there is a path connecting them.
% If they don't, there is no path connecting them.
if labeledImage(3,5) == labeledImage(2,5)
% They're connected.
% They are in the same blob, so there is a path.
else
% They are not connected.
% They are in different blobs.
end

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

その他の回答 (1 件)

Ahmad
Ahmad 2023 年 3 月 19 日
clc;
clear all;
matrix1 = [147 155 139 104 84 139; 136 134 99 73 60 144; 98 82 60 54 47 118; 86 59 46 48 38 90; 88 66 50 44 35 67; 88 75 53 40 43 48];
V = {1:50};
binary_matrix = [147 155 139 104 84 139; 136 134 99 73 60 144; 98 82 60 54 47 118; 86 59 46 48 38 90; 88 66 50 44 35 67; 88 75 53 40 43 48];
binary_matrix(binary_matrix > 60) = 0;
binary_matrix(binary_matrix <= 50 & binary_matrix >= 1) = 1;
L = bwlabel(binary_matrix,4);
[r, c] = find(L==1);
rc = [r c];
imshow(L);
  1 件のコメント
DGM
DGM 2023 年 3 月 19 日
It's good to post answers, but try to post answers which:
  • address the specific needs of the OP or the general needs of future readers
  • produce correct results for either of those two objectives
  • are documented well enough to communicate your methods and intent.
  • are formatted
To improve:
% the given array (assumed to be integer-valued)
matrix = [147 155 139 104 84 139;
136 134 99 73 60 144;
98 82 60 54 47 118;
86 59 46 48 38 90;
88 66 50 44 35 67;
88 75 53 40 43 48];
% don't need a cell array (op's mistake)
% 60, not 50
V = 1:60;
% test point coordinates
p = [3 3]; % assumed to be [y x]
q = [2 5];
% create the logical image without overwriting the thing you're testing
% use the specific values of V instead of literals
% as it's not clear that V always a fully-populated linear series
% i'm treating it as an arbitrary set of discrete values
mask = ismember(matrix,V);
% label the image
L = bwlabel(mask,4);
% check to see if points p,q belong to the same blob
areconnected = L(p(1),p(2)) == L(q(1),q(2))
areconnected = logical
1
% show the label array and mark the points
imshow(L,[]); hold on
plot([p(2) q(2)],[p(1) q(1)],'x','linewidth',3,'markersize',10)
% i don't know why this was included
[r, c] = find(L==1);
rc = [r c];
There is only one blob. If p and q are both members of the foreground, they are connected.

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

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by