フィルターのクリア

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

2 ビュー (過去 30 日間)
ammu v
ammu v 2022 年 3 月 2 日
回答済み: DGM 2022 年 3 月 3 日
How to replace all nan values in an image to 255 different colors distributed almost equally

回答 (2 件)

David Hill
David Hill 2022 年 3 月 2 日
idx=yourImage==nan;
N=nnz(idx);
yourImage(idx)=uint8(randi(255,1,N));
  1 件のコメント
ammu v
ammu v 2022 年 3 月 3 日
thanku for response, but using randi will make it change everytime i suppose.is there a way to fix that

サインインしてコメントする。


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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by