How do I grayscale, find the area and centre of mass of a '64x32x148 double'?

6 views (last 30 days)
Hi, I am beginner with matlab and I am wanting to grayscale a 64x32x148 double, then subsequently finding the centre of mass and the area of the double, but been having some trouble. If you could point out the mistake in my methodology would be most appreciated:
for x=1:length(LA_maxDF_M)
I(x,:)=mat2gray(LA_maxDF_M); %converts to grayscale image
area(x,:)=grayarea(gray); %calculate surface area of grayscale image
c(x,:)=centerOfMass(gray); %calculate centre of mass of grayscale image
end
when I try this an error says: "Subscripted assignment dimension mismatch."

Accepted Answer

Guillaume
Guillaume on 15 Apr 2015
Edited: Guillaume on 16 Apr 2015
I'm assuming that LA_maxDF is the 64x32x148 array, and that it is a stack of 2D images.
The line that is not going to work is
I(x, :) = mat2gray(LA_maxDF_M);
because your indexing makes no sense.
I(:, :, x) = mat2gray(LA_maxDF_M(:, :, x));
would work. But most likely, you meant to do:
gray = mat2gray(LA_maxDF_M(:, :, x));
possibly followed by
I(:, :, x) = gray;
if you want to keep the greyscale images around.
It does not look like you predeclared any of your output array, so they're going to be resized on each iteration of the loop. That's not a good idea. Predeclaring the arrays, would also help you in identifying how to index them.
Secondly, your for line works as you intend for an array of size 64x32x148 because the number of pages is the more than the width or height of your images. If that weren't the case, then length(...) would return either the width or the height (whichever is greater) of the image, and your code would not work. As a matter of rule, don't use length either use size with an explicit dimension for matrices or numel for vectors.
Finally, it's a bad idea to use x as a variable name if you're iterating across the pages of a 3D array. z or page would be better.
In summary, this may be what you were trying to do:
grayimages = zeros(size(LA_maxDF_M)); %optional if you don't want to keep the grey images around
numpages = size(LA_maxDF_M, 3); %do not use length, use size with an explicit dimension
area = zeros(numpages, 1);
c = zeros(numpages, 2); %assuming centreOfMass returns a 2 element vector
for page = 1:numpages
grayimage = mat2gray(LA_maxDF_M(:, :, page));
grayimages(:, :, page) = grayimage %optional if you don't want to keep the grey images around
area(page) = grayarea(grayimage); %calculate surface area of grayscale image. It's a scalar
c(page, :) = centerOfMass(grayimage); %calculate centre of mass of grayscale image
end
edit: misspelt some variables
  2 Comments
johnsmithofmatlab
johnsmithofmatlab on 15 Apr 2015
Edited: johnsmithofmatlab on 15 Apr 2015
Hey I now understand the need for the numpages, am I right in saying it provides a suitable 'canvas' for the values to be masked on? As for the code, the grayscaling seems to be working, judging from the values. Though the area and centre of mass functions don't seem to work: c(page,:) = centerOfMass(grayimages); yields an error: "Subscripted assignment dimension mismatch." and the area: I am looking for a value for the area for each individual 2D image so wouldn't I need: area(page,:) = area(grayimages)? Also get the error: "Subscript indices must either be real positive integers or logicals."
Guillaume
Guillaume on 16 Apr 2015
I'd forgot to change some variable names when I pasted and edited your code. It looks like you spotted the mistake but possibly, you made the wrong edits. I've edited the answer to correct the mistakes.
My assumptions were that:
  • centerOfMass expects a 2D grey image and returns a 1x2 row vector. If that is not the case, then you'll get a dimension mismatch error.
Therefore, what is
size(centerOfMass(grayimage)) %note the variable is grayimage not grayimages
  • grayarea expects a 2d grey image and returns a scalar. I'm not sure how you could get a subscript index error though.
Since area is a numpages x 1 column vector, area(page, :) is the same as area(page, 1:1), thus the same as area(page).

Sign in to comment.

More Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!