how to locate group of pixels in a large image

2 ビュー (過去 30 日間)
bidlee devi
bidlee devi 2020 年 4 月 26 日
コメント済み: neil jerome 2020 年 7 月 28 日
suppose i have a group of pixels arranged as
28 29 30 31
30 31 32 30
34 34 33 32
34 34 35 34
which has been cropped from a larger image.
Is there away to locate and find the exact group of pixels given above in the larger image?
I have cropped this particular set of pixels from a large image few months ago. My system got crashed and I dont remember the locations of these set of data.
Thanks! :)

回答 (1 件)

neil jerome
neil jerome 2020 年 7 月 28 日
%% find known submatrix in large matrix
% will find all instances
sourceMat = magic(15); % example source matrix
targMat = [218 10 27; 9 26 43]; % example target submatrix
[s1, s2] = size(targMat); % size of target
searchGrid = zeros(size(sourceMat,1)-s1, size(sourceMat,2)-s2); % grid of possible locations
for ii = 1:size(sourceMat,1)-s1 % loop over first dimension
for jj = 1:size(sourceMat,2)-s2 % loop over second dimension
subMat = sourceMat(ii:ii+s1-1,jj:jj+s2-1); % look at this region in source
diff = sum(squash(subMat - targMat)); % compare target to this region
if diff == 0 % is this region the same as target?
searchGrid(ii,jj) = 1; % record success!
end
end
end
[rows, cols] = find(searchGrid); % coordinate pairs of top corner of matching areas
horzcat(rows, cols)
  2 件のコメント
Image Analyst
Image Analyst 2020 年 7 月 28 日
You can use isequal:
% Not needed : diff = sum(squash(subMat - targMat)); % compare target to this region
% Compare current window with template using isequal().
if isequal(subMat, targMat) % is this region the same as target?
neil jerome
neil jerome 2020 年 7 月 28 日
yep :)

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

カテゴリ

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