Matlab error: Index in position 2 exceeds array bounds (must not exceed 737). Error in FinalLaser (line 57) test_imb=i​magebb(tes​t_xmin-x_d​isp_max:te​st_xmax+x_​disp_max, test_ymin-​y_disp_max​:test_ymax​+y_disp_ma​x);

조회 수: 1 (최근 30일)
I am new to MATLAB, can anybody help me solve this error for code:
clear all;
clc;
close all;
imagea = imread('00020361.bmp');
imageb = imread('00020362.bmp');
%[xmax, ymax]=size(imageaa);
imageaa = imcrop(imagea,[100 100 736 736]);
imagebb = imcrop(imageb,[100 100 736 736]);
% Windows Size
wsize=[32,32];
w_width=wsize(1);
w_height=wsize(2);
% Center point grid
xmin=w_width/2;
ymin=w_height/2;
xgrid=100:w_width/2:736;
ygrid=100:w_height/2:736;
% Number of windows in total
w_xcount=length(xgrid);
w_ycount=length(ygrid);
% These correspond to the ranges for search windows in image b
x_disp_max=w_width/2;
y_disp_max=w_height/2;
% For every window, first we have to create the test matrix
% in image a. Then in image B, we have to correlate this test window
% around its originak position in image a, the range is
% predetermined. The point of max correlation corresponds to the final avg
% displacement of that window
test_ima(w_width, w_height)=0;
test_imb(w_width+2*x_disp_max, w_height+2*y_disp_max)=0;
dpx(w_xcount, w_ycount)=0;
dpy(w_xcount, w_ycount)=0;
xpeak1=0;
ypeak1=0;
% i, j are for the windows
% test_i and test_j are for the test window to be
% extracted from imaga aa
for i=1:(w_xcount)
for j=1:(w_ycount)
max_correlation=0;
test_xmin=xgrid(i)-w_width/2;
test_xmax=xgrid(i)+w_width/2;
test_ymin=ygrid(j)-w_height/2;
test_ymax=ygrid(j)+w_height/2;
x_disp=0;
y_disp=0;
test_ima=imageaa(test_xmin:test_xmax, test_ymin:test_ymax);
test_imb=imagebb(test_xmin-x_disp_max:test_xmax+x_disp_max, test_ymin-y_disp_max:test_ymax+y_disp_max);
correlation=normxcorr2(test_ima, test_imb);
[xpeak, ypeak]=find(correlation==max(correlation(:)));
% re scaling
xpeak1= test_xmin+xpeak-wsize(1)/2-x_disp_max;
ypeak1= test_ymin+ypeak-wsize(2)/2-y_disp_max;
dpx(i,j)=xpeak1-xgrid(i);
dpy(i,j)=ypeak1-ygrid(j);
end
end
% vector display
quiver(dpy,-dpx)

답변 (1개)

Guru Mohanty
Guru Mohanty 2020년 1월 17일
Hi, it is difficult to provide an exact solution without your original data. However, when I executed your code using demo data, it is trying to access pixels which are beyond the boundary set by imcrop function. However, after modifying the grid positions, the code should work.
clear all;
clc;
imagea = imread('tire.tif');
imageb = imread('trees.tif');
%[xmax, ymax]=size(imageaa);
imageaa = imcrop(imagea,[0 0 170 170]);
imagebb = imcrop(imageb,[0 0 170 170]);
% Windows Size
wsize=[32,32];
w_width=wsize(1);
w_height=wsize(2);
% Center point grid
xmin=w_width/2;
ymin=w_height/2;
xgrid=1+(w_width):w_width/2:170-(w_width);
ygrid=1+(w_width):w_height/2:170-(w_width);
% Number of windows in total
w_xcount=length(xgrid);
w_ycount=length(ygrid);
% These correspond to the ranges for search windows in image b
x_disp_max=w_width/2;
y_disp_max=w_height/2;
% For every window, first we have to create the test matrix
% in image a. Then in image B, we have to correlate this test window
% around its originak position in image a, the range is
% predetermined. The point of max correlation corresponds to the final avg
% displacement of that window
test_ima(w_width, w_height)=0;
test_imb(w_width+2*x_disp_max, w_height+2*y_disp_max)=0;
dpx(w_xcount, w_ycount)=0;
dpy(w_xcount, w_ycount)=0;
xpeak1=0;
ypeak1=0;
% i, j are for the windows
% test_i and test_j are for the test window to be
% extracted from imaga aa
for i=1:(w_xcount)
for j=1:(w_ycount)
max_correlation=0;
test_xmin=xgrid(i)-w_width/2;
test_xmax=xgrid(i)+w_width/2;
test_ymin=ygrid(j)-w_height/2;
test_ymax=ygrid(j)+w_height/2;
x_disp=0;
y_disp=0;
test_ima=imageaa(test_xmin:test_xmax, test_ymin:test_ymax);
test_imb=imagebb(test_xmin-x_disp_max:test_xmax+x_disp_max, test_ymin-y_disp_max:test_ymax+y_disp_max);
correlation=normxcorr2(test_ima, test_imb);
[xpeak, ypeak]=find(correlation==max(correlation(:)));
% re scaling
xpeak1= test_xmin+xpeak-wsize(1)/2-x_disp_max;
ypeak1= test_ymin+ypeak-wsize(2)/2-y_disp_max;
dpx(i,j)=xpeak1-xgrid(i);
dpy(i,j)=ypeak1-ygrid(j);
end
end
% vector display
quiver(dpy,-dpx)
image.jpg

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by