Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
ammu v
ammu v am 2 Mär. 2022
Beantwortet: DGM am 3 Mär. 2022
How to replace all nan values in an image to 255 different colors distributed almost equally

Antworten (2)

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

Melden Sie sich an, um zu kommentieren.


DGM
DGM am 3 Mär. 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!

Translated by