Clear Filters
Clear Filters

How to replace all nan values in an image to 255 different colors

2 views (last 30 days)
How to replace all nan values in an image to 255 different colors distributed almost equally

Answers (2)

David Hill
David Hill on 2 Mar 2022
idx=yourImage==nan;
N=nnz(idx);
yourImage(idx)=uint8(randi(255,1,N));
  1 Comment
ammu v
ammu v on 3 Mar 2022
thanku for response, but using randi will make it change everytime i suppose.is there a way to fix that

Sign in to comment.


DGM
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.

Tags

Community Treasure Hunt

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

Start Hunting!