help ! how do i fix this error : Error using .* Integers can only be combined with integers of the same class, or scalar doubles.

5 次查看(过去 30 天)
clear;clc;
im=imread(' image.png');
R=im(:,:,1);
G=im(:,:,2);
B=im(:,:,3);
Y=0.299*R + 0.587*G + 0.114*B;
im = im2double(rgb2gray(im));
[~, T]=graythresh(im);
bwImage=imbinarize(im,T);
[M,~] = size(im);
[label,num] = bwlabel(im);
coor = zeros(num,2);
for n = 1:num
[x,y] = find(label==n);
m_iN = length(x);
for k = 1:m_iN
pSrc(k) = Y(x(k),y(k));
tmp_B(k,1) = x(k)^2;
tmp_B(k,2) = y(k)^2;
tmp_B(k,3) = x(k);
tmp_B(k,4) = y(k);
tmp_B(k,5) = 1;
end
[Q R]=qr(tmp_B);
pSrc=pSrc';
S = Q'*pSrc;
S = S(1:5);
R1 = R(1:5,1:5);
C = R1\S;
coor(n,:) = [-0.5*C(1)/C(3),-0.5*C(2)/C(4)];
end

回答(4 个)

KSSV
KSSV 2022-4-14
Read about class. To multiply two variables, they should be of same class.
a = int8(6) ;
b = int16(6) ;
c = int8(6) ;
class(a)
ans = 'int8'
class(b)
ans = 'int16'
class(c)
ans = 'int8'
a*c % no error
ans = int8 36
a*int8(b) % no error, class int16 converted to class int8
ans = int8 36
a*b % error
Error using *
Integers can only be combined with integers of the same class, or scalar doubles.

Walter Roberson
Walter Roberson 2022-4-14
The code you posted does not contain any .* operations, unless inside one of the calls.
But
S = Q'*pSrc;
If I scan correctly, Q is double precision but pSrc is uint8.
Y=0.299*R + 0.587*G + 0.114*B;
R G B are all uint8, and double times array of uint8 gives uint8, so Y will be uint8. Later you copy values out of Y into pSrc without having initialized pSrc so the uint8 type will be copied so pSrc will be uint8.

Bruno Luong
Bruno Luong 2022-4-14
Add cast to double statement after reading your image
im=imread(' image.png');
im = double(im);
...

yanqi liu
yanqi liu 2022-4-19
im=imread('football.jpg');
im=imresize(im,20/size(im,1),'bilinear');
R=im(:,:,1);
G=im(:,:,2);
B=im(:,:,3);
Y=0.299*R + 0.587*G + 0.114*B;
im = im2double(rgb2gray(im));
[~, T]=graythresh(im);
bwImage=imbinarize(im,T);
[M,~] = size(im);
[label,num] = bwlabel(im);
coor = zeros(num,2);
for n = 1:num
[x,y] = find(label==n);
m_iN = length(x);
for k = 1:m_iN
pSrc(k) = Y(x(k),y(k));
tmp_B(k,1) = x(k)^2;
tmp_B(k,2) = y(k)^2;
tmp_B(k,3) = x(k);
tmp_B(k,4) = y(k);
tmp_B(k,5) = 1;
end
[Q, R]=qr(tmp_B);
pSrc=pSrc';
S = double(Q')*double(pSrc);
S = S(1:5);
R1 = R(1:5,1:5);
C = R1\S;
coor(n,:) = [-0.5*C(1)/C(3),-0.5*C(2)/C(4)];
end
coor
coor = 1×2
0.0256 0.0199

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by