Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.

2 views (last 30 days)
%Calculate ncc not working see again
bdiff(k+1, 1) = MatchCostNCC(blockL(:), blockR(:));
But the i have this problem : " Unable to perform assignment because the indices on the left side are not compatible with the size of the right side."
Even if BlockL is 25by25 so BlockR !.
Can anyone help me plz ?
  2 Comments
Image Analyst
Image Analyst on 25 Dec 2021
Is MatchCostNCC an array or a function?
Is bdiff 625 rows tall? Because blobL(:) is 625-by-1, so bdiff needs to have the same number of rows.
Hamid Fsian
Hamid Fsian on 25 Dec 2021
Hello,
MatchCostNCC is a function. I will leave here the code for it
function cost=MatchCostNCC(img1, img2)
%normalized cross-correlation
[nrow2, ncol2, ndim2]=size(img2);
win_size=9;
% params.ncc_winsize;
f=fspecial('average', win_size);
mu1=conv2(img1, f, 'same');
mu2=conv2(img2, f, 'same');
rau1=conv2(img1.^2, f, 'same');
rau2=conv2(img2.^2, f, 'same');
sigma1=rau1-mu1.^2;
sigma2=rau2-mu2.^2;
diff1=(img1-mu1)./(eps+sqrt(sigma1));
diff2=(img2-mu2)./(eps+sqrt(sigma2));
max_disp=14;
%params.max_disp;
% switch params.direction =-1
% case -1
disp_range=max_disp+1;
pad_direct='pre';
% case 0
% disp_range=2*max_disp+1;
% pad_direct='both';
% case 1
% disp_range=max_disp+1;
% pad_direct='post';
% end
cost=zeros(nrow2, ncol2, disp_range);
diff2_pad=padarray(diff2, [0 max_disp], 0, pad_direct);
for d=1:disp_range
temp=diff1.*diff2_pad(:, d:d+ncol2-1,:);
cost(:,:,d)=-conv2(temp, f, 'same');
end
and the bdiff is an empty array that I init it before the for loop. You can check my code here to
function dispImg=blockmatching(leftI,rightI,blockSize,maxd,cost_type)
if length(size(leftI))==3
% convert it grayscale
leftI = mean(leftI, 3);
rightI = mean(rightI, 3);
end
hb=fix(blockSize/2);
dispImg=zeros(size(rightI));
for i=hb+1:size(leftI,1)-hb
i;
for j=hb+1:size(leftI,2)-hb
blockR=rightI(i-hb:i+hb,j-hb:j+hb);
bdiff=[];
for k=0:min(maxd,size(leftI,2)-hb-j)
blockL=leftI(i-hb:i+hb,j-hb+k:j+hb+k);
switch lower(cost_type)
case 'ad'
%Absolute Difference
bdiff(k+1, 1) = abs(blockL(:) - blockR(:));
case 'sd'
% calculate sum of differences
bdiff(k+1, 1) = sum(blockL(:) - blockR(:));
case 'sad'
% calculate sum of absolute differences (SAD)
bdiff(k+1, 1) = sum(abs(blockL(:) - blockR(:)));
case 'mse'
%Calculate Mean Square Error
bdiff(k+1, 1) = immse(blockL, blockR);
case 'ncc'
%Calculate ncc not working see again
bdiff(k+1, 1) = MatchCostNCC(blockL(:), blockR(:));
end
end
[a1 b1]=min(bdiff);
if size(bdiff,1)>3 & b1>1 & b1<length(bdiff)
% use minimum disparities left and right block score and find
% subpixel disparity
dispImg(i, j) = (b1-1) - (0.5 * (bdiff(b1+1,1) - bdiff(b1-1,1)) / (bdiff(b1-1,1) - (2*bdiff(b1,1)) + bdiff(b1+1,1)));
else
% use minimum disparit match directly
dispImg(i, j) = (b1-1);
end
end

Sign in to comment.

Answers (1)

Jan
Jan on 25 Dec 2021
Let Matlab stop at the error using the debugger:
dbstop if error
Then run the code again. If it stops, chek the sizes of the used variables:
size(blockR(:))
size(blockL(:))
size(MatchCostNCC(blockL(:), blockR(:)))
size(bdiff(k+1, 1))
Remember, that the readers cannot know or check the sizes, but you can. We could guess, that k is a scalar, but you can clatify this.
My guess:
bdiff(k+1, 1) is a scalar, but MatchCostNCC(blockL(:), blockR(:))) is not. Than the assignment must fail.
  2 Comments
