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?
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?
a hint:
- replace
Size
with
size
us
the same error :(
any other solutions please?
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
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.
hopefully(!)... :-) ...
anyhow, thanks, ia, for dropping in - and all the best for the next decade to you and your family...
urs
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
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
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.
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');
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
----------------------------------------------------
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
ok , i am understand your example , but where the problem in code ,,
which the type must be modify ??
thanks a lot
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
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
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