stacking rgb images into 3d or 4d variable
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
I have mri frames that are in rgb and I want stack them in a 4d variable ( height x width x 3 channels x number of frames) so that I can view them as a montage.
How can i do this?
채택된 답변
a = ones(5,6,3)+0;
b = ones(5,6,3)+1;
c = ones(5,6,3)+2;
stacked = cat(4,a,b,c);
% ^ concatenate along 4th dim
If the image sizes are not identical, you can pad them smaller ones to match the size of the larged one by using paddarray() and then use cat().
댓글 수: 10
Raheema Al Karim Damani
2019년 9월 26일
편집: Raheema Al Karim Damani
2019년 9월 26일
thank you for your reply. I have 33 frames which were loaded and stored in a 1x33 cell (baseFile86611) , each frame is 128 x 128 x 3. so could you explain what a and b would mean in this context.
file86611 = dir(fullfile(img_Folder86611 , '*.dcm'));
for P = 1:length(file86611)
baseFile86611 = {file86611.name};
end
This fake data fits your description
%Fake data
data = arrayfun(@(x)rand(128,128,3),1:33,'UniformOutput',false);
To concatenate it,
array = cat(4,data{:});
Check size of array
size(array)
% ans =
% 128 128 3 33
Raheema Al Karim Damani
2019년 9월 26일
편집: Adam Danz
2019년 9월 27일
thank you it worked! It created a 4d variable, however when I try to visualize the 4d variable as a montage
array = uint8(array);
montage86611 = imtile(array, []);
set(gca, 'clim');
figure; imshow(montage86611);
it does not display the images correctly. what am i doing wrong?
After you concatenate using cat(), what is the size of "array" output?
size(array)
Also, just curious, what's the purpose of the 8-bit unsigned conversion (unit8()) ?
If you attach a mat file with "array" variable just after you concatenate, I can take a look at it.
"the size of array is 1 x 10 x 1 x 33 "
Well, that tells us something's wrong.
The code you attached starts with this:
file86611 = dir(fullfile(img_Folder86611 , '*.dcm'));
for P = 1:length(file86611)
baseFile86611 = {file86611.name};
end
There are 2 problems with the for-loop.
- On each iteration of your loop, the variable "baseFile86611" is being overwritten...
- ...but that doesn't matter because you're writing the same values to it each time.
In other words, that for-loop does the same thing over and over again: it assigns a cell array of filenames to the variable.
Then you're reading in the images but you never use the image variable! Instead, you're concatenating the file names.
array = cat(4, baseFile86611{:}); %stack the images into 4-D array
% ^^^^^^^^^^^^^^
% This is your list of filenames
What's the purpose of this line? The variable "array" is being overwritten on the next line.
array = zeros(info86611.Rows, info86611.Columns, 3, images86611 , class86611);
There are lots of things wrong with this code but the good news is that it's a very short code so it shouldn't take you look to study it. I suggest you clear your workspace and execute each line of code - one line at a time - and understand what is going on in each line of your code.
- What do the variable values look like?
- Are the outputs to each function what you expect?
- Where does the code start to go wrong?
Ah I see what you're saying. I forgot to add the code where i used dicomread to read in one image so that i could find the dimensions of the frame (128 x 128 x 3)
info86611 = dicominfo(fullfile(img_Folder86611, baseFile86611{1})); %examine information from metadata
slice86611 = dicomread(fullfile(img_Folder86611 , baseFile86611{1}))
Okay so how do I create a cell array containing all the images?
From your quesiton,
I have mri frames that are in rgb and I want stack them in a 4d variable ( height x width x 3 channels x number of frames) so that I can view them as a montage.
I assumed that you had already read in the images somehwere. First you'll need to read in the dcm images using dicomread. Below is a link to the documentation for dicomread and it contains examples of how to plot a montage. Those examples are different from my approach since I didn't know you were using this type of image file.
Give that a try and if you get stuck, share your updated code and describe where you're stuck.
Raheema Al Karim Damani
2019년 11월 5일
편집: Raheema Al Karim Damani
2019년 11월 5일
could you help me with this error I'm getting with trying to fuzzy cluster nonzero pixels in my image. mage in which I have isolated the tumor region using thresholding (img 1) . Now im trying to use fuzzy clustering to cluster the pixels in just the tumor ROI but not the black background (basically ignore any pixels = 0 in clustering).When I try to segment the clusters into images it s not outputting any clustered images, because its throwing an error that "to reshape the number of elements must not change."
fuzzyT2 = mriAdjust(:,:,15);
[y, x] = find(fuzzyT2)
idx = fuzzyT2 > 0 %creates a mask for true value for all nonzero pixels
z = fuzzyT2(idx);
%% fuzzy c-means
G = 4
z = double(z)
[cluster_idx, cluster_center, obj] = fcm(z, G)
% reshape into four 2-D array
cluster1 = cluster_center(1,:);
cluster2 = cluster_center(2,:);
cluster3 = cluster_center(3,:);
cluster4 = cluster_center(4,:)
imIDX1 = reshape(cluster1, size(idx));
imIDX2 = reshape(cluster2, size(idx));
imIDX3 = reshape(cluster3, size(idx));
imIDX4 = reshape(cluster4, size(idx));
subplot(2,2,1), imshow(img_1), title('cluster 1')
subplot(2,2,2), imshow(img_2), title('cluster 2')
subplot(2,2,3), imshow(img_3), title('cluster 3')
subplot(2,2,4), imshow(img_4), title('cluster 4')
Adam Danz
2019년 11월 6일
I see you've asked similar questions since adding the question above. If you still need help, please create a new question in the forum and send me the link to your new question or add the link as a comment below.
Thank you for your response. Yes here is the link to the question I put previously on the forum https://www.mathworks.com/matlabcentral/answers/489011-fuzzy-clustering-only-on-pixels-zero
I've re put my question on the forum, showing my thought process of trying to work through this problem, but Im still getting the same reshaping error
추가 답변 (1개)
Raheema Al Karim Damani
2019년 9월 27일
the array was in double so I thought to turn it in to uint8 class which was the original format of the frames so that i could visualize it.
the size of array is 1 x 10 x 1 x 33 which is strange.
and array is stored as a 4-d uint8.
where as the dimensions of each frame is 128 x 128 x 3, so i'm guessing that when it is concatenating it is cutting down the image dimensions.
카테고리
도움말 센터 및 File Exchange에서 Built-In Training에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
