Why do I get the error message "Subscripted assignment dimension mismatch"?

Subscripted assignment dimension mismatch.
Error in L1SR (line 144)
hIm(1:3, :) = bhIm(1:3, :);
Error in yima (line 60)
ReconIm = L1SR(lowIm, zooming, patch_size, overlap, Dh, Dl, lambda, regres);
Error in Research_Experiment (line 248)
res{2} = {yima(low{1}, upscaling)};
The highlighted Line (144) to Line (148) has these on code
hIm(1:3, :) = bhIm(1:3, :);
hIm(:, 1:3) = bhIm(:, 1:3);
hIm(end-2:end, :) = bhIm(end-2:end, :);
hIm(:, end-2:end) = bhIm(:, end-2:end);
Why do I have these errors ??
Thanks

 采纳的回答

You have
[lhg, lwd] = size(lIm);
hhg = lhg*zooming;
hwd = lwd*zooming;
[...]
hIm = zeros([hhg, hwd]);
Assuming that a grayscale image was passed in, the size of lIm will be multiplied by zooming (which is not certain to be an integer by the way) and the result is used to construct hIm . Imagine that lIm is 256 x 256 and that zooming is set to 2, then 256*2 by 256*2 would give a hIm of 512 by 512.
You also have
bhIm = imresize(lIm, 3, 'bicubic');
If the original image lIm were 256 x 256 then the result of bhIm would be 768 x 768.
Now you do
hIm(1:3, :) = bhIm(1:3, :);
Left hand side variable would be 512 x 512, right hand side variable would be 768 x 768. You select three rows and all of the columns of the left hand side as the destination, so that would be 3 x 512 as the destination. The source is three rows and all the columns so that would be 3 x 758 as the source. You cannot store 2304 elements in a space only big enough for 1536.

5 个评论

Well understood. Thank you. But permit me to ask, whats the ideal array size to clear the Subscripted assignment dimension mismatch error..??
There are too few comments to figure out what the purpose of the various arrays are or why there might be empty rows or columns that have to have something copied into them.
the purposed is to reconstruct image patches to generate a high resolution results
Within the context of the code, what good does mIm do you? What good does bhIm do you? Why do they exist? How big are they expected to be? How do they fit into the computation? We know that hIm exists because it is the output variable. Why does hIm end up with places that need to be copied onto? What should be the source for that copying?
Based on my little knowlege, mlm has no relevance as its only to estimate array size and can be %% commented. bhIm is considered bicubic results of the input low resolution image lIm. The previous code (function file named "L1SR") and the below code (function file named "scaleup_Yang") both of which are called in the main code. The Subscripted assignment dimension mismatch error was generated when I uncomment scaleup_Yang file in main code while trying to compare it with other methods called up in the main file. The error can be traced on this scaleup_Yang function file below where LISR was used on the last line of the code. That how the previous code (LISR) fits into the computations of scaleup_Yang results to reconstruct final results.
Please how do i solve this issue?
function ReconIm = scaleup_Yang(lowIm, upscaling)
% Image super-resolution using sparse representation
d = 'CVPR08-SR';
addpath(d, [d '/Solver'], [d '/Sparse_coding'], [d '/Sparse_coding/sc2']);
tr_dir = 'CVPR08-SR/Data/Training'; % path for training images
% =====================================================================
% specify the parameter settings
patch_size = 3; % patch size for the low resolution input image
overlap = 2; % overlap between adjacent patches
lambda = 0.1; % sparsity parameter
zooming = 3; % zooming factor, if you change this, the dictionary needs to be retrained.
if exist('upscaling','var')
zooming = upscaling; % zooming factor, if you change this, the dictionary needs to be retrained.
end
regres = 'L1'; % 'L1' or 'L2', use the sparse representation directly, or use the supports for L2 regression
% =====================================================================
% training coupled dictionaries for super-resolution
if zooming==3
load([d '/Data/Dictionary/Dictionary.mat']);
else
if ~exist([d '/Data/Dictionary/Dictionary' num2str(zooming) '.mat'],'file')
num_patch = 50000; % number of patches to sample as the dictionary
codebook_size = 1024; % size of the dictionary
regres = 'L1'; % 'L1' or 'L2', use the sparse representation directly, or use the supports for L2 regression
% =====================================================================
% training coupled dictionaries for super-resolution
if ~exist([d '/Data/Dictionary/smp_patches' num2str(zooming) '.mat'],'file')
disp('Sampling image patches...');
[Xh, Xl] = rnd_smp_dictionary(tr_dir, patch_size, zooming, num_patch);
save([d '/Data/Dictionary/smp_patches' num2str(zooming) '.mat'], 'Xh', 'Xl');
else
load ([d '/Data/Dictionary/smp_patches' num2str(zooming) '.mat']);
end
[Dh, Dl] = coupled_dic_train(Xh, Xl, codebook_size, lambda);
save([d '/Data/Dictionary/Dictionary' num2str(zooming) '.mat'], 'Dh', 'Dl');
else
load([d '/Data/Dictionary/Dictionary' num2str(zooming) '.mat']);
end
end
% ======================================================================
% Super-resolution using sparse representation
disp('Start superresolution...');
ReconIm = L1SR(lowIm, zooming, patch_size, overlap, Dh, Dl, lambda, regres);

