Cropping the same roipoly for multiple images

16 ビュー (過去 30 日間)
Marios Kats
Marios Kats 2021 年 10 月 23 日
編集済み: Marios Kats 2021 年 10 月 25 日
I've created a code that opens two images and crops the same rectangle on both of them.
Code:
%%% Crop Selected Slice %%% part1
dicomSeriesTime = dicomSeriesTime - dicomSeriesTime(1,1);
selectedSlice = dicomStruct(i).dcmImg(:,:,selectedSliceNum);
rescaledX1 = rescale_Image(selectedSlice);
rescaledX1 = selectedSlice;
[croppedX1start, croppedBox] = imcrop(rescaledX1)
[dcmCropped, croppedBox] = imcrop(selectedSlice, croppedBox);
selectedSliceSize = size(selectedSlice);
figure; imagesc(dcmCropped), colormap(gray)
%%% Crop Rest Slices at same area as Selected Slice and Calculate Mean %%%part2
%%% Intensity of Cropped Slices
for j=1:dicomSeriesNum
currentSeriesSelectedSlice = dicomStruct(j).dcmImg(:,:,selectedSliceNum);
dcmVibeCroppedSeriesROI(:,:,j) = imcrop(currentSeriesSelectedSlice,croppedBox); %error part
currentSeriesSelectedSliceCropped = dcmVibeCroppedSeriesROI(:,:,j);
figure; imagesc(currentSeriesSelectedSliceCropped), colormap(gray)
figure;
imshow(currentSeriesSelectedSlice,[]); % colormap(gray)
title('ROI of Lesion Selected');
hold on
%%% Select ROI of Lesion %%%part3
croppedBoxMask = zeros(selectedSliceSize(1),selectedSliceSize(2));
croppedBoxMask(croppedBox(2):croppedBox(2)+croppedBox(4),croppedBox(1):croppedBox(1)+croppedBox(3)) = 1;
structBoundariesCropped = bwboundaries(croppedBoxMask);
xyCropped = structBoundariesCropped{1}; % Get n by 2 array of x,y coordinates.
xCropped = xyCropped(:, 2); % Columns.
yCropped = xyCropped(:, 1); % Rows.
xBoundariesCropped = xCropped;
yBoundariesCropped = yCropped;
plot(xBoundariesCropped,yBoundariesCropped,'r')
%%%This is added after the roipoly to show the 2 areas cropped
% hold on
%plot(min(xBoundariesCropped) + xroi,min(yBoundariesCropped)+yroi,'r')
end
This works correctly. I now want to enhance the code in a way that, after part1, the user will be given the option to drawfreehand or roipoly inside the cropped rectangle. The code i've created for this is:
%%%placed just after part1
[Roi,xroi,yroi] = roipoly; %imfreehand; drawfreehand; roipoly;
Roi = uint8(Roi);
figure;
imagesc(dcmCropped),colormap(gray)
title ('Region of Interest');
dcmCropped2 = (dcmCropped.*Roi);
figure;
imagesc(dcmCropped2),colormap(gray)
title ('Region of Interest2');
Now, i want to edit parts 2 and 3 in a way that the selected roipoly is applied in the other images, the same way the code works for the rectangle. I've been trying to do that in the same principle but it is not working. Even from the get go, changing cropppedBox in part2 with Roi or even an array containg the coordinates xroi yroi gives this error:
Error using parseInputsOverTwo
Expected input number 2, RECT, to be a vector.
Error in images.internal.crop.parseInputsOverTwo>validateRectangle (line 173)
validateattributes(rect,{'numeric'},{'real','vector'}, ...
Error in images.internal.crop.parseInputsOverTwo (line 54)
validateRectangle(spatial_rect,2);
Error in imcrop (line 104)
images.internal.crop.parseInputsOverTwo(varargin{:});
Error in project2 (line 91)
dcmVibeCroppedSeriesROI(:,:,j) = imcrop(currentSeriesSelectedSlice,Roi);
Please ask me if you require further information or more parts of the code, i didn't want this to be a huge post. Just before part1, is a loop that opens, reads and places the images inside a struct.
and rescale images is just:
function [rescaledImage] = rescale_Image(Image)
minValue = min(min(Image));
maxValue = max(max(Image));
%k = 65536/(maxValue - minValue)
k = 65536/600;
rescaledImage = k*Image;
Any inputs on how to resovle this and maybe continue the same way, or a new way?
Thank you for your time.

採用された回答

Matt J
Matt J 2021 年 10 月 23 日
編集済み: Matt J 2021 年 10 月 23 日
I don't understand what you mean by "cropping" an image to a non-rectangular region. An image, cropped or otherwise, is an inherently rectangular thing.
However, if you mean you want to apply a binary mask to multiple images, all you need to do is stack them in a 3D array and multiply with the ROI mask, as in the example below:
Images=rand(5,5,3);
ROI=[0 1 1 0 0
0 0 1 1 0
0 1 1 1 0
0 0 0 0 0
0 0 0 0 0 ];
Images.*ROI
ans =
ans(:,:,1) = 0 0.3850 0.3234 0 0 0 0 0.2919 0.2281 0 0 0.3257 0.2491 0.5684 0 0 0 0 0 0 0 0 0 0 0 ans(:,:,2) = 0 0.1263 0.6733 0 0 0 0 0.2816 0.6985 0 0 0.9292 0.6877 0.8524 0 0 0 0 0 0 0 0 0 0 0 ans(:,:,3) = 0 0.0329 0.4931 0 0 0 0 0.7209 0.9280 0 0 0.1487 0.0998 0.6810 0 0 0 0 0 0 0 0 0 0 0
  3 件のコメント
Marios Kats
Marios Kats 2021 年 10 月 25 日
Indeed, it does, thank you!...only problem now, is that the Roi is not a compatible size for the second image.
Resizing manually with imresize does not really do much besides showing me that what you said is correct, because if either of them gets smaller or bigger, i miss my Region.

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

その他の回答 (0 件)

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by