This is slow (15 seconds on my machine) but it seems to return the expected location,
% Create a mask for selected pixels in ISubset
iMask = true(size(ISubset));
iMask(ISubset==255) = false;
[nRowFull, nColFull] = size(IFull);
[nRowSub, nColSub] = size(ISubset);
corrMat = zeros(size(IFull)); % Where correlation values will be stored
x = double(ISubset(iMask)); % First input to correlation is ISubset
for iRow = 1:nRowFull
iRowSlice = (1:nRowSub) + iRow-1;
if max(iRowSlice) <= nRowFull % If subset fits in IFull
for iCol = 1:nColFull
% Index to subset sized chunk
iColSlice = (1:nColSub) + iCol-1;
if max(iColSlice) <= nColFull % If subset fits in IFull
temp = IFull(iRowSlice, iColSlice); % Extract subset sized chunk from IFull
y = double(temp(iMask)); % Second input to correlation is IFull
R = corrcoef(x,y); % Get correlation coefficient
% Place results in upper right of subset sized chunk
iRowOffset = iRowSlice(1); % y-offset
iColOffset = iColSlice(1); % x-offset
corrMat(iRowOffset, iColOffset) = R(1,2); % Off diagonal value
end
end
end
end
% Offset of ISubset in full image, based on the max correlation
[yOffset, xOffset] = find(corrMat == max(corrMat(:)));
% Find x,y pixel locations of selected regions
xSelection = []; ySelection = []; % x,y pixel locations of selection
for iRow = 1:nRowSub
for iCol = 1:nColSub
if iMask(iRow,iCol) % True if pixel is in selection
ySelection = [ySelection iRow+yOffset-1];
xSelection = [xSelection iCol+xOffset-1];
end
end
end
% Display image and selected points
f3 = figure;
imshow(IFull); % Show full image
plot(xSelection, ySelection, 'y.'); % Selected points in IFull coordinates
plot([124 446 446 124 124], [164 164 325 325 164], 'r'); % Bounding box of selection, to confirm