请先登录,再进行评论。

更多回答(1 个)

14 个评论

Hello, I tried to make corrections where necessary but still have the same error.
Can you please help guide me with this ?
I appreciate
function [hIm, ww] = L1SR(lIm, zooming, patch_size, overlap, Dh, Dl, lambda, regres)
% [hIm] = L1SR(lIm, zooming, patch_size, overlap, Dh, Dl, lambda)
%
% Inputs
% -lIm: low resolution input image, single channel, e.g.
% illuminance
% -zooming: zooming factor, e.g. 3
% -patch_size: patch size for the low resolution image
% -overlap: overlap among patches, e.g. 1
% -Dh: dictionary for the high resolution patches
% -Dl: dictionary for the low resolution patches
% -regres: 'L1' use the sparse representation directly to high
% resolution dictionary;
% 'L2' use the supports found by sparse representation
% and apply least square regression coefficients to high
% resolution dictionary.
% Ouputs
% -hIm: the recovered image, single channel
[lhg, lwd] = size(lIm);
hhg = lhg*zooming;
hwd = lwd*zooming;
mIm = imresize(lIm, 2,'bicubic');
[mhg, mwd] = size(mIm);
hpatch_size = patch_size*zooming;
mpatch_size = patch_size*2;
% extract gradient feature from lIm
hf1 = [-1,0,1];
vf1 = [-1,0,1]';
hf2 = [1,0,-2,0,1];
vf2 = [1,0,-2,0,1]';
lImG11 = conv2(mIm,hf1,'same');
lImG12 = conv2(mIm,vf1,'same');
lImG21 = conv2(mIm,hf2,'same');
lImG22 = conv2(mIm,vf2,'same');
lImfea(:,:,1) = lImG11;
lImfea(:,:,2) = lImG12;
lImfea(:,:,3) = lImG21;
lImfea(:,:,4) = lImG22;
lgridx = 2:patch_size-overlap:lwd-patch_size;
lgridx = [lgridx, lwd-patch_size];
lgridy = 2:patch_size-overlap:lhg-patch_size;
lgridy = [lgridy, lhg-patch_size];
mgridx = (lgridx - 1)*2 + 1;
mgridy = (lgridy - 1)*2 + 1;
% using linear programming to find sparse solution
bhIm = imresize(lIm, 3, 'bicubic');
hIm = zeros([hhg, hwd]);
nrml_mat = zeros([hhg, hwd]);
hgridx = (lgridx-1)*zooming + 1;
hgridy = (lgridy-1)*zooming + 1;
disp('Processing the patches sequentially...');
count = 0;
% loop to recover each patch
for xx = 1:length(mgridx),
for yy = 1:length(mgridy),
mcolx = mgridx(xx);
mrowy = mgridy(yy);
count = count + 1;
if ~mod(count, 100),
fprintf('.\n');
else
fprintf('.');
end;
mpatch = mIm(mrowy:mrowy+mpatch_size-1, mcolx:mcolx+mpatch_size-1);
mmean = mean(mpatch(:));
mpatchfea = lImfea(mrowy:mrowy+mpatch_size-1, mcolx:mcolx+mpatch_size-1, :);
mpatchfea = mpatchfea(:);
mnorm = sqrt(sum(mpatchfea.^2));
if mnorm > 1,
y = mpatchfea./mnorm;
else
y = mpatchfea;
end;
w = SolveLasso(Dl, y, size(Dl, 2), 'nnlasso', [], lambda);
% w = feature_sign(Dl, y, lambda);
if isempty(w),
w = zeros(size(Dl, 2), 1);
end;
switch regres,
case 'L1'
if mnorm > 1,
hpatch = Dh*w*mnorm;
else
hpatch = Dh*w;
end;
case 'L2'
idx = find(w);
lsups = Dl(:, idx);
hsups = Dh(:, idx);
w = inv(lsups'*lsups)*lsups'*mpatchfea;
hpatch = hsups*w;
otherwise
error('Unknown fitting!');
end;
hpatch = reshape(hpatch, [hpatch_size, hpatch_size]);
hpatch = hpatch + mmean;
hcolx = hgridx(xx);
hrowy = hgridy(yy);
hIm(hrowy:hrowy+hpatch_size-1, hcolx:hcolx+hpatch_size-1)...
= hIm(hrowy:hrowy+hpatch_size-1, hcolx:hcolx+hpatch_size-1) + hpatch;
nrml_mat(hrowy:hrowy+hpatch_size-1, hcolx:hcolx+hpatch_size-1)...
= nrml_mat(hrowy:hrowy+hpatch_size-1, hcolx:hcolx+hpatch_size-1) + 1;
end;
end;
fprintf('done!\n');
% fill the empty
hIm(1:3, :) = bhIm(1:3, :);
hIm(:, 1:3) = bhIm(:, 1:3);
hIm(end-2:end, :) = bhIm(end-2:end, :);
hIm(:, end-2:end) = bhIm(:, end-2:end);
nrml_mat(nrml_mat < 1) = 1;
hIm = hIm./nrml_mat;
% hIm = uint8(hIm);
There are no comments in your code, no explanation of what it's doing. I doubt anybody is going to spend the time trying to decipher it.
In any case, the problem is simple. With:
hIm(1:3, :) = bhIm(1:3, :);
you're telling matlab to copy all the columns of rows 1 to 3 of bhIm into all the columns of rows 1 to 3 of hIm. As the error message tells, you they don't have the same number of columns. So of course, matlab doesn't know what to do. Should it drop some columns (which ones?) if hIm has less columns? Should it just invent some data to fill the mising columns if hIm has more columns?
If you were expecting the two variables to have the same number of columns, then you need to debug your program to find out why this is not so. If you were not expecting the two to have the same number of columns, then why were you intending to do with that copy operation?
I'm not sure what you mean by sharing the code. You've already shared the code above.
The problem is not our lack of access to the code, it's the lack of explanation that goes with it.
Noted. What this code does is simply image super resolution via sparse representation of raw Image patches. I call this function file in my main code to execute image reconstruction.
So, did you or did you not expect hIm and bhIm to have the same number of columns?
Did you use the debugger to find why they do not have the same number of columns?
Obviously they should have the same number of columns. Sincerely, I am not good at it's use.. I really wish to resolve this
One of the two arrays has a size that is proportional to the input zooming that we do not know the value of. The other of the two arrays is forced to be a multiple of 3 because of the imresize() by a factor of 3. It is not obvious to me that the number of columns should be the same.
I did use the debugger, placing breakpoints at line 144 and 145. It ran successfully until line 141.. and ends there. Please, Find attached.
There are two imresize (2 and 3) on line 37 and 67. Upscaling factors used are 2,3 and 4.
Only the resize with factor 3 is used for bhIm .
When your code stops in the debugger you should examine the size() of the variables on the left side and right side of the assignment statement.
Okay. It says
hIm(1:3, :) = 512X512 double
bhIm(1:3, :) = 768X768 Single
bhIm = imresize(lIm, zooming, 'bicubic');
whereas in this present code, you replaced zooming with 3. If it is left as zooming then there is no conflict with the rest of the code.
You are right. I observed that ealier brforebefore now and resolved it already. Thanks for your seupport, You are th best!

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by