Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

error in code impementation

3 views
Skip to first unread message

memo mohd

unread,
Dec 30, 2009, 2:09:04 PM12/30/09
to
hello
i want to measure the senter of gravity of image
i am use this code

M=imread('D:\f1.jpg');
size(M)
[rc,cc] = ndgrid(1:Size(M,1),1:Size(M,2));
Mt = sum(M(:));
c1 = sum(M(:) .* rc(:)) / Mt;
c2 = sum(M(:) .* cc(:)) / Mt;


but see the error that
in
=

240 320 3

??? Error using ==> times
Integers can only be combined with integers of the same class, or scalar doubles.

Error in ==> sadih at 5
c1 = sum(M(:) .* rc(:)) / Mt;

please help me to solve the error?

us

unread,
Dec 30, 2009, 2:26:05 PM12/30/09
to
"memo mohd" <m-a...@hotmail.com> wrote in message <hhg8gg$q1u$1...@fred.mathworks.com>...

one of the many solutions

M=imread('D:\f1.jpg');
% must read
M=double(imread('D:\f1.jpg'));

us

memo mohd

unread,
Dec 30, 2009, 6:14:03 PM12/30/09
to
"us " <u...@neurol.unizh.ch> wrote in message >
> one of the many solutions
>
> M=imread('D:\f1.jpg');
> % must read
> M=double(imread('D:\f1.jpg'));
>
> us

thanks for replay
but also there are error !!!

Undefined command/function 'Size'.

Error in ==> mmww at 3


[rc,cc] = ndgrid(1:Size(M,1),1:Size(M,2));

can you help me please?

us

unread,
Dec 30, 2009, 6:48:03 PM12/30/09
to
"memo mohd"

> Undefined command/function 'Size'.
>
> Error in ==> mmww at 3
> [rc,cc] = ndgrid(1:Size(M,1),1:Size(M,2));
>
> can you help me please?

a hint:
- replace
Size
with
size

us

memo mohd

unread,
Dec 31, 2009, 6:04:03 AM12/31/09
to
"us " <u...@neurol.unizh.ch> wrote in message >
> a hint:
> - replace
> Size
> with
> size
>
> us

the same error :(
any other solutions please?

us

unread,
Dec 31, 2009, 7:44:03 AM12/31/09
to
"memo mohd" <m-a...@hotmail.com> wrote in message <hhi0f2$p8r$1...@fred.mathworks.com>...

this clearly cannot(!) be...
- show CSSM the result of

which size -all;
% must yield something like
% built-in (MATLABROOT\toolbox\matlab\elmat\size) % <- first entry
% ...

us

memo mohd

unread,
Dec 31, 2009, 11:06:04 AM12/31/09
to
"us " <u...@neurol.unizh.ch> wrote in message <> this clearly cannot(!) be...

> - show CSSM the result of
>
> which size -all;
> % must yield something like
> % built-in (MATLABROOT\toolbox\matlab\elmat\size) % <- first entry
> % ...
>
> us
i am not understand what did you mean
can you explain for me clearly?
thanks a lot

ImageAnalyst

unread,
Dec 31, 2009, 11:22:42 AM12/31/09
to
He means go to the command window. At the >> prompt, type in "which
size -all" and hit enter. Then come back here and reply to this
thread and paste in what it typed out in the command window after you
issued that command. It should say something like:

built-in (C:\Program Files\MATLAB\R2008b\toolbox\matlab\elmat\size)

It should just list that one built-in function. If it lists two
things for you then you have "overridden" the size() function and that
is causing your problem. Hopefully that is clear enough.

us

unread,
Dec 31, 2009, 11:40:22 AM12/31/09
to
ImageAnalyst <imagea...@mailinator.com> wrote in message <c467b04c-51cb-4754...@m26g2000yqb.googlegroups.com>...

hopefully(!)... :-) ...
anyhow, thanks, ia, for dropping in - and all the best for the next decade to you and your family...
urs

memo mohd

unread,
Dec 31, 2009, 11:52:05 AM12/31/09
to
ImageAnalyst <imagea...@mailinator.com> wrote in message <c467b04c-51cb-

