Problem in using imcrop

9 visualizaciones (últimos 30 días)
Farisa
Farisa el 15 de Mayo de 2023
Comentada: Farisa el 15 de Mayo de 2023
Error in using imcrop function as follows:
Error using images.internal.crop.parseInputsOverTwo>validateRectangle
Input number 2, RECT, is expected to contain 4 elements.
Error in images.internal.crop.parseInputsOverTwo (line 54)
validateRectangle(spatial_rect,2);
Error in imcrop (line 104)
images.internal.crop.parseInputsOverTwo(varargin{:});
Error in HistRuru (line 31)
MaskCrop = imcrop(Mask, thisBB); % Crop GT in binary
My code is:
while hasdata(train)
img = read(train) ; % read image from datastore
red = img(:,:,1); % Red channel
green = img(:,:,2); % Green channel
x = read(GTTrain);
Mask = logical(x);
% Crop Image
props = regionprops(Mask, 'BoundingBox');
thisBB = props.BoundingBox;
MaskCrop = imcrop(Mask, thisBB); % Crop GT in binary
TrainCrop = imcrop(green, thisBB); % Crop image in red channel
% Masked & cropped image
% Use GT as mask
Maskedred = bsxfun(@times, uint8(TrainCrop), cast(MaskCrop,class(uint8(TrainCrop))));
figure;
imhist(Maskedred);
saveas(gcf,['histo green train ' num2str(trainName(i)) '.jpg'])
count = count + imhist(green);
countmasked = countmasked + imhist(Maskedred);
i = i + 1;
end

Respuestas (1)

Walter Roberson
Walter Roberson el 15 de Mayo de 2023
x = read(GTTrain);
Mask = logical(x);
If the result of the read(GTTrain) has 3 or more dimensions (for example it is RGB) then logical() of it will have the same number of dimensions, and regionprops('BoundingBox') will be a vector of length 2 * ndims(x) (for example length 6 for RGB)
  3 comentarios
Walter Roberson
Walter Roberson el 15 de Mayo de 2023
After
thisBB = props.BoundingBox;
do
BB2 = reshape(thisBB, [], 2);
thisBB = reshape(BB2(1:2,:), 1, []);
Whether x is read in as 2D or as 3D, this will extract the bounding box from the first 2 dimensions, giving a vector of length 4 (total) suitable for imcrop.
MaskCrop = imcrop(Mask, thisBB); % Crop GT in binary
Note though that if x is 3D then the cropping that is done will not affect the third dimension -- so if for example you had a sphere inside a cube outline, then the cropping would remove the left, right, up, down extra area, but leave the extra front and back area (or something like that, depending which axes you are talking about.)
green = img(:,:,2); % Green channel
That is certain to be 2D
TrainCrop = imcrop(green, thisBB); % Crop image in red channel
with thisBB having been extracted specifically to give the 2D subset, provided that the number of rows and columns in train is the same as the number of rows and columns in GTTrain, then this imcrop should work, even though GTTrain might be 3D whereas green is certain to be 2D.
Maskedred = bsxfun(@times, uint8(TrainCrop), cast(MaskCrop,class(uint8(TrainCrop))));
That code appears to have been specifically designed for the case where TrainCrop (2D) might be a different size than MaskCrop (might be 2D might be 3D) -- if the code was expecting that TrainCrop and MaskCrop are both certain to be 2D then the code could have used .* instead of bsxfun .
It seems rather strange, though, to bother multiplying what might be a 3D mask by 2D data... what is the point of such an output?
I would suggest to you that you should be considering a completely different approach -- you can make sure that GTTrain is 2D so you do not have to worry about this 3D stuff and can use the mask code you already had... or you can (if necessary) convert GTTrain to grayscale (assuming that the problem is that it is RGB)
Farisa
Farisa el 15 de Mayo de 2023
Okay, it's really really helpful. Thanks A Lot Ms/Mr.Robertson!

Iniciar sesión para comentar.

Etiquetas

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by