Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/badges/code_issues.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
120 changes: 0 additions & 120 deletions code/+nansen/+tools/+image/+resize/imsplit.m

This file was deleted.

120 changes: 0 additions & 120 deletions code/algorithms/+stack/+reshape/imsplit.m

This file was deleted.

41 changes: 41 additions & 0 deletions code/algorithms/+stack/+reshape/mergeImageBlocks.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
function IM = mergeImageBlocks(blocks, outputSize, overlap)
%MERGEIMAGEBLOCKS Reassemble a cell grid of overlapping 2-D blocks.
%
% IM = stack.reshape.mergeImageBlocks(blocks, outputSize) merges the
% (numRows x numCols) cell array of overlapping 2-D image blocks
% produced by stack.reshape.splitImage into a single outputSize image,
% trimming the 1-pixel overlap on shared edges back to a clean tiling.
%
% IM = stack.reshape.mergeImageBlocks(blocks, outputSize, overlap)
% uses a custom overlap (in pixels; default 1). Must match the value
% used when the blocks were created.
%
% Syntax:
% IM = stack.reshape.mergeImageBlocks(blocks, outputSize)
% IM = stack.reshape.mergeImageBlocks(blocks, outputSize, overlap)

arguments
blocks cell
outputSize (1,2) double {mustBePositive, mustBeInteger}
overlap (1,1) double {mustBeNonnegative, mustBeInteger} = 1
end

[numRows, numCols] = size(blocks);
h = outputSize(1);
w = outputSize(2);
rowEdges = floor(linspace(0, h, numRows + 1));
colEdges = floor(linspace(0, w, numCols + 1));

IM = zeros(h, w, 'like', blocks{1,1});
for i = 1:numRows
rowDst = (rowEdges(i)+1):rowEdges(i+1);
srcR0 = rowEdges(i) + 1 - max(rowEdges(i) + 1 - overlap, 1) + 1;
for j = 1:numCols
colDst = (colEdges(j)+1):colEdges(j+1);
srcC0 = colEdges(j) + 1 - max(colEdges(j) + 1 - overlap, 1) + 1;
IM(rowDst, colDst) = blocks{i,j}( ...
srcR0 : srcR0 + numel(rowDst) - 1, ...
srcC0 : srcC0 + numel(colDst) - 1);
end
end
end
42 changes: 42 additions & 0 deletions code/algorithms/+stack/+reshape/splitImage.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
function blocks = splitImage(IM, numRows, numCols, overlap)
%SPLITIMAGE Split an H-by-W(-by-T) array into a (numRows x numCols) cell grid.
%
% blocks = stack.reshape.splitImage(IM, numRows, numCols) splits the
% first two dimensions of IM into a numRows-by-numCols cell array of
% sub-arrays. Each block extends one pixel into its neighbours on each
% side (clamped at the image borders) so that per-pixel computations
% needing a 1-pixel neighbourhood can be performed block-wise. The third
% dimension (e.g. time) is preserved in full inside each block.
%
% blocks = stack.reshape.splitImage(IM, numRows, numCols, overlap) uses
% a custom overlap (in pixels; default 1).
%
% Use stack.reshape.mergeImageBlocks to reassemble the blocks; the
% overlap region is trimmed during reassembly.
%
% Syntax:
% blocks = stack.reshape.splitImage(IM, numRows, numCols)
% blocks = stack.reshape.splitImage(IM, numRows, numCols, overlap)

arguments
IM
numRows (1,1) double {mustBePositive, mustBeInteger}
numCols (1,1) double {mustBePositive, mustBeInteger}
overlap (1,1) double {mustBeNonnegative, mustBeInteger} = 1
end

[h, w, ~] = size(IM);
rowEdges = floor(linspace(0, h, numRows + 1));
colEdges = floor(linspace(0, w, numCols + 1));

blocks = cell(numRows, numCols);
for i = 1:numRows
r0 = max(rowEdges(i) + 1 - overlap, 1);
r1 = min(rowEdges(i+1) + overlap, h);
for j = 1:numCols
c0 = max(colEdges(j) + 1 - overlap, 1);
c1 = min(colEdges(j+1) + overlap, w);
blocks{i,j} = IM(r0:r1, c0:c1, :);
end
end
end
6 changes: 3 additions & 3 deletions code/algorithms/+stack/+zproject/globalCorrelation.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
imageSignal = single( squeeze(imageSignal) );

% Divide data into chunks to use less memory during calculation
tmpIm = stack.reshape.imsplit(IM, true, [], 'numRows', numRows, 'numCols', numCols);
tmpIm = stack.reshape.splitImage(IM, numRows, numCols);
tmpC = cell(size(tmpIm));

for i = 1:size(tmpIm,1)
Expand All @@ -43,9 +43,9 @@
end
end

% Unchunk data
% Reassemble blocks into a single image
[d1,d2,~] = size(IM);
cIm = stack.reshape.imsplit(tmpC, false, [d1,d2,1], 'numRows', numRows, 'numCols', numCols);
cIm = stack.reshape.mergeImageBlocks(tmpC, [d1,d2]);

% cast...
cIm = cIm .* nansen.util.range(P) + P(1);
Expand Down
5 changes: 2 additions & 3 deletions code/algorithms/+stack/+zproject/localCorrelation.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

showWaitbar = false;

getChunkedData = @stack.reshape.imsplit;
tmpIm = getChunkedData(imArray, true, [], 'numRows', numRows, 'numCols', numCols);
tmpIm = stack.reshape.splitImage(imArray, numRows, numCols);

tmpC = cell(size(tmpIm));

Expand Down Expand Up @@ -45,7 +44,7 @@
end

[d1,d2,~] = size(imArray);
cIm = getChunkedData(tmpC, false, [d1,d2,1], 'numRows', numRows, 'numCols', numCols);
cIm = stack.reshape.mergeImageBlocks(tmpC, [d1,d2]);
%cIm = stack.makeuint8(cIm);

cIm = cIm .* nansen.util.range(P) + P(1);
Expand Down