作业帮 > 综合 > 作业

用matlab图像配准中出现Subscript indices must either be real positive

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/11/09 10:27:47
用matlab图像配准中出现Subscript indices must either be real positive integers or logicals是什么题
I=imread('lily.jpg');
lily=rgb2gray(I);
M=imread('flowers.jpg');
flowers=rgb2gray(M);
%选择图像的配准区域
imshow(lily)
figure,imshow(flowers)
rect_lily=[50 121 200 120];
rect_flowers=[50 0 428 129];
sub_lily=imcrop(lily,rect_lily);
sub_flowers=imcrop(flowers,rect_flowers);
%计算两幅图的互相关
c=normxcorr2(sub_lily(:,:,1),sub_flowers(:,:,1));
figure,surf(c),shading flat
%根据互相关计算两幅图像的偏差
[max_c,imax]=max(abs(c(:)));
[ypeak,xpeak]=ind2sub(size(c),imax(1));
corr_offset=[(xpeak-size(sub_lily,2))
(ypeak-size(sub_lily,1))];
rect_offset=[(rect_flowers(1)-rect_lily(1))
(rect_flowers(2)-rect_lily(2))];
offset=corr_offset+rect_offset;
xoffset=offset(1);
yoffset=offset(2);
%从标准图像中提取待配准图像
xbegin=xoffset+1;
xend=xoffset+size(lily,2);
ybegin=yoffset+1;
yend=yoffset+size(lily,1);
extracted_lily=flowers(ybegin:yend,xbegin:xend,:);
if isequal(lily,extracted_lily)
disp('lily.jpg was extracted from flowers.jpg')
end
%修正待配图像
recovered_lily=uint8(zeros(size(flowers)));
recovered_lily(ybegin:yend,xbegin:xend,:)=lily;
figure,imshow(recovered_lily)
%将修正待配图像和标准图像融合
[m,n,p]=size(flowers);
mask=ones(m,n);
i=find(recovered_lily(:,:,1)= =0);
mask(i)=2;
figure,imshow(flowers(:,:,1)) %仅显示红色的花
hold on
h=imshow(recovered_lily); %覆盖recovered_lily图像
set(h,'AlphaData',mask)
Subscript indices must either be real positive integers or logicals
中文解释:下标索引必须是正整数类型或者逻辑类型
出错原因:在访问矩阵(包括向量、二维矩阵、多维数组,下同)的过程中,下标索引要么从 0 开始,要么出现了负数.注:matlab 的语法规定矩阵的索引从 1 开始,这与 C 等编程语言的习惯不一样.
解决办法:自己调试一下程序,把下标为 0 或者负数的地方修正.
另,我运行上述两句时是正确的,且得到了结果.
>> p=[1 -8 6 -30];
>> r=roots(p)
r =
7.7260
0.1370 + 1.9658i
0.1370 - 1.9658i
>>