What does the function 'ones' do to a matrix when trying to load an image?

1 次查看(过去 30 天)
I am just wondering why we need to put the ones for this code to work. When I get rid of the ones it does not work. Can I get an explanation of why this is?
BrainImage = ones(91, 109,'int8');
  2 个评论
Matt J
Matt J 2022-7-8
编辑:Matt J 2022-7-8
The only part of the code you've shown us is the part the single line that uses ones(). If you remove it, you'll be left with no code at all, so of course it won't work!!
Alexandar
Alexandar 2022-7-8
Hello Matt, sorry about that. I did not inlcude the rest of my code. My question is about the basic understanding of why we do this for imaging. Here is the rest of the code you can look at.
% Load Atlas
% Relies on https://www.fieldtriptoolbox.org/template/atlas/ and
% https://github.com/fieldtrip/fieldtrip/tree/master/template/atlas and
% https://www.gin.cnrs.fr/en/tools/aal/
aalAtlas = ft_read_atlas('ROI_MNI_V4.nii');
BrainImage = ones(91, 109,'int8'); %creates the variable for the brain image to be read
BrainImage(:,:) = aalAtlas.tissue(:, :, 50) %replaces the one with the tissue is part of the 3D BrainImage of the Atlas that looks at tissue
test_size1 = size(app.CerebralAreasListBox.Value,2) %take into account what is selected, looks at columns only
area_num = ones(1,test_size1) %Changes the # of values selected to a row of that same # of 1's
for j=1:test_size1 % Cycle through the values you have choosen on the list
area_num(j) = str2double(app.CerebralAreasListBox.Value(j));
end
test_size2 = size(app.CerebralAreasListBox_2.Value,2) %take into account what is selected, looks at columns only
area_num2 = ones(1,test_size2)
for j=1:test_size2 % Cycle through lateralization, step change the multiplier
area_num2(j) = str2double(app.CerebralAreasListBox_2.Value(j));
end
test_size3 = size(app.CerebralAreasListBox_3.Value,2) %take into account what is selected, looks at columns only
area_num3 = ones(1,test_size3)
for j=1:test_size3 % Cycle through lateralization, step change the multiplier
area_num3(j) = str2double(app.CerebralAreasListBox_3.Value(j));
end
%put it in a for loop to display each section
for slice = 1:91
BrainImage(:,:) = aalAtlas.tissue(:, :, slice);
ColorBrainImage = ind2rgb(BrainImage, copper);
regions_1 = ismember(BrainImage, area_num);
ColorBrainImage(regions_1) = uint8(2* double(ColorBrainImage(regions_1)));
regions_2 = ismember(BrainImage, area_num2);
ColorBrainImage(regions_2) = uint8(3* double(ColorBrainImage(regions_2)));
regions_3 = ismember(BrainImage, area_num3);
ColorBrainImage(regions_3) = uint8(4* double(ColorBrainImage(regions_3)));
BBrainImage = imresize(ColorBrainImage,9);
imshow(BBrainImage)
end

请先登录,再进行评论。

采纳的回答

GandaBerunda
GandaBerunda 2022-7-8
Hi Alexandr,
ones() returns an array or a matrix all the elements of which are the number 1. In your case, it will return a 91x109 int8 array all of whose elements are 1, which will be stored in BrainImage.

更多回答(1 个)

Image Analyst
Image Analyst 2022-7-9
If you don't, and you just do
BrainImage(:,:) = aalAtlas.tissue(:, :, 50)
then, since ":" means "all" it will try to take the 50th slice and put it into BrainImage. The problem is that since you used : it means 1:end but it doesn't know the size of BrainImage yet so it throws an error with that syntax. It would have worked if you just didn't try to reference any row and column range, like this:
BrainImage = aalAtlas.tissue(:, :, 50)

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by