Is it possible to set some elements in animage array to null or in some way that they are ignored?
I have an automatically detected circular ROI outside of which is to be ignored (those values will skew the results) and need to step through the array using a for loop. Obviously a for loop can only step in rectangles so is there any way I can set an element in that image array to null or NaN that is acceptable?
Regards
Tim

댓글 수: 4

Manikanta Aditya
Manikanta Aditya 2024년 11월 13일
Yes, you can set elements in an image array to NaN to effectively ignore them in your calculations.
  1. Create a mask for the circular ROI.
  2. Apply the mask to the image array to set the values outside the ROI to NaN.
Tim
Tim 2024년 11월 13일
Hi Manikanta, thanks for the reply. I tried manually setting an element in the resultant array to NaN but it set it to 0 which isn't the same thing. Maybe I should try programetically?
Manikanta Aditya
Manikanta Aditya 2024년 11월 13일
Yes, you can programmatically set elements in an image array to NaN to ignore them in your calculations.
DGM
DGM 2024년 11월 13일
What is the actual applied operation? Does it actually require a loop, or can it be done with basic logical indexing?

댓글을 달려면 로그인하십시오.

 채택된 답변

Epsilon
Epsilon 2024년 11월 13일
편집: Epsilon 2024년 11월 13일

0 개 추천

Hi Tim,
It is possible to set the elements to NAN in order to be ignored. Attaching an example:
% Sample image array
img = rand(100);
% Define the circular ROI
[rows, cols] = size(img);
center = [50, 50];
radius = 20;
% Create a mask for the circular ROI
[x, y] = meshgrid(1:cols, 1:rows);
mask = sqrt((x - center(1)).^2 + (y - center(2)).^2) <= radius;
% Set elements outside the circle to NaN
imgModified = img;
imgModified(~mask) = NaN;
% Display original and modified images
figure;
subplot(2, 1, 1); imagesc(img); title('Original'); colorbar;
subplot(2, 1, 2); imagesc(imgModified); title('Modified'); colorbar;
Hope it helps.

댓글 수: 2

Tim
Tim 2024년 11월 14일
That worked well. Thanks Epsilon
Walter Roberson
Walter Roberson 2024년 11월 14일
This code relies upon img being type double or single . This code would not work if img is uint8() for example. You really should use im2double() like I showed in my Answer.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Walter Roberson
Walter Roberson 2024년 11월 13일
편집: Walter Roberson 2024년 11월 13일

1 개 추천

The trick is that if your image array is not datatype single() or double(), then there is no way to set locations to be ignored as part of the same array
You can, however,
Imd = im2double(YourImageArray);
Imd(AppropriateLocations) = nan;
This will not in itself cause locations to be skipped. For example if you have
for ROW = 1 : size(YourImageArray,1)
for COL = 1 : size(YourImageArray,2)
do something with Imd(ROW,COL)
end
end
then this code in itself will not skip the locations that are NaN. You would need
for ROW = 1 : size(YourImageArray,1)
for COL = 1 : size(YourImageArray,2)
if ~isnan(Imd(ROW,COL))
do something with Imd(ROW,COL)
end
end
end
but if you are going to do that then you might as well use
for ROW = 1 : size(YourImageArray,1)
for COL = 1 : size(YourImageArray,2)
if YourImageMask(ROW,COL)
do something with YourImageArray(ROW,COL)
end
end
end
Steven Lord
Steven Lord 2024년 11월 13일

0 개 추천

If your images are stored in an integer data type, assigning a NaN value into that array will store a 0 in that location. There is no equivalent of a NaN value in the integer types; all the possible bit patterns that can be stored in that type represent finite values.
A = int16(magic(3))
A = 3×3
8 1 6 3 5 7 4 9 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
A(3, 3) = NaN % 2 replaced by 0 not NaN
A = 3×3
8 1 6 3 5 7 4 9 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
B = uint32(magic(4))
B = 4×4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
B(2, 4) = NaN % 8 replaced by 0 not NaN
B = 4×4
16 2 3 13 5 11 10 0 9 7 6 12 4 14 15 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Tim
Tim 2024년 11월 14일

0 개 추천

Thanks for your help guys.

카테고리

도움말 센터File Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

제품

릴리스

R2024b

질문:

Tim
2024년 11월 13일

댓글:

2024년 11월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by