how to locate group of pixels in a large image
2 views (last 30 days)
Show older comments
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! :)
0 Comments
Answers (1)
neil jerome
on 28 Jul 2020
%% 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 Comments
Image Analyst
on 28 Jul 2020
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?
See Also
Categories
Find more on Motor Drives in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!