Hamid Fsian
Hamid Fsian on 25 Dec 2021
I am terribly sorry. you can check my MatchCostNCC
function cost=MatchCostNCC(img1, img2)
%normalized cross-correlation
[nrow2, ncol2, ndim2]=size(img2);
win_size=9;
% params.ncc_winsize;
f=fspecial('average', win_size);
mu1=conv2(img1, f, 'same');
mu2=conv2(img2, f, 'same');
rau1=conv2(img1.^2, f, 'same');
rau2=conv2(img2.^2, f, 'same');
sigma1=rau1-mu1.^2;
sigma2=rau2-mu2.^2;
diff1=(img1-mu1)./(eps+sqrt(sigma1));
diff2=(img2-mu2)./(eps+sqrt(sigma2));
max_disp=14;
%params.max_disp;
% switch params.direction =-1
% case -1
disp_range=max_disp+1;
pad_direct='pre';
% case 0
% disp_range=2*max_disp+1;
% pad_direct='both';
% case 1
% disp_range=max_disp+1;
% pad_direct='post';
% end
cost=zeros(nrow2, ncol2, disp_range);
diff2_pad=padarray(diff2, [0 max_disp], 0, pad_direct);
for d=1:disp_range
temp=diff1.*diff2_pad(:, d:d+ncol2-1,:);
cost(:,:,d)=-conv2(temp, f, 'same');
end
And also I will put the other function that contains the call for the MatchCostNCC
function dispImg=blockmatching(leftI,rightI,blockSize,maxd,cost_type)
if length(size(leftI))==3
% convert it grayscale
leftI = mean(leftI, 3);
rightI = mean(rightI, 3);
end
hb=fix(blockSize/2);
dispImg=zeros(size(rightI));
for i=hb+1:size(leftI,1)-hb
i;
for j=hb+1:size(leftI,2)-hb
blockR=rightI(i-hb:i+hb,j-hb:j+hb);
bdiff=[];
for k=0:min(maxd,size(leftI,2)-hb-j)
blockL=leftI(i-hb:i+hb,j-hb+k:j+hb+k);
switch lower(cost_type)
case 'ad'
%Absolute Difference
bdiff(k+1, 1) = abs(blockL(:) - blockR(:));
case 'sd'
% calculate sum of differences
bdiff(k+1, 1) = sum(blockL(:) - blockR(:));
case 'sad'
% calculate sum of absolute differences (SAD)
bdiff(k+1, 1) = sum(abs(blockL(:) - blockR(:)));
case 'mse'
%Calculate Mean Square Error
bdiff(k+1, 1) = immse(blockL, blockR);
case 'ncc'
%Calculate ncc not working see again
bdiff(k+1, 1) = MatchCostNCC(blockL(:), blockR(:));
end
end
[a1 b1]=min(bdiff);
if size(bdiff,1)>3 & b1>1 & b1<length(bdiff)
% use minimum disparities left and right block score and find
% subpixel disparity
dispImg(i, j) = (b1-1) - (0.5 * (bdiff(b1+1,1) - bdiff(b1-1,1)) / (bdiff(b1-1,1) - (2*bdiff(b1,1)) + bdiff(b1+1,1)));
else
% use minimum disparit match directly
dispImg(i, j) = (b1-1);
end
end
and to answer your question I used a breakpoint in order to see every step of my code (and because a function blockmatching is calling the function MatchCostNCC) and the size of every one was okay (625 by 1 ) for blockL(:) and blockR(:) but bdiff will take a new value on every loop.
Walter Roberson
Walter Roberson on 26 Dec 2021
blockR=rightI(i-hb:i+hb,j-hb:j+hb);
That is obviously intended to be a rectangular array.
blockL=leftI(i-hb:i+hb,j-hb+k:j+hb+k);
That is obviously intended to be a rectangular array.
bdiff(k+1, 1) = abs(blockL(:) - blockR(:));
With the variables on the right both being arrays, blockL(:) is a vector and blockR(:) is a vector, and the difference of vectors is a vector, and abs() of a vector is a vector. But you are assigning the result to a scalar location.
bdiff(k+1, 1) = MatchCostNCC(blockL(:), blockR(:));
Okay, you are passing vectors into MatchCostNCC
[nrow2, ncol2, ndim2]=size(img2);
but the code is expecting possibly RGB images
f=fspecial('average', win_size);
mu1=conv2(img1, f, 'same');
mu2=conv2(img2, f, 'same');
but conv2() cannot operate on more than 2 dimensions, so if an RGB image was passed in the code would fail.
for d=1:disp_range
temp=diff1.*diff2_pad(:, d:d+ncol2-1,:);
cost(:,:,d)=-conv2(temp, f, 'same');
end
You construct cost as a multidimensional array.
cost then gets passed back to the call
bdiff(k+1, 1) = MatchCostNCC(blockL(:), blockR(:));
and you then try to assign the multidimensional array to the scalar location bdiff(k+1,1)

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!