this is the result
built-in (C:\Program Files\MATLAB71\toolbox\matlab\elmat\size)
C:\Program Files\MATLAB71\toolbox\matlab\iofun\@timer\size.m % timer method
C:\Program Files\MATLAB71\toolbox\matlab\iofun\@serial\size.m % serial method
C:\Program Files\MATLAB71\toolbox\matlab\timeseries\@tscollection\size.m % tscollection method
C:\Program Files\MATLAB71\toolbox\matlab\timeseries\@timeseries\size.m % timeseries method
C:\Program Files\MATLAB71\toolbox\comm\comm\@gf\size.m % gf method
C:\Program Files\MATLAB71\toolbox\control\control\@zpk\size.m % zpk method
C:\Program Files\MATLAB71\toolbox\control\control\@tf\size.m % tf method
C:\Program Files\MATLAB71\toolbox\control\control\@ss\size.m % ss method
C:\Program Files\MATLAB71\toolbox\control\control\@frd\size.m % frd method
C:\Program Files\MATLAB71\toolbox\daq\daq\@daqdevice\size.m % daqdevice method
C:\Program Files\MATLAB71\toolbox\daq\daq\@daqchild\size.m % daqchild method
C:\Program Files\MATLAB71\toolbox\ftseries\ftseries\@fints\size.m % fints method
C:\Program Files\MATLAB71\toolbox\ident\ident\@idmodel\size.m % idmodel method
C:\Program Files\MATLAB71\toolbox\ident\ident\@idfrd\size.m % idfrd method
C:\Program Files\MATLAB71\toolbox\ident\ident\@iddata\size.m % iddata method
C:\Program Files\MATLAB71\toolbox\imaq\imaq\@videosource\size.m % videosource method
C:\Program Files\MATLAB71\toolbox\imaq\imaq\@videoinput\size.m % videoinput method
C:\Program Files\MATLAB71\toolbox\instrument\instrument\@visa\size.m % visa method
C:\Program Files\MATLAB71\toolbox\instrument\instrument\@udp\size.m % udp method
C:\Program Files\MATLAB71\toolbox\instrument\instrument\@tcpip\size.m % tcpip method
C:\Program Files\MATLAB71\toolbox\instrument\instrument\@icgroup\size.m % icgroup method
C:\Program Files\MATLAB71\toolbox\instrument\instrument\@icdevice\size.m % icdevice method
C:\Program Files\MATLAB71\toolbox\instrument\instrument\@gpib\size.m % gpib method
C:\Program Files\MATLAB71\toolbox\mbc\mbc\@xregpointer\size.m % xregpointer method
C:\Program Files\MATLAB71\toolbox\mbc\mbcdata\@cgoppoint\size.m % cgoppoint method
C:\Program Files\MATLAB71\toolbox\mbc\mbcdesign\@xregdesign\size.m % xregdesign method
C:\Program Files\MATLAB71\toolbox\mbc\mbcdesign\@designdev\size.m % designdev method
C:\Program Files\MATLAB71\toolbox\mbc\mbcdesign\@candidateset\size.m % candidateset method
C:\Program Files\MATLAB71\toolbox\mbc\mbcguitools\@xregtable\size.m % xregtable method
C:\Program Files\MATLAB71\toolbox\mbc\mbcguitools\@xreglistctrl\size.m % xreglistctrl method
C:\Program Files\MATLAB71\toolbox\mbc\mbcmodels\@xregusermod\size.m % xregusermod method
C:\Program Files\MATLAB71\toolbox\mbc\mbcmodels\@xregunispline\size.m % xregunispline method
C:\Program Files\MATLAB71\toolbox\mbc\mbcmodels\@xregtwostage\size.m % xregtwostage method
C:\Program Files\MATLAB71\toolbox\mbc\mbcmodels\@xregmultilin\size.m % xregmultilin method
C:\Program Files\MATLAB71\toolbox\mbc\mbcmodels\@xregmulti\size.m % xregmulti method
C:\Program Files\MATLAB71\toolbox\mbc\mbcmodels\@xregmodswitch\size.m % xregmodswitch method
C:\Program Files\MATLAB71\toolbox\mbc\mbcmodels\@xregmodel\size.m % xregmodel method
C:\Program Files\MATLAB71\toolbox\mbc\mbcmodels\@xreglinear\size.m % xreglinear method
C:\Program Files\MATLAB71\toolbox\mbc\mbcmodels\@localusermod\size.m % localusermod method
C:\Program Files\MATLAB71\toolbox\mbc\mbcmodels\@localtruncps\size.m % localtruncps method
C:\Program Files\MATLAB71\toolbox\mbc\mbcmodels\@localsurface\size.m % localsurface method
C:\Program Files\MATLAB71\toolbox\mbc\mbcmodels\@localpspline\size.m % localpspline method
C:\Program Files\MATLAB71\toolbox\mbc\mbcmodels\@localmulti\size.m % localmulti method
C:\Program Files\MATLAB71\toolbox\mbc\mbcmodels\@localbspline\size.m % localbspline method
C:\Program Files\MATLAB71\toolbox\mbc\mbctools\@xregmonitorplotproperties\size.m % xregmonitorplotproperties method
C:\Program Files\MATLAB71\toolbox\mbc\mbctools\@xregdataset\size.m % xregdataset method
C:\Program Files\MATLAB71\toolbox\mbc\mbctools\@sweepsetfilter\size.m % sweepsetfilter method
C:\Program Files\MATLAB71\toolbox\mbc\mbctools\@sweepset\size.m % sweepset method
C:\Program Files\MATLAB71\toolbox\mbc\mbctools\@guidarray\size.m % guidarray method
C:\Program Files\MATLAB71\toolbox\mpc\mpc\@mpc\size.m % mpc method
C:\Program Files\MATLAB71\toolbox\opc\opc\@opcroot\size.m % opcroot method
C:\Program Files\MATLAB71\toolbox\robust\robust\@uss\size.m % uss method
C:\Program Files\MATLAB71\toolbox\robust\robust\@umat\size.m % umat method
C:\Program Files\MATLAB71\toolbox\robust\robust\@ufrd\size.m % ufrd method
C:\Program Files\MATLAB71\toolbox\robust\robust\@ndlft\size.m % ndlft method
C:\Program Files\MATLAB71\toolbox\robust\robust\@icsignal\size.m % icsignal method
C:\Program Files\MATLAB71\toolbox\robust\robust\@atom\size.m

