How to replace all nan values in an image to 255 different colors
4 views (last 30 days)
Show older comments
How to replace all nan values in an image to 255 different colors distributed almost equally
0 Comments
Answers (2)
DGM
on 3 Mar 2022
Depends what your needs are. If I assume that your image is an RGB color image to begin with, and that you want colors picked sequentially from a given color table:
% specify a color table
ncolors = 256;
ct = parula(ncolors);
% get an image
A = im2double(imread('peppers.png'));
A = imresize(A,0.5);
s = size(A);
% put some NaNs in the image
idx = randi([1 numel(A)],1024,1);
A(idx) = NaN;
% reshape the image
A = reshape(A,[],3);
% get a map of the NaNs
nanmap = any(isnan(A),2);
% generate a list of replacement pixels
fillcolors = ct(mod(0:nnz(nanmap)-1,ncolors)+1,:);
% fill and reshape
A(nanmap,:) = fillcolors;
A = reshape(A,s(1),s(2),3);
imshow(A)
Note that there are 1024 NaNs added to the image, but the color table has only 256 entries. Note that the color table visibly repeats 4 times across the image.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!