fillgaps in an array/matrix/image

Fill gaps in a cell-array/vector/matrix or image based on an alternative cell/vector/matrix/image
738 Downloads
Updated Fri, 13 Jul 2012 16:26:51 +0000

View License

Objective: This code is useful when there is two or more versions of data
for the same dates or spatial points or images where each version has
some gaps in certain fields that are crucial for the analysis. The
objective is to fill the gaps in the best version (or any version if no
preferance), using corresponding values in the alternative versions.

The function accepts a cell-array (or matrix), and replaces NaN values
of certain fields of it with available values of the second or more given
reference cell-arrays (or matrixes). Both cell-arrays and matrixes are
accepted as target and reference(s).
The first two arguments are the target and reference
cell-arrays/matrices, the third argument provides index(es) of fields
where NaNs should be replaced (target and reference cell-arrays/matrices
should have similar fields). More reference cell-arrays/matrices/images
can be given as arbitrary arguments. If the values to be replaced in
target cell-array/matrix are NaNs in the first refenece, the extra
references (if available) will also be checked. If non of the references
have valid value for a NaN record in the target, the returned
cell-array/matrix will be still NaN (no change) for that record. As a
rule, NaNs will be replaced with the available non-NaN values from the
first reference cell-array(s)/matrices.
For images where NaNs or missing data are spread all over the image, all
columns (1:length(cols)) can be given so that the entire image is
repaired based on the available reference image(s). Note that the
function works with doubles, so if the input image is in a bit-scale
(such as 8-bit or any unint), the input first should be turned to double.
If the missing data in the image are, for example, 0, they can be turned
to NaN before calling the function.

Inputs:
target: target cell-array (or matirx)
refarray: reference cell-array (or matirx)
fields2fill: index or indexes of fields where NaNs should be replaced;
it can be only single index or a set of indices.
varargin: one or more extra reference cell-array(s)/matrices.
Outputs:
combined_data: same as target input cell-array/matrix with gaps for the
requested field(s) filled using corresponding field(s) in the reference
cell-arrays/matrices (refarray and extra references if available).
data: a cell-array where the input target and reference cell-arrays
or matrices are stored (can be used to compare data before and after
filling the gaps).
If the input target was cell-array, the output result (combined_data)
will be also cell-array, if the target was a matrix, the result will be
also a matrix. The second output will always be a cell-array of
cell-array elements.

Example:
target=[3 4 NaN 4; 2.4 5 6.1 7; 10 11 1.1 NaN; 0.1 3 2 8];
refarray=[4 2 30 4; 8 7 9 2; 4 5 12 24; 9 8 2 23];
fields2fill=[3 4];
[combined_data,data] = fillgaps(target,refarray,fields2fill)
combined_data =
3.0000 4.0000 30.0000 4.0000
2.4000 5.0000 6.1000 7.0000
10.0000 11.0000 1.1000 24.0000
0.1000 3.0000 2.0000 8.0000
data =
{4x4 cell} {4x4 cell}

Example for image input:

im=imread('NZ.jpg'); %or any 3-band image
b1=double(im(:,:,1)); %single out band 1 (red)
b2=double(im(:,:,2)); %single out band 2 (green)
b1=b1-min(b1(:)); b2=b2-min(b2(:)); %enhance contract by removing bg haze
xidx=randi(400,12); %propogate random noise along lines
yidx=randi(250,12); %propogate random noise along columns
b1(xidx,yidx)=NaN; %assign NaN to random points of b1
% call func to fill NaNs of b1 using b2 as reference:
[b_repaired,data] = fillgaps(b1,b2,1:300);
% compare the results:
subplot(1,2,1), imshow(b1,[min(b1(:)) max(b1(:))]);
title('band1 with holes'), set(gca,'FontWeight','bold','FontSize',16);
subplot(1,2,2), imshow(b_repaired,[min(b1(:)) max(b1(:))]);
title('band1 with holes filled');
set(gca,'FontWeight','bold','FontSize',16);

First version: 04 June 2012
Updated: 09 June 2012 and 12 Jul 2012

Cite As

M Sohrabinia (2024). fillgaps in an array/matrix/image (https://www.mathworks.com/matlabcentral/fileexchange/37479-fillgaps-in-an-array-matrix-image), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2008b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Preprocessing Data in Help Center and MATLAB Answers
Acknowledgements

Inspired: interp1gap, repnan

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.0.0