us

unread,
Dec 31, 2009, 12:08:05 PM12/31/09
to
"memo mohd" <m-a...@hotmail.com> wrote in message <hhikrl$91q$1...@fred.mathworks.com>...

> ImageAnalyst <imagea...@mailinator.com> wrote in message <c467b04c-51cb-
> > He means go to the command window. At the >> prompt, type in "which
> > size -all" and hit enter. Then come back here and reply to this
> > thread and paste in what it typed out in the command window after you
> > issued that command. It should say something like:
> >
> > built-in (C:\Program Files\MATLAB\R2008b\toolbox\matlab\elmat\size)
> >
> > It should just list that one built-in function. If it lists two
> > things for you then you have "overridden" the size() function and that
> > is causing your problem. Hopefully that is clear enough.
>
> this is the result
> built-in (C:\Program Files\MATLAB71\toolbox\matlab\elmat\size)

ok...
- now, you said this was the error message:
> Undefined command/function 'Size'.
...even after you replaced Size with size(!?!?)...
- and CSSMers say:
based on what you show: this CANNOT be...

us

Andy

unread,
Dec 31, 2009, 12:25:05 PM12/31/09
to
> [rc,cc] = ndgrid(1:Size(M,1),1:Size(M,2));

Perhaps this is a silly questions, but did you replace Size with size in both instances? Also, did you use Size anywhere else in your code and forget to replace it with size? MATLAB is case sensitive, so size (lowercase) is a function and Size (capital S) is not. It is possible you're getting the same error ("Undefined command/function 'Size' ") but referencing some other place in your code that you forgot to change.

ImageAnalyst

unread,
Dec 31, 2009, 12:27:25 PM12/31/09
to
urs:
You have a good new year too. I've been dropping in quite a bit
lately since I'm taking vacation at home over these two weeks. So
I've had lots of time to help people and do coding. I'm working on
some demos and fun things that I may post shortly.

In the meantime I've proved that the code that memo gave can work if
he/she casts their image to single or double. The main problem was in
doing this
M(:) .* rc(:)
Now rc is a double, but if M is a uint8, then you can't do that. You
can't multiply an integer array by a double array. Multiplying by a
double scalar is fine, but not by a double array. Memo, to prove this
(and to fix your code), pay attention to this error "Integers can only
be combined with integers of the same class, or scalar doubles." and
then simply cast the image to double and watch the error vanish.
Here's a working version of your code (with better-named variables):

clc;
close all;
clear all;
workspace; % Display workspace panel.

% Read in standard MATLAB demo image and display it.
grayImage = double(imread('moon.tif')); % No error if you cast to
double
% grayImage = imread('moon.tif'); % Will cause error if you leave as
uint8.
imshow(grayImage, []);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.

[rows, cols] = ndgrid(1:size(grayImage,1), 1:size(grayImage,2));

% Calculate center of gravity.
totalMass = sum(grayImage(:));
xCenterOfGravity = sum(grayImage(:) .* cols(:)) / totalMass;
yCenterOfGravity = sum(grayImage(:) .* rows(:)) / totalMass;

% Plot the center of gravity over the image.
hold on; % Prevent image from getting blown away.
plot(xCenterOfGravity, yCenterOfGravity, 'r+', 'Linewidth', 5);
title('Image with center of gravity indicated by red spot');

memo mohd

unread,
Dec 31, 2009, 12:30:22 PM12/31/09
to
"us " <u...@neurol.unizh.ch> > ok...

> - now, you said this was the error message:
> > Undefined command/function 'Size'.
> ...even after you replaced Size with size(!?!?)...
> - and CSSMers say:
> based on what you show: this CANNOT be...
>
> us

now , this is the code and the error (after modification you are said to me)

M=double(imread('c:\m.jpg'));
[rc,cc] = ndgrid(1:size(M,1),1:size(M,2));


Mt = sum(M(:));
c1 = sum(M(:) .* rc(:)) / Mt
c2 = sum(M(:) .* cc(:)) / Mt

and here the error


??? Error using ==> times

Matrix dimensions must agree.

Error in ==> Untitled2 at 4

ImageAnalyst

unread,
Dec 31, 2009, 12:32:00 PM12/31/09
to
Which of those dozens of size functions get run first? If it's the
top one, then the problem is not with size and it is with having the
image be integer instead of floating point, as I said in my earlier
post (just a few minutes ago). That is also the problem if the other
sizes all do what you'd expect (same as the built-in one).

ImageAnalyst

unread,
Dec 31, 2009, 12:41:01 PM12/31/09
to

----------------------------------------------------
memo:
Examine this code and you will understand:

a = uint8(1:7) % Integer
b = rand(1,7) % Floating point
c = double(a) .* b % OK
d = a .* b % NOT OK

a =
1 2 3 4 5 6 7

b =
0.8909 0.9593 0.5472 0.1386 0.1493 0.2575 0.8407

c =
0.8909 1.9186 1.6416 0.5545 0.7465 1.5450 5.8850

??? Error using ==> times
Integers can only be combined with integers of the same class, or
scalar
doubles.

Error in ==> test at 11
d = a .* b

memo mohd

unread,
Dec 31, 2009, 1:34:03 PM12/31/09
to
ImageAnalyst <imagea...@mailinator.com> wrote in message <8b5f483a-b9dc-4874...@h10g2000vbm.googlegroups.com>...

ok , i am understand your example , but where the problem in code ,,
which the type must be modify ??
thanks a lot

ImageAnalyst

unread,
Dec 31, 2009, 1:54:31 PM12/31/09
to
On Dec 31, 1:34 pm, "memo mohd" <m-as...@hotmail.com> wrote:
> ok  , i am understand your example , but where the problem in code ,,
> which the type must be modify ??
> thanks a lot
-----------------------------------------------------------------
memo mohd:
Did you not see my corrected code (above) where I cast the image to
double?

grayImage = double(imread('moon.tif')); % No error if you cast to
double

Regards,
ImageAnalyst

memo mohd

unread,
Dec 31, 2009, 4:33:20 PM12/31/09
to
ImageAnalyst <imagea...@mailinator.com> wrote in message <9fea5d0a-6479-43cf-

> memo mohd:
> Did you not see my corrected code (above) where I cast the image to
> double?
>
> grayImage = double(imread('moon.tif')); % No error if you cast to
> double
>
> Regards,
> ImageAnalyst

very sorry , i am not see the code in above ,
the code work well with standared matlab image , but when i am but image from my computer ther is show this error

error using ==> times
Matrix dimensions must agree.

Error in ==> Untitled3 at 18

us

unread,
Dec 31, 2009, 4:48:02 PM12/31/09
to
ImageAnalyst <imagea...@mailinator.com> wrote in message <9fea5d0a-6479-43cf...@s31g2000yqs.googlegroups.com>...

well... if you all look at the very first reply in this doomed thread:
that is EXACTLY what i said (a long time ago)...

us

ImageAnalyst

unread,
Dec 31, 2009, 5:06:07 PM12/31/09
to
memo mohd:
Then the problem is that you're dealing with a color image - a 3D
image. That's why the length of grayImage(:) is not the same as cols
(:). You're converting it to a linear vector but the grayImage is
really an rgb image and it's length is 3 times as long as cols(:).
That's why they don't agree.

Please tell us what you want to do in that case.
You can either convert the image from rgb to grayscale according to
some formula (e.g. the rgb2ind() function), or you can calculate the
center of gravity for each color plane individually - in which case
the centroids will not necessarily align unless your rgb image is
monochrome (red = green = blue).

What does this say?
[rows columns numberOfColorChannels] = size(yourOriginalImageArray);
(Note: yourOriginalImageArray is what you tersely called "M.")
I bet it will show that numberOfColorChannels = 3, and so it's a color
image.
Regards,
ImageAnalyst

0 new messages