how to locate group of pixels in a large image
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
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
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?
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!