How to replace all nan values in an image to 255 different colors
4 次查看(过去 30 天)
显示 更早的评论
How to replace all nan values in an image to 255 different colors distributed almost equally
0 个评论
回答(2 个)
DGM
2022-3-3
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 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!