Why do I receive "attempt to grow array along ambiguous dimension" when trying to apply a binary mask over a greyscale image?
7 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Radoslav Yankov
il 6 Set 2021
Commentato: Radoslav Yankov
il 7 Set 2021
I am working on a particle detection project and I am using the Hough method to detect the particles (white rings on a greyscale background). Once the particle is detected I want to crop it and apply a black circular mask over it in order to exclude overlapping particles from my dataset. (If the detected particle overlaps with another particle the intensity outside of the masked area is above my threshold and the image is rejected). The code works fine for about 70-100 iterations and after that I get the following error message:
Attempt to grow array along ambiguous dimension.
Error in F1_save_PI_M2 (line 31)
greyscale(circleMask) = 0;
Here is the Code:
for i=1:length(xA)
D = dA(i);
if(D > 0)
%% Cutting out particle images
pIN = pIN +1; %running variable for particle image number
ol = 63; %outline around particle center
lb = max(yA(i)-ol,1);
rb = min(yA(i)+ol+1,size(A,1));
bb = max(xA(i)-ol,1);
ub = min(xA(i)+ol+1,size(A,2));
partIm = A(lb:rb, bb:ub); %stiff image size definition
%% Reject Overlaps, random noise by masking image
% Create circular mask
imageSizeX = 128;
imageSizeY = 128;
cX = 63;
cY = 63;
R = D/2 + ovl;
[columns rows] = meshgrid(1:imageSizeX, 1:imageSizeY);
circleMask = (rows - cY).^2 + (columns - cX).^2 <= R.^2;
greyscale=partIm;
greyscale(circleMask) = 0;
0 Commenti
Risposta accettata
Walter Roberson
il 6 Set 2021
partIm = A(lb:rb, bb:ub)
That would typically be a 2D array.
imageSizeX = 128;
imageSizeY = 128;
% stuff
[columns rows] = meshgrid(1:imageSizeX, 1:imageSizeY);
circleMask = (rows - cY).^2 + (columns - cX).^2 <= R.^2;
That is a logical array that is 128 x 128, which is bigger than partIm
greyscale=partIm;
greyscale(circleMask) = 0;
circleMask is larger than grayscale, so assigning 0 into those locations would requiring expanding the array. MATLAB is confused about how to do that growing.
You could do
if size(grayscale,1) < imageSizeY; grayscale(imageSizeY,1) = 0; end
if size(grayscale,2) < imageSizeX; grayscale(1,imageSizeX) = 0; end
Those lines would, if necessary, expand grayscale to the same size as circleMask.
Or... you could use
imageSizeX = min(128, size(partIm,2));
imageSizeY = min(128, size(partIm,1));
% stuff
[columns rows] = meshgrid(1:imageSizeX, 1:imageSizeY);
circleMask = (rows - cY).^2 + (columns - cX).^2 <= R.^2;
and then you would know that circleMask would be no larger than partIm.
I have to wonder, though, whether you really want to apply the circle mask to whatever whatever the selected portion of the image is as if that selected window was the upper-left corner of the circle. It would seem to me to make more sense to create a circle mask the size of the original image, and then extract the same portion of it as you extracted for partIm, and mask